From 597945453d7b2527a3c9a2ed577d2fc5e3111163 Mon Sep 17 00:00:00 2001 From: Raphael Reitzig <4246780+reitzig@users.noreply.github.com> Date: Wed, 18 Jul 2018 23:35:32 +0200 Subject: [PATCH] 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 --- conf.d/sdk.fish | 57 +++++++++++++++++++++++++++++++++++++++------- functions/sdk.fish | 23 ------------------- test/wrapper.fish | 5 ++-- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/conf.d/sdk.fish b/conf.d/sdk.fish index acc6a05..71ee809 100644 --- a/conf.d/sdk.fish +++ b/conf.d/sdk.fish @@ -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 diff --git a/functions/sdk.fish b/functions/sdk.fish index 0e5dead..5a65e50 100644 --- a/functions/sdk.fish +++ b/functions/sdk.fish @@ -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" diff --git a/test/wrapper.fish b/test/wrapper.fish index c61c1af..4979cf4 100644 --- a/test/wrapper.fish +++ b/test/wrapper.fish @@ -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."