From 9dfe9faabe72e51d0224ce6fe9509422ba78fcb1 Mon Sep 17 00:00:00 2001 From: Wessel van Norel Date: Mon, 5 Apr 2021 03:01:01 +0200 Subject: [PATCH] System Java now works on macOS Fixes https://github.com/halcyon/asdf-java/issues/132 Added special case for macOS in all 4 set-java-home scripts. Also created tests to validate this. Unfortunately xonsh does not seem to have a way to exit with other status then 0, so this will not report errors (yet). Updated the README.md to contain information on how to use the set-java-home scripts. Cloned mstksg/get-package and released delgurth/get-package@v3 because it was using linuxbrew on Ubuntu and because I needed the apt-update (not released by mstksg) and -y flag in the apt-get install (when running github action on https://github.com/nektos/act) added .editorconfig because of mixed indent styles added .gitignore to ignore Intellij directory asdf/asdf.sh needs $PWD in front as soon as you change directories added tests for JAVA_HOME setting added tests for macOS JAVA_HOME setting in case version is set to system Unfortunately the tests are not exactly as it would be on a system because of the way github actions spawn shells. Have to call the bash function _asdf_java_prompt_command and the fish & zsh function asdf_update_java_home to actually update the JAVA_HOME. --- .editorconfig | 8 +++ .github/workflows/tests.yml | 108 ++++++++++++++++++++++++++++++++++-- .gitignore | 1 + README.md | 33 ++++++----- set-java-home.bash | 29 ++++++---- set-java-home.fish | 27 ++++++--- set-java-home.xsh | 11 +++- set-java-home.zsh | 17 ++++-- 8 files changed, 188 insertions(+), 46 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..d4798dac --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +[*] +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{y,ya}ml] +indent_size = 2 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index be4e01ee..79a1747f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,10 +16,10 @@ jobs: TERM: dumb steps: - uses: actions/checkout@v2 - - uses: mstksg/get-package@v1 + - uses: delgurth/get-package@v3 with: - brew: bash coreutils jq shellcheck - apt-get: jq shellcheck + brew: bash coreutils jq shellcheck fish xonsh zsh + apt-get: jq shellcheck zsh fish xonsh bash - name: Run ShellCheck run: | shellcheck -V @@ -34,9 +34,10 @@ jobs: env: GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - . asdf/asdf.sh + . $PWD/asdf/asdf.sh asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version adoptopenjdk-8.0.252+9.1.openj9-0.20.0 java -version - name: macOS Check java_home integration + if: matrix.os == 'macOS-latest' env: GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -44,4 +45,103 @@ jobs: echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}" . asdf/asdf.sh asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version zulu-8.52.0.23 /usr/libexec/java_home -V 2>&1 | grep "Zulu 8.52.0.23" + - name: Setup JAVA_HOME setting + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + export ASDF_CONFIG_FILE=${HOME}"/.asdfrc" + echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}" + . $PWD/asdf/asdf.sh + asdf plugin add java "$GITHUB_WORKSPACE" + asdf install java adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + asdf global java adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + echo ". $PWD/asdf/asdf.sh" >> $HOME/.zshrc + echo ". $HOME/.asdf/plugins/java/set-java-home.zsh" >> $HOME/.zshrc + mkdir -p $HOME/.config/fish/functions/ + ln -s $HOME/.asdf/plugins/java/set-java-home.fish $HOME/.config/fish/functions/asdf_update_java_home.fish + echo "source $PWD/asdf/asdf.fish" >> $HOME/.config/fish/config.fish + echo "asdf_update_java_home" >> $HOME/.config/fish/config.fish + - name: Run JAVA_HOME setting test fish + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + shell: fish {0} + - name: Run JAVA_HOME setting test bash + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + . $PWD/asdf/asdf.sh + . set-java-home.bash + _asdf_java_prompt_command + echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + shell: bash {0} + - name: Run JAVA_HOME setting test xonsh (returns success even though it might fail!) + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + source-bash $PWD/asdf/asdf.sh + source set-java-home.xsh + echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + shell: xonsh {0} + - name: Run JAVA_HOME setting test zsh + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + source $HOME/.zshrc + asdf_update_java_home + echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0 + shell: zsh {0} + - name: Setup system tool-version test on macOS + if: matrix.os == 'macOS-latest' + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + export ASDF_CONFIG_FILE=${HOME}"/.asdfrc" + echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}" + . $PWD/asdf/asdf.sh + . set-java-home.bash + asdf install java zulu-16.28.11 + mkdir system + cd system + echo "java system" > .tool-versions + - name: Run system tool-version test on macOS with fish + if: matrix.os == 'macOS-latest' + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + cd system + asdf_update_java_home + echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/" + shell: fish {0} + - name: Run system tool-version test on macOS with bash + if: matrix.os == 'macOS-latest' + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + . $PWD/asdf/asdf.sh + . set-java-home.bash + cd system + _asdf_java_prompt_command + echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/" + shell: bash {0} + - name: Run system tool-version test on macOS with xonsh (returns success even though it might fail!) + if: matrix.os == 'macOS-latest' + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + source-bash $PWD/asdf/asdf.sh + source set-java-home.xsh + cd system + echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/" + shell: xonsh {0} + - name: Run system tool-version test on macOS with zsh if: matrix.os == 'macOS-latest' + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + source $HOME/.zshrc + cd system + asdf_update_java_home + echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/" + shell: zsh {0} diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9f11b755 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/README.md b/README.md index 94bfdad4..3bdaa8c0 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,19 @@ - [unzip](http://infozip.sourceforge.net/UnZip.html) - [jq](https://stedolan.github.io/jq/) (only for updating the release data) +_Besides bash (the shell used by the maintainer) this plugin **should** work on Fish, Zsh and Xonsh_ + ## Install ``` -asdf plugin-add java https://github.com/halcyon/asdf-java.git +asdf plugin add java ``` ## Use Check [asdf](https://asdf-vm.github.io/asdf/) for instructions on how to install & manage versions of Java. -## Install +## Install new Java version List candidate JDKs: @@ -29,28 +31,31 @@ List candidate JDKs: Install a candidate listed from the previous command like this: -`asdf install java adopt-openjdk-12.0.2+10.2` +`asdf install java adoptopenjdk-12.0.2+10.1` Select an installed candidate for use like this: -`asdf global java adopt-openjdk-12.0.2+10.2` +`asdf global java adoptopenjdk-12.0.2+10.1` ## JAVA_HOME To set JAVA_HOME in your shell's initialization add the following: -`. ~/.asdf/plugins/java/set-java-home.bash` - -For zsh shell, instead use: - -`. ~/.asdf/plugins/java/set-java-home.zsh` +### Bash +`echo ". ~/.asdf/plugins/java/set-java-home.bash" >> ~/.bashrc` -For fish shell, instead use: +### Zsh +`echo ". ~/.asdf/plugins/java/set-java-home.zsh" >> ~/.zshrc` -`. ~/.asdf/plugins/java/set-java-home.fish` - -For xonsh shell, instead use: +### Fish +``` +mkdir -p ~/.config/fish/functions/ +ln -s ~/.asdf/plugins/java/set-java-home.fish ~/.config/fish/functions/asdf_update_java_home.fish +echo "asdf_update_java_home" >> ~/.config/fish/config.fish +``` +_the mkdir is only needed in case you didn't add any functions yet_ -`source ~/.asdf/plugins/java/set-java-home.xsh` +### Xonsh +`echo "source ~/.asdf/plugins/java/set-java-home.xsh" >> ~/.xonshrc` ## macOS Integration Some applications in macOS use `/usr/libexec/java_home` to set java home. diff --git a/set-java-home.bash b/set-java-home.bash index 56218a70..3a1df7e5 100644 --- a/set-java-home.bash +++ b/set-java-home.bash @@ -5,22 +5,27 @@ function _asdf_java_absolute_dir_path { } function _asdf_java_update_java_home() { - local java_path - java_path="$(asdf which java)" - if [[ -n "${java_path}" ]]; then - export JAVA_HOME - JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")" - fi + local java_path + java_path="$(asdf which java)" + if [[ -n "${java_path}" ]]; then + if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then + export JAVA_HOME + JAVA_HOME="$(/usr/libexec/java_home)" + else + export JAVA_HOME + JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")" + fi + fi } function _asdf_java_prompt_command() { - if [[ "${PWD}" == "${LAST_PWD}" ]]; then - return - fi - LAST_PWD="${PWD}" - _asdf_java_update_java_home + if [[ "${PWD}" == "${LAST_PWD}" ]]; then + return + fi + LAST_PWD="${PWD}" + _asdf_java_update_java_home } if ! [[ "${PROMPT_COMMAND:-}" =~ _asdf_java_prompt_command ]]; then - PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}" + PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}" fi diff --git a/set-java-home.fish b/set-java-home.fish index d75a583f..d08acf79 100644 --- a/set-java-home.fish +++ b/set-java-home.fish @@ -1,10 +1,21 @@ function asdf_update_java_home --on-event fish_prompt - set --local java_path (asdf which java) - if test -n "$java_path" - set --local full_path (builtin realpath "$java_path") - - # `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls - # in order to get the correct JAVA_HOME directory - set -gx JAVA_HOME (dirname (dirname "$full_path")) - end + set --local java_path (asdf which java) + if test -n "$java_path" + set --local full_path (builtin realpath "$java_path") + + switch (uname) + case Darwin + if test $java_path = "/usr/bin/java" + set -gx JAVA_HOME (/usr/libexec/java_home) + else + # `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls + # in order to get the correct JAVA_HOME directory + set -gx JAVA_HOME (dirname (dirname "$full_path")) + end + case '*' + # `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls + # in order to get the correct JAVA_HOME directory + set -gx JAVA_HOME (dirname (dirname "$full_path")) + end + end end diff --git a/set-java-home.xsh b/set-java-home.xsh index 7a1ba49d..331eb123 100644 --- a/set-java-home.xsh +++ b/set-java-home.xsh @@ -1,9 +1,16 @@ #!/usr/bin/env xonsh def asdf_update_java_home() -> None: - $java_path=$(asdf which java) + import xonsh.platform + $java_path=$(asdf which java).rstrip('\n') if len($java_path) > 0: - $JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n') + if xonsh.platform.ON_DARWIN: + if $java_path == '/usr/bin/java': + $JAVA_HOME=$(/usr/libexec/java_home).rstrip('\n') + else: + $JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n') + else: + $JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n') del $java_path @events.on_chdir diff --git a/set-java-home.zsh b/set-java-home.zsh index 887a13ad..bf55e085 100644 --- a/set-java-home.zsh +++ b/set-java-home.zsh @@ -1,10 +1,15 @@ asdf_update_java_home() { - local java_path - java_path="$(asdf which java)" - if [[ -n "${java_path}" ]]; then - export JAVA_HOME - JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")" - fi + local java_path + java_path="$(asdf which java)" + if [[ -n "${java_path}" ]]; then + if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then + export JAVA_HOME + JAVA_HOME="$(/usr/libexec/java_home)" + else + export JAVA_HOME + JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")" + fi + fi } autoload -U add-zsh-hook