Created
September 17, 2021 12:03
-
-
Save mikybars/42fadf5580940145dd7ab1010f0b5b99 to your computer and use it in GitHub Desktop.
[Shell command proxy] Lazy initialization of heavy tool (SDKMAN!) + interception of `sdk use java` to fuzzy filter installed versions when none specified. #shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export SDKMAN_DIR="/usr/local/sdkman" | |
# https://mharrison.org/post/bashfunctionoverride/ | |
copy_function() { | |
local orig_fn=$(declare -f $1) | |
local new_fn="$2${orig_fn#$1}" | |
eval "$new_fn" | |
} | |
sdk_fuzzy_find_local_version() { | |
local candidate=$1 | |
__sdkman_build_version_csv $candidate | tr ',' '\n' | sort -r -n | | |
fzf -d '\.' --nth 1 # filter by major version | |
} | |
is_loaded() { | |
(( ${+SDKMAN_VERSION} )) | |
} | |
init() { | |
source "${SDKMAN_DIR}/bin/sdkman-init.sh" | |
copy_function sdk sdk_orig # `sdk` is not actually a shell command but a function | |
copy_function sdk_proxy sdk # reinject proxy after initialization | |
} | |
lazy_init() { | |
is_loaded || init | |
} | |
# https://frederic-hemberger.de/articles/speed-up-initial-zsh-startup-with-lazy-loading/ | |
sdk_proxy() { | |
lazy_init | |
if (( $# == 2 )) && [[ $1 =~ ^(u|use)$ ]]; then # proxied subcommand | |
local candidate=$2 | |
sdk_orig use $candidate $(sdk_fuzzy_find_local_version $candidate) | |
else | |
# every other use of sdk goes through the original command | |
sdk_orig "$@" | |
fi | |
} | |
copy_function sdk_proxy sdk # inject proxy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment