Giter VIP home page Giter VIP logo

Comments (17)

donbeave avatar donbeave commented on July 26, 2024 13

Zsh users can add this to .zshrc:

# set JAVA_HOME on every change directory
function asdf_update_java_home {
  asdf current java 2>&1 > /dev/null
  if [[ "$?" -eq 0 ]]
  then
      export JAVA_HOME=$(asdf where java)
  fi
}

precmd() { asdf_update_java_home; }
# end set JAVA_HOME

Also, suggest to add it to the README.

from asdf-java.

rubencaro avatar rubencaro commented on July 26, 2024 2

I live with this in my .bashrc as a workaround. It updates JAVA_HOME every time it updates the prompt:

function asdf_update_java_home {
  asdf current java 2>&1 > /dev/null
  if [[ "$?" -eq 0 ]]
  then
      export JAVA_HOME=$(asdf where java)
  fi
}

function prompt_command {
  __vte_prompt_command  # put here whatever previous PROMPT_COMMAND value
  asdf_update_java_home
}

export PROMPT_COMMAND=prompt_command

Maybe you could add this to the README while the hook is not there.

from asdf-java.

halcyon avatar halcyon commented on July 26, 2024 2

@rubencaro @donbeave @joschi @jonatan-ivanov - how would you feel about #76 ?

from asdf-java.

joschi avatar joschi commented on July 26, 2024 1

@jonatan-ivanov I think there's currently no hook in asdf which will invoke a plugin when a version was selected (asdf global <plugin> <version> or asdf local <plugin> <version>).

from asdf-java.

joschi avatar joschi commented on July 26, 2024 1

@jonatan-ivanov exec-env controls the environment when a shim is executed (for example $HOME/.asdf/shims/java) but it doesn't change the environment you're calling asdf or the shim from, which would be required to update $JAVA_HOME after changing the version with asdf shell, asdf local, or asdf global.

It's not that it couldn't be added to asdf, but this hook doesn't exist right now, so there's nothing asdf-java could do about it. It has to be added in asdf first.

Related issue: asdf-vm/asdf#384

from asdf-java.

jthegedus avatar jthegedus commented on July 26, 2024 1

@joschi is this something the asdf-direnv plugin can solve? Does it have hooks for this kind of functionality?

from asdf-java.

joschi avatar joschi commented on July 26, 2024 1

@rubencaro You're right. I mixed up precmd and preexec. 👍

For reference: https://github.com/rothgar/mastering-zsh/blob/master/docs/config/hooks.md#hooks

from asdf-java.

thuandt avatar thuandt commented on July 26, 2024 1

@halcyon How about my solution? It worked with both system java and asdf provider java

asdf_update_java_home() {
  # shellcheck disable=SC2046
  JAVA_HOME=$(realpath $(dirname $(readlink -f $(asdf which java)))/../)
  export JAVA_HOME
}
autoload -U add-zsh-hook
add-zsh-hook precmd asdf_update_java_home

https://github.com/thuandt/zsh-config/blob/master/functions.sh#L3-L7
https://github.com/thuandt/zsh-config/blob/master/my-zsh-config.plugin.zsh#L103

from asdf-java.

donbeave avatar donbeave commented on July 26, 2024 1

@halcyon LGTM, I just tested zsh script, works perfectly.

from asdf-java.

halcyon avatar halcyon commented on July 26, 2024

You'll need to restart your shell for JAVA_HOME to be updated.

from asdf-java.

jonatan-ivanov avatar jonatan-ivanov commented on July 26, 2024

@halcyon Yes, that is the issue, I should not, right? :)

from asdf-java.

jonatan-ivanov avatar jonatan-ivanov commented on July 26, 2024

@joschi I'm not very familiar with asdf but isn't exec-env for this? https://asdf-vm.com/#/plugins-create?id=binexec-env
The main documentation is not very verbose, this might help a little more: https://github.com/asdf-vm/asdf/blob/master/docs/core-manage-versions.md#shims
And here how it's used: https://github.com/asdf-vm/asdf/blob/master/lib/utils.bash#L527-L543

from asdf-java.

jonatan-ivanov avatar jonatan-ivanov commented on July 26, 2024

@joschi Thank you very much!

from asdf-java.

rubencaro avatar rubencaro commented on July 26, 2024

@halcyon do you prefer a PR adding those to the readme? I could do it.

from asdf-java.

joschi avatar joschi commented on July 26, 2024

@rubencaro @donbeave Be aware that it can substantially slow down your shell when asdf is being called before every single command you're running in your shell.

from asdf-java.

rubencaro avatar rubencaro commented on July 26, 2024

@rubencaro @donbeave Be aware that it can substantially slow down your shell when asdf is being called before every single command you're running in your shell.

I didn't notice any effect after months using it like that. You should take into account that this will call asdf only when the shell renders the prompt, so right before going idle. It will not slow down execution of every single command 😁.

Maybe it would be enough to add a warning for that specific usecase (i.e. if somebody needs to render the prompt many times per second ?).

from asdf-java.

halcyon avatar halcyon commented on July 26, 2024

Thank you @thuandt
I've integrated your solution into PR #76

from asdf-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.