Offline mode now works.

- Initialization now delegates do sdkman-init.sh
 - bash-runner now catches all SDKMAN_* environment variables.

Fixes issue #17
Unearthes issue #19
This commit is contained in:
Raphael Reitzig 2018-07-18 23:35:32 +02:00
parent e01e9a4bea
commit 597945453d
3 changed files with 52 additions and 33 deletions

View file

@ -14,15 +14,56 @@ if not test -f "$sdkman_init"
exit 0
end
# Add binaries from installed SDKs to PATH, if necessary
switch "$PATH"
case "*$HOME/.sdkman/candidates/*"
# This is a subshell, SDKMAN! binaries already in path.
case '*'
# No SDKMAN! in PATH yet, so add candidate binaries
for ITEM in $HOME/.sdkman/candidates/*/current ;
set -gx PATH $PATH $ITEM/bin
# Runs the given command in bash, capturing some side effects
# and repeating them on the current fish shell.
# Returns the same status code as the given command.
function __fish_sdkman_run_in_bash
# We need to leave stdin and stdout of sdk free for user interaction.
# So, pipe relevant environment variables (which might have changed)
# through a file.
# But since now getting the exit code of sdk itself is a hassle,
# pipe it as well.
#
# TODO: Can somebody get this to work without the overhead of a file?
set pipe (mktemp)
bash -c "$argv[1];
echo -e \"\$?\" > $pipe;
env | grep -e '^SDKMAN_\|^PATH' >> $pipe;
echo \"SDKMAN_OFFLINE_MODE=\${SDKMAN_OFFLINE_MODE}\" >> $pipe" # it's not an environment variable!
set bashDump (cat $pipe; rm $pipe)
set sdkStatus $bashDump[1]
set bashEnv $bashDump[2..-1]
# If SDKMAN! succeeded, copy relevant environment variables
# to the current shell (they might have changed)
if [ $sdkStatus = 0 ]
for line in $bashEnv
set parts (string split "=" $line)
set var $parts[1]
set value (string join "=" $parts[2..-1])
switch "$var"
case "PATH"
# Special treatment: need fish list instead
# of colon-separated list.
set value (string split : "$value")
end
if test -n value
set -gx $var $value
# Note: This makes SDKMAN_OFFLINE_MODE an environment variable.
# That gives it the behavariour we _want_!
end
end
end
return $sdkStatus
end
# If this is a subshell of a(n initialized) fish, no initialization
# necessary. Otherwise:
if not set -q SDKMAN_DIR
__fish_sdkman_run_in_bash "source $sdkman_init"
end

View file

@ -5,29 +5,6 @@ if not test -f "$sdkman_init"
exit 0
end
# Runs the given command in bash, capturing some side effects
# and repeating them on the current fish shell.
# Returns the same status code as the given command.
function __fish_sdkman_run_in_bash
# We need to leave stdin and stdout of sdk free for user interaction.
# So, pipe PATH (which might have changed) through a file.
# Now, getting the exit code of sdk itself is a hassle so pipe it as well.
# TODO: Can somebody get this to work without the overhead of a file?
set pipe (mktemp)
bash -c "$argv[1]; echo -e \"\$PATH\n\$?\" > $pipe"
set bashDump (cat $pipe; rm $pipe)
set bashPath $bashDump[1]
set sdkStatus $bashDump[2]
# If SDKMAN! succeeded, copy PATH here (might have changed)
if [ $sdkStatus = 0 ]
set newPath (string split : "$bashPath")
set -gx PATH $newPath
end
return $sdkStatus
end
# Declare the sdk command for fish
function sdk -d "Manage SDKs"
__fish_sdkman_run_in_bash "source $sdkman_init && sdk $argv"

View file

@ -7,7 +7,8 @@ set test_commands \
"sdk version" \
"sdk list java" \
"sdk update" \
"sdk use ant 1.9.9"
"sdk use ant 1.9.9" \
"sdk offline enable > /dev/null; sdk install ant foo"
set test_count (count $test_commands)
set check_count (math "3 * $test_count")
@ -39,7 +40,7 @@ for sdk_cmd in $test_commands
echo ""
end
rm {sout, status, path}_{bash, fish}
rm {sout,status,path}_{bash,fish}
echo "Ran $test_count commands with 3 checks each."
echo "$failures/$check_count checks failed."