# We shouldn't rely on the user's grep settings to be correct. If we set these # here anytime asdf invokes grep it will be invoked with these options # shellcheck disable=SC2034 setglobal GREP_OPTIONS = '"--color=never'" # shellcheck disable=SC2034 setglobal GREP_COLORS = '' proc asdf_version { cat "$[asdf_dir]/VERSION" } proc asdf_dir { if test -z $ASDF_DIR { var current_script_path = $(BASH_SOURCE[0]) export ASDF_DIR setglobal ASDF_DIR = $[cd $[dirname $[dirname $current_script_path]] || exit; pwd] } echo $ASDF_DIR } proc asdf_repository_url { echo "https://github.com/asdf-vm/asdf-plugins.git" } proc get_install_path { var plugin = $1 var install_type = $2 var version = $3 mkdir -p "$[asdf_dir]/installs/$(plugin)" if test $install_type = "version" { echo "$[asdf_dir]/installs/$(plugin)/$(version)" } else { echo "$[asdf_dir]/installs/$(plugin)/$(install_type)-$(version)" } } proc list_installed_versions { var plugin_name = $1 var plugin_path = '' set plugin_path = $[get_plugin_path $plugin_name] var plugin_installs_path = '' set plugin_installs_path = "$[asdf_dir]/installs/$(plugin_name)" if test -d $plugin_installs_path { # shellcheck disable=SC2045 for install in [$[ls -d "$(plugin_installs_path)"/*/ !2 >/dev/null]] { basename $install } } } proc check_if_plugin_exists { # Check if we have a non-empty argument if test -z $(1) { display_error "No plugin given" exit 1 } if test ! -d "$[asdf_dir]/plugins/$1" { display_error "No such plugin" exit 1 } } proc check_if_version_exists { var plugin_name = $1 var version = $2 check_if_plugin_exists $plugin_name var install_path = '' set install_path = $[find_install_path $plugin_name $version] if test $version != "system" && test ! -d $install_path { display_error "version $version is not installed for $plugin_name" exit 1 } } proc get_plugin_path { echo "$[asdf_dir]/plugins/$1" } proc display_error { echo >&2 $1> !2 "$1" } proc get_version_in_dir { var plugin_name = $1 var search_path = $2 var legacy_filenames = $3 var asdf_version = '' set asdf_version = $[parse_asdf_version_file "$search_path/.tool-versions" $plugin_name] if test -n $asdf_version { echo "$asdf_version|$search_path/.tool-versions" return 0 } for filename in [$legacy_filenames] { var legacy_version = '' set legacy_version = $[parse_legacy_version_file "$search_path/$filename" $plugin_name] if test -n $legacy_version { echo "$legacy_version|$search_path/$filename" return 0 } } } proc find_version { var plugin_name = $1 var search_path = $2 var version = '' set version = $[get_version_from_env $plugin_name] if test -n $version { echo $version return 0 } var plugin_path = '' set plugin_path = $[get_plugin_path $plugin_name] var legacy_config = '' set legacy_config = $[get_asdf_config_value "legacy_version_file] var legacy_list_filenames_script = '' set legacy_list_filenames_script = ""$(plugin_path)/bin/list-legacy-filenames"" var legacy_filenames = ''"" if test $legacy_config = "yes" && test -f $legacy_list_filenames_script { set legacy_filenames = $[bash $legacy_list_filenames_script] } while test $search_path != "/" { set version = $[get_version_in_dir $plugin_name $search_path $legacy_filenames] if test -n $version { echo $version return 0 } set search_path = $[dirname $search_path] } get_version_in_dir $plugin_name $HOME $legacy_filenames } proc get_version_from_env { var plugin_name = $1 var upcase_name = '' set upcase_name = $[echo $plugin_name | tr '[:lower:]' '[:upper:]] var version_env_var = ""ASDF_$(upcase_name)_VERSION"" var version = $(!version_env_var) echo $version } proc find_install_path { var plugin_name = $1 var version = $2 # shellcheck disable=SC2162 env IFS=':' read -a version_info <<< $version if test $version = "system" { echo "" } elif test $(version_info[0]) = "ref" { var install_type = $(version_info[0]) var version = $(version_info[1]) get_install_path $plugin_name $install_type $version } elif test $(version_info[0]) = "path" { # This is for people who have the local source already compiled # Like those who work on the language, etc # We'll allow specifying path:/foo/bar/project in .tool-versions # And then use the binaries there var install_type = '"path'" var version = '"path'" echo $(version_info[1]) } else { var install_type = '"version'" var version = $(version_info[0]) get_install_path $plugin_name $install_type $version } } proc get_executable_path { var plugin_name = $1 var version = $2 var executable_path = $3 check_if_version_exists $plugin_name $version if test $version = "system" { setglobal path = $[echo $PATH | sed -e "s|$ASDF_DIR/shims||g; s|::|:|g] setglobal cmd = $[basename $executable_path] setglobal cmd_path = $[env PATH=$path which $cmd !2 > !1] # shellcheck disable=SC2181 if test $Status -ne 0 { return 1 } echo $cmd_path } else { var install_path = '' set install_path = $[find_install_path $plugin_name $version] echo "$(install_path)"/"$(executable_path)" } } proc parse_asdf_version_file { var file_path = $1 var plugin_name = $2 if test -f $file_path { var version = '' set version = $[grep "$(plugin_name) " $file_path | sed -e "s/^$(plugin_name) //] if test -n $version { echo $version return 0 } } } proc parse_legacy_version_file { var file_path = $1 var plugin_name = $2 var plugin_path = '' set plugin_path = $[get_plugin_path $plugin_name] var parse_legacy_script = '' set parse_legacy_script = ""$(plugin_path)/bin/parse-legacy-file"" if test -f $file_path { if test -f $parse_legacy_script { bash $parse_legacy_script $file_path } else { cat $file_path } } } proc get_preset_version_for { var plugin_name = $1 var search_path = '' set search_path = $[pwd] var version_and_path = '' set version_and_path = $[find_version $plugin_name $search_path] var version = '' set version = $[cut -d '|' -f 1 <<< $version_and_path]; echo $version } proc get_asdf_config_value_from_file { var config_path = $1 var key = $2 if test ! -f $config_path { return 0 } var result = '' set result = $[grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }] if test -n $result { echo $result } } proc get_asdf_config_value { var key = $1 var config_path = $(AZDF_CONFIG_FILE:-"$HOME/.asdfrc") var default_config_path = $(AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults") var result = '' set result = $[get_asdf_config_value_from_file $config_path $key] if test -n $result { echo $result } else { get_asdf_config_value_from_file $default_config_path $key } } proc repository_needs_update { var update_file_dir = '' set update_file_dir = ""$[asdf_dir]/tmp"" var update_file_name = '' set update_file_name = '"repo-updated'" # `find` outputs filename if it has not been modified in the last day var find_result = '' set find_result = $[find $update_file_dir -name $update_file_name -type f -mtime +1 -print] test -n $find_result } proc initialize_or_update_repository { var repository_url = '' var repository_path = '' set repository_url = $[asdf_repository_url] set repository_path = "$[asdf_dir]/repository" if test ! -d $repository_path { echo "initializing plugin repository..." git clone $repository_url $repository_path } elif repository_needs_update { echo "updating plugin repository..." shell {cd $repository_path && git fetch && git reset --hard origin/master} } mkdir -p "$[asdf_dir]/tmp" touch "$[asdf_dir]/tmp/repo-updated" } proc get_plugin_source_url { var plugin_name = $1 var plugin_config = '' set plugin_config = ""$[asdf_dir]/repository/plugins/$plugin_name"" if test -f $plugin_config { grep "repository" $plugin_config | awk -F'=' '{print $2}' | sed 's/ //' } }