4
GREP_OPTIONS="--color=never"
10
version="v$(cat "$(asdf_dir)/version.txt")"
11
if [ -d "$(asdf_dir)/.git" ]; then
12
git_rev="$(git --git-dir "$(asdf_dir)/.git" rev-parse --short HEAD)"
13
printf "%s-%s\n" "$version" "$git_rev"
15
printf "%s\n" "$version"
19
asdf_tool_versions_filename() {
20
printf '%s\n' "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-.tool-versions}"
24
printf '%s\n' "${ASDF_CONFIG_FILE:-$HOME/.asdfrc}"
30
if [ -n "${ASDF_DATA_DIR}" ]; then
31
data_dir="${ASDF_DATA_DIR}"
32
elif [ -n "$HOME" ]; then
33
data_dir="$HOME/.asdf"
38
printf "%s\n" "$data_dir"
42
if [ -z "$ASDF_DIR" ]; then
43
local current_script_path=${BASH_SOURCE[0]}
45
cd -- "$(dirname "$(dirname "$current_script_path")")" || exit
49
printf '%s\n' "$ASDF_DIR"
53
asdf_plugin_repository_url() {
54
printf "https://github.com/asdf-vm/asdf-plugins.git\n"
63
install_dir="$(asdf_data_dir)/installs"
65
[ -d "${install_dir}/${plugin}" ] || mkdir -p "${install_dir}/${plugin}"
67
if [ "$install_type" = "version" ]; then
68
printf "%s/%s/%s\n" "$install_dir" "$plugin" "$version"
69
elif [ "$install_type" = "path" ]; then
70
printf "%s\n" "$version"
72
printf "%s/%s/%s-%s\n" "$install_dir" "$plugin" "$install_type" "$version"
82
download_dir="$(asdf_data_dir)/downloads"
84
[ -d "${download_dir}/${plugin}" ] || mkdir -p "${download_dir}/${plugin}"
86
if [ "$install_type" = "version" ]; then
87
printf "%s/%s/%s\n" "$download_dir" "$plugin" "$version"
88
elif [ "$install_type" = "path" ]; then
91
printf "%s/%s/%s-%s\n" "$download_dir" "$plugin" "$install_type" "$version"
95
list_installed_versions() {
98
plugin_path=$(get_plugin_path "$plugin_name")
100
local plugin_installs_path
101
plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}"
103
if [ -d "$plugin_installs_path" ]; then
104
for install in "${plugin_installs_path}"/*/; do
105
[[ -e "$install" ]] || break
106
basename "$install" | sed 's/^ref-/ref:/'
111
check_if_plugin_exists() {
115
if [ -z "${1}" ]; then
116
display_error "No plugin given"
120
if [ ! -d "$(asdf_data_dir)/plugins/$plugin_name" ]; then
121
display_error "No such plugin: $plugin_name"
126
check_if_version_exists() {
130
check_if_plugin_exists "$plugin_name"
133
install_path=$(find_install_path "$plugin_name" "$version")
135
if [ "$version" != "system" ] && [ ! -d "$install_path" ]; then
140
version_not_installed_text() {
144
printf "version %s is not installed for %s\n" "$version" "$plugin_name"
149
printf "%s\n" "$(asdf_data_dir)/plugins/$1"
151
printf "%s\n" "$(asdf_data_dir)/plugins"
156
printf "%s\n" "$1" >&2
159
get_version_in_dir() {
162
local legacy_filenames=$3
166
file_name=$(asdf_tool_versions_filename)
167
asdf_version=$(parse_asdf_version_file "$search_path/$file_name" "$plugin_name")
169
if [ -n "$asdf_version" ]; then
170
printf "%s\n" "$asdf_version|$search_path/$file_name"
174
for filename in $legacy_filenames; do
176
legacy_version=$(parse_legacy_version_file "$search_path/$filename" "$plugin_name")
178
if [ -n "$legacy_version" ]; then
179
printf "%s\n" "$legacy_version|$search_path/$filename"
190
version=$(get_version_from_env "$plugin_name")
191
if [ -n "$version" ]; then
193
upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_')
194
local version_env_var="ASDF_${upcase_name}_VERSION"
196
printf "%s\n" "$version|$version_env_var environment variable"
201
plugin_path=$(get_plugin_path "$plugin_name")
203
legacy_config=$(get_asdf_config_value "legacy_version_file")
204
local legacy_list_filenames_script
205
legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames"
206
local legacy_filenames=""
208
if [ "$legacy_config" = "yes" ] && [ -f "$legacy_list_filenames_script" ]; then
209
legacy_filenames=$("$legacy_list_filenames_script")
212
while [ "$search_path" != "/" ]; do
213
version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames")
214
if [ -n "$version" ]; then
215
printf "%s\n" "$version"
218
search_path=$(dirname "$search_path")
221
get_version_in_dir "$plugin_name" "$HOME" "$legacy_filenames"
223
if [ -f "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" ]; then
224
versions=$(parse_asdf_version_file "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$plugin_name")
225
if [ -n "$versions" ]; then
226
printf "%s\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME"
232
display_no_version_set() {
234
printf "No version is set for %s; please run \`asdf <global | shell | local> %s <version>\`\n" "$plugin_name" "$plugin_name"
237
get_version_from_env() {
240
upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_')
241
local version_env_var="ASDF_${upcase_name}_VERSION"
242
local version=${!version_env_var:-}
243
printf "%s\n" "$version"
251
IFS=':' read -a version_info <<<"$version"
253
if [ "$version" = "system" ]; then
255
elif [ "${version_info[0]}" = "ref" ]; then
256
local install_type="${version_info[0]}"
257
local version="${version_info[1]}"
258
get_install_path "$plugin_name" "$install_type" "$version"
259
elif [ "${version_info[0]}" = "path" ]; then
264
local install_type="path"
267
util_resolve_user_path "${version_info[1]}"
268
printf "%s\n" "${util_resolve_user_path_reply}"
270
local install_type="version"
271
local version="${version_info[0]}"
272
get_install_path "$plugin_name" "$install_type" "$version"
276
get_custom_executable_path() {
278
local install_path=$2
279
local executable_path=$3
282
if [ -x "${plugin_path}/bin/exec-path" ]; then
283
cmd=$(basename "$executable_path")
286
relative_path=$(printf "%s\n" "$executable_path" | sed -e "s|${install_path}/||")
287
relative_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$relative_path")"
288
executable_path="$install_path/$relative_path"
291
printf "%s\n" "$executable_path"
294
get_executable_path() {
297
local executable_path=$3
299
check_if_version_exists "$plugin_name" "$version"
301
if [ "$version" = "system" ]; then
302
path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims")
303
cmd=$(basename "$executable_path")
304
cmd_path=$(PATH=$path command -v "$cmd" 2>&1)
306
if [ $? -ne 0 ]; then
309
printf "%s\n" "$cmd_path"
312
install_path=$(find_install_path "$plugin_name" "$version")
313
printf "%s\n" "${install_path}"/"${executable_path}"
317
parse_asdf_version_file() {
321
if [ -f "$file_path" ]; then
323
version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //")
325
if [ -n "$version" ]; then
326
if [[ "$version" == path:* ]]; then
327
util_resolve_user_path "${version#path:}"
328
printf "%s\n" "path:${util_resolve_user_path_reply}"
330
printf "%s\n" "$version"
338
parse_legacy_version_file() {
343
plugin_path=$(get_plugin_path "$plugin_name")
344
local parse_legacy_script
345
parse_legacy_script="${plugin_path}/bin/parse-legacy-file"
347
if [ -f "$file_path" ]; then
348
if [ -f "$parse_legacy_script" ]; then
349
"$parse_legacy_script" "$file_path"
356
get_preset_version_for() {
360
local version_and_path
361
version_and_path=$(find_versions "$plugin_name" "$search_path")
363
version=$(cut -d '|' -f 1 <<<"$version_and_path")
365
printf "%s\n" "$version"
368
get_asdf_config_value_from_file() {
372
if [ ! -f "$config_path" ]; then
376
util_validate_no_carriage_returns "$config_path"
379
result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
380
if [ -n "$result" ]; then
381
printf "%s\n" "$result"
388
get_asdf_config_value() {
391
config_path=$(asdf_config_file)
392
local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
394
local local_config_path
395
local_config_path="$(find_file_upwards ".asdfrc")"
397
get_asdf_config_value_from_file "$local_config_path" "$key" ||
398
get_asdf_config_value_from_file "$config_path" "$key" ||
399
get_asdf_config_value_from_file "$default_config_path" "$key"
405
repository_needs_update() {
406
local plugin_repository_last_check_duration
409
plugin_repository_last_check_duration="$(get_asdf_config_value "plugin_repository_last_check_duration")"
411
if [ "never" != "$plugin_repository_last_check_duration" ]; then
412
local update_file_dir
413
local update_file_name
414
update_file_dir="$(asdf_data_dir)/tmp"
415
update_file_name="repo-updated"
417
sync_required=$(find "$update_file_dir" -name "$update_file_name" -type f -mmin +"${plugin_repository_last_check_duration:-60}" -print)
423
initialize_or_update_plugin_repository() {
425
local repository_path
427
disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")"
428
if [ "yes" = "$disable_plugin_short_name_repo" ]; then
429
printf "Short-name plugin repository is disabled\n" >&2
433
repository_url=$(asdf_plugin_repository_url)
434
repository_path=$(asdf_data_dir)/repository
436
if [ ! -d "$repository_path" ]; then
437
printf "initializing plugin repository..."
438
git clone "$repository_url" "$repository_path"
439
elif repository_needs_update; then
440
printf "updating plugin repository..."
441
git -C "$repository_path" fetch
442
git -C "$repository_path" reset --hard origin/master
445
[ -d "$(asdf_data_dir)/tmp" ] || mkdir -p "$(asdf_data_dir)/tmp"
446
touch "$(asdf_data_dir)/tmp/repo-updated"
449
get_plugin_source_url() {
453
plugin_config="$(asdf_data_dir)/repository/plugins/$plugin_name"
455
if [ -f "$plugin_config" ]; then
456
grep "repository" "$plugin_config" | awk -F'=' '{print $2}' | sed 's/ //'
460
find_tool_versions() {
461
find_file_upwards "$(asdf_tool_versions_filename)"
468
while [ "$search_path" != "/" ]; do
469
if [ -f "$search_path/$name" ]; then
470
util_validate_no_carriage_returns "$search_path/$name"
472
printf "%s\n" "${search_path}/$name"
475
search_path=$(dirname "$search_path")
486
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|')
491
case $resolved_path in
493
printf "%s\n" "$resolved_path"
497
cd "$(dirname "$symlink")" || exit 1
498
printf "%s\n" "$PWD/$resolved_path"
504
list_plugin_bin_paths() {
507
local install_type=$3
509
plugin_path=$(get_plugin_path "$plugin_name")
511
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
513
if [ -f "${plugin_path}/bin/list-bin-paths" ]; then
514
local space_separated_list_of_bin_paths
517
space_separated_list_of_bin_paths=$(
518
export ASDF_INSTALL_TYPE=$install_type
519
export ASDF_INSTALL_VERSION=$version
520
export ASDF_INSTALL_PATH=$install_path
521
"${plugin_path}/bin/list-bin-paths"
524
local space_separated_list_of_bin_paths="bin"
526
printf "%s\n" "$space_separated_list_of_bin_paths"
529
list_plugin_exec_paths() {
531
local full_version=$2
532
check_if_plugin_exists "$plugin_name"
534
IFS=':' read -r -a version_info <<<"$full_version"
535
if [ "${version_info[0]}" = "ref" ]; then
536
local install_type="${version_info[0]}"
537
local version="${version_info[1]}"
538
elif [ "${version_info[0]}" = "path" ]; then
539
local install_type="${version_info[0]}"
540
local version="${version_info[1]}"
542
local install_type="version"
543
local version="${version_info[0]}"
546
local plugin_shims_path
547
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
548
if [ -d "$plugin_shims_path" ]; then
549
printf "%s\n" "$plugin_shims_path"
552
space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")"
553
IFS=' ' read -r -a all_bin_paths <<<"$space_separated_list_of_bin_paths"
556
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
558
for bin_path in "${all_bin_paths[@]}"; do
559
printf "%s\n" "$install_path/$bin_path"
565
local full_version=$2
568
IFS=':' read -r -a version_info <<<"$full_version"
569
if [ "${version_info[0]}" = "ref" ]; then
570
local install_type="${version_info[0]}"
571
local version="${version_info[1]}"
573
local install_type="version"
574
local version="${version_info[0]}"
577
if [ "$version" = "system" ]; then
584
plugin_path=$(get_plugin_path "$plugin_name")
587
local path exec_paths
588
exec_paths="$(list_plugin_exec_paths "$plugin_name" "$full_version")"
592
path="$(tr '\n' ':' <<<"$exec_paths")$PATH"
595
if [ ! -f "${plugin_path}/bin/exec-env" ]; then
596
PATH=$path "$callback"
602
install_path=$(find_install_path "$plugin_name" "$full_version")
605
ASDF_INSTALL_TYPE=$install_type \
606
ASDF_INSTALL_VERSION=$version \
607
ASDF_INSTALL_PATH=$install_path \
608
. "${plugin_path}/bin/exec-env"
610
PATH=$path "$callback"
613
plugin_executables() {
615
local full_version=$2
617
IFS=$'\n' read -rd '' -a all_bin_paths <<<"$(list_plugin_exec_paths "$plugin_name" "$full_version")"
618
for bin_path in "${all_bin_paths[@]}"; do
619
for executable_file in "$bin_path"/*; do
620
if is_executable "$executable_file"; then
621
printf "%s\n" "$executable_file"
628
local executable_path=$1
629
if [[ (-f "$executable_path") && (-x "$executable_path") ]]; then
637
local full_version=$2
638
grep -lx "# asdf-plugin: $plugin_name $full_version" "$(asdf_data_dir)/shims"/* 2>/dev/null
641
shim_plugin_versions() {
642
local executable_name
643
executable_name=$(basename "$1")
645
shim_path="$(asdf_data_dir)/shims/${executable_name}"
646
if [ -x "$shim_path" ]; then
647
grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq
649
printf "asdf: unknown shim %s\n" "$executable_name"
655
local executable_name
656
executable_name=$(basename "$1")
658
shim_path="$(asdf_data_dir)/shims/${executable_name}"
659
if [ -x "$shim_path" ]; then
660
grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | cut -d' ' -f 1 | uniq
662
printf "asdf: unknown shim %s\n" "$executable_name"
667
strip_tool_version_comments() {
668
local tool_version_path="$1"
676
sed '/^[[:blank:]]*#/d;s/#.*//;s/[[:blank:]]*$//' "$tool_version_path"
682
hook_cmd="$(get_asdf_config_value "$hook_name")"
683
if [ -n "$hook_cmd" ]; then
688
asdf_hook_fun "${@:2}"
694
shim_plugin_versions "${shim_name}"
695
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}'
700
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\n\" PLUGIN \$(get_preset_version_for PLUGIN)"
703
select_from_preset_version() {
706
local preset_versions
708
shim_versions=$(get_shim_versions "$shim_name")
709
if [ -n "$shim_versions" ]; then
710
preset_versions=$(preset_versions "$shim_name")
711
grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\n" VERSION
726
IFS=$'\n' read -rd '' -a shim_versions <<<"$(get_shim_versions "$shim_name")"
729
IFS=$'\n' read -rd '' -a plugins <<<"$(shim_plugins "$shim_name")"
731
for plugin_name in "${plugins[@]}"; do
732
local version_and_path
734
local usable_plugin_versions
736
version_and_path=$(find_versions "$plugin_name" "$search_path")
737
IFS='|' read -r version_string _path <<<"$version_and_path"
738
IFS=' ' read -r -a usable_plugin_versions <<<"$version_string"
739
for plugin_version in "${usable_plugin_versions[@]}"; do
740
for plugin_and_version in "${shim_versions[@]}"; do
741
local plugin_shim_name
742
local plugin_shim_version
743
IFS=' ' read -r plugin_shim_name plugin_shim_version <<<"$plugin_and_version"
744
if [[ "$plugin_name" == "$plugin_shim_name" ]]; then
745
if [[ "$plugin_version" == "$plugin_shim_version" ]]; then
746
printf "%s\n" "$plugin_name $plugin_version"
748
elif [[ "$plugin_version" == "path:"* ]]; then
749
printf "%s\n" "$plugin_name $plugin_version"
758
with_shim_executable() {
760
shim_name=$(basename "$1")
761
local shim_exec="${2}"
763
if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then
764
printf "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2
768
local selected_version
769
selected_version="$(select_version "$shim_name")"
771
if [ -z "$selected_version" ]; then
772
selected_version="$(select_from_preset_version "$shim_name")"
775
if [ -n "$selected_version" ]; then
780
IFS=' ' read -r plugin_name full_version <<<"$selected_version"
781
plugin_path=$(get_plugin_path "$plugin_name")
787
path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims")
789
executable_path=$(PATH=$path command -v "$shim_name")
791
if [ -x "${plugin_path}/bin/exec-path" ]; then
792
install_path=$(find_install_path "$plugin_name" "$full_version")
793
executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path:-${shim_name}}")
796
"$shim_exec" "$plugin_name" "$full_version" "$executable_path"
799
with_plugin_env "$plugin_name" "$full_version" run_within_env
804
local preset_plugin_versions
805
preset_plugin_versions=()
806
local closest_tool_version
807
closest_tool_version=$(find_tool_versions)
810
IFS=$'\n' read -rd '' -a shim_plugins <<<"$(shim_plugins "$shim_name")"
811
for shim_plugin in "${shim_plugins[@]}"; do
814
version_string=$(get_preset_version_for "$shim_plugin")
815
IFS=' ' read -r -a shim_versions <<<"$version_string"
816
local usable_plugin_versions
817
for shim_version in "${shim_versions[@]}"; do
818
preset_plugin_versions+=("$shim_plugin $shim_version")
822
if [ -n "${preset_plugin_versions[*]}" ]; then
823
printf "%s %s\n" "No preset version installed for command" "$shim_name"
824
printf "%s\n\n" "Please install a version by running one of the following:"
825
for preset_plugin_version in "${preset_plugin_versions[@]}"; do
826
printf "%s %s\n" "asdf install" "$preset_plugin_version"
828
printf "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version"
830
printf "%s %s\n" "No version is set for command" "$shim_name"
831
printf "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version"
833
shim_plugin_versions "${shim_name}"
845
printf "%s" "${input//"$find_str"/"$replace"}"
848
remove_path_from_path() {
853
substitute "$PATH" "$path" "" | sed -e "s|::|:|g"
858
util_resolve_user_path() {
859
util_resolve_user_path_reply=
863
if [ "${path::2}" = '~/' ]; then
864
util_resolve_user_path_reply="${HOME}/${path:2}"
866
util_resolve_user_path_reply="$path"
871
util_validate_no_carriage_returns() {
874
if grep -qr $'\r' "$file_path"; then
875
printf '%s\n' "asdf: Warning: File $file_path contains carriage returns. Please remove them." >&2
879
get_plugin_remote_url() {
880
local plugin_name="$1"
882
plugin_path="$(get_plugin_path "$plugin_name")"
883
git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null
886
get_plugin_remote_branch() {
887
local plugin_name="$1"
889
plugin_path="$(get_plugin_path "$plugin_name")"
890
git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null
893
get_plugin_remote_gitref() {
894
local plugin_name="$1"
896
plugin_path="$(get_plugin_path "$plugin_name")"
897
git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null