Skip to content

Commit

Permalink
System Java now works on macOS
Browse files Browse the repository at this point in the history
Fixes halcyon#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.
  • Loading branch information
delgurth committed Apr 6, 2021
1 parent cedde6d commit 9dfe9fa
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 46 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.{y,ya}ml]
indent_size = 2
108 changes: 104 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,14 +34,114 @@ 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: |
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
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}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,51 @@
- [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:

`asdf list-all java`

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.
Expand Down
29 changes: 17 additions & 12 deletions set-java-home.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 19 additions & 8 deletions set-java-home.fish
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions set-java-home.xsh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 11 additions & 6 deletions set-java-home.zsh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9dfe9fa

Please sign in to comment.