forked from asdf-vm/asdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
720 lines (599 loc) · 18.9 KB
/
utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# 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
GREP_OPTIONS="--color=never"
# shellcheck disable=SC2034
GREP_COLORS=
ASDF_DIR=${ASDF_DIR:-''}
asdf_version() {
local version git_rev
version="$(cat "$(asdf_dir)/VERSION")"
if [ -d "$(asdf_dir)/.git" ]; then
git_rev="$(git --git-dir "$(asdf_dir)/.git" rev-parse --short HEAD)"
echo "${version}-${git_rev}"
else
echo "${version}"
fi
}
asdf_dir() {
if [ -z "$ASDF_DIR" ]; then
local current_script_path=${BASH_SOURCE[0]}
export ASDF_DIR
ASDF_DIR=$(
cd "$(dirname "$(dirname "$current_script_path")")" || exit
pwd
)
fi
echo "$ASDF_DIR"
}
asdf_repository_url() {
echo "https://github.com/asdf-vm/asdf-plugins.git"
}
asdf_data_dir() {
local data_dir
if [ -n "${ASDF_DATA_DIR}" ]; then
data_dir="${ASDF_DATA_DIR}"
elif [[ $EUID -ne 0 ]]; then
data_dir="$HOME/.asdf"
else
data_dir="$(asdf_dir)"
fi
echo "$data_dir"
}
get_install_path() {
local plugin=$1
local install_type=$2
local version=$3
local install_dir
install_dir="$(asdf_data_dir)/installs"
mkdir -p "${install_dir}/${plugin}"
if [ "$install_type" = "version" ]; then
echo "${install_dir}/${plugin}/${version}"
else
echo "${install_dir}/${plugin}/${install_type}-${version}"
fi
}
list_installed_versions() {
local plugin_name=$1
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local plugin_installs_path
plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}"
if [ -d "$plugin_installs_path" ]; then
for install in "${plugin_installs_path}"/*/; do
[[ -e "$install" ]] || break
basename "$install" | sed 's/^ref-/ref:/'
done
fi
}
check_if_plugin_exists() {
local plugin_name=$1
# Check if we have a non-empty argument
if [ -z "${1}" ]; then
display_error "No plugin given"
exit 1
fi
if [ ! -d "$(asdf_data_dir)/plugins/$plugin_name" ]; then
display_error "No such plugin: $plugin_name"
exit 1
fi
}
check_if_version_exists() {
local plugin_name=$1
local version=$2
check_if_plugin_exists "$plugin_name"
local install_path
install_path=$(find_install_path "$plugin_name" "$version")
if [ "$version" != "system" ] && [ ! -d "$install_path" ]; then
display_error "version $version is not installed for $plugin_name"
exit 1
fi
}
get_plugin_path() {
if test -n "$1"; then
echo "$(asdf_data_dir)/plugins/$1"
else
echo "$(asdf_data_dir)/plugins"
fi
}
display_error() {
echo >&2 "$1"
}
get_version_in_dir() {
local plugin_name=$1
local search_path=$2
local legacy_filenames=$3
local asdf_version
asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" "$plugin_name")
if [ -n "$asdf_version" ]; then
echo "$asdf_version|$search_path/.tool-versions"
return 0
fi
for filename in $legacy_filenames; do
local legacy_version
legacy_version=$(parse_legacy_version_file "$search_path/$filename" "$plugin_name")
if [ -n "$legacy_version" ]; then
echo "$legacy_version|$search_path/$filename"
return 0
fi
done
}
find_versions() {
local plugin_name=$1
local search_path=$2
local version
version=$(get_version_from_env "$plugin_name")
if [ -n "$version" ]; then
local upcase_name
upcase_name=$(echo "$plugin_name" | tr '[:lower:]-' '[:upper:]_')
local version_env_var="ASDF_${upcase_name}_VERSION"
echo "$version|$version_env_var environment variable"
return 0
fi
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local legacy_config
legacy_config=$(get_asdf_config_value "legacy_version_file")
local legacy_list_filenames_script
legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames"
local legacy_filenames=""
if [ "$legacy_config" = "yes" ] && [ -f "$legacy_list_filenames_script" ]; then
legacy_filenames=$(bash "$legacy_list_filenames_script")
fi
while [ "$search_path" != "/" ]; do
version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames")
if [ -n "$version" ]; then
echo "$version"
return 0
fi
search_path=$(dirname "$search_path")
done
get_version_in_dir "$plugin_name" "$HOME" "$legacy_filenames"
if [ -f "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" ]; then
versions=$(parse_asdf_version_file "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$plugin_name")
if [ -n "$versions" ]; then
echo "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME"
return 0
fi
fi
}
display_no_version_set() {
local plugin_name=$1
echo "No version set for ${plugin_name}; please run \`asdf <global | local> ${plugin_name} <version>\`"
}
get_version_from_env() {
local plugin_name=$1
local upcase_name
upcase_name=$(echo "$plugin_name" | tr '[:lower:]-' '[:upper:]_')
local version_env_var="ASDF_${upcase_name}_VERSION"
local version=${!version_env_var}
echo "$version"
}
find_install_path() {
local plugin_name=$1
local version=$2
# shellcheck disable=SC2162
IFS=':' read -a version_info <<<"$version"
if [ "$version" = "system" ]; then
echo ""
elif [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
get_install_path "$plugin_name" "$install_type" "$version"
elif [ "${version_info[0]}" = "path" ]; then
# 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
local install_type="path"
local version="path"
echo "${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
get_install_path "$plugin_name" "$install_type" "$version"
fi
}
get_custom_executable_path() {
local plugin_path=$1
local install_path=$2
local executable_path=$3
# custom plugin hook for executable path
if [ -x "${plugin_path}/bin/exec-path" ]; then
cmd=$(basename "$executable_path")
local relative_path
# shellcheck disable=SC2001
relative_path=$(echo "$executable_path" | sed -e "s|${install_path}/||")
relative_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$relative_path")"
executable_path="$install_path/$relative_path"
fi
echo "$executable_path"
}
get_executable_path() {
local plugin_name=$1
local version=$2
local executable_path=$3
check_if_version_exists "$plugin_name" "$version"
if [ "$version" = "system" ]; then
path=$(echo "$PATH" | sed -e "s|$(asdf_data_dir)/shims||g; s|::|:|g")
cmd=$(basename "$executable_path")
cmd_path=$(PATH=$path command -v "$cmd" 2>&1)
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
return 1
fi
echo "$cmd_path"
else
local install_path
install_path=$(find_install_path "$plugin_name" "$version")
echo "${install_path}"/"${executable_path}"
fi
}
parse_asdf_version_file() {
local file_path=$1
local plugin_name=$2
if [ -f "$file_path" ]; then
local version
version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //")
if [ -n "$version" ]; then
echo "$version"
return 0
fi
fi
}
parse_legacy_version_file() {
local file_path=$1
local plugin_name=$2
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local parse_legacy_script
parse_legacy_script="${plugin_path}/bin/parse-legacy-file"
if [ -f "$file_path" ]; then
if [ -f "$parse_legacy_script" ]; then
bash "$parse_legacy_script" "$file_path"
else
cat "$file_path"
fi
fi
}
get_preset_version_for() {
local plugin_name=$1
local search_path
search_path=$(pwd)
local version_and_path
version_and_path=$(find_versions "$plugin_name" "$search_path")
local version
version=$(cut -d '|' -f 1 <<<"$version_and_path")
echo "$version"
}
get_asdf_config_value_from_file() {
local config_path=$1
local key=$2
if [ ! -f "$config_path" ]; then
return 1
fi
local result
result=$(grep -E "^\\s*$key\\s*=\\s*" "$config_path" | head | awk -F '=' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$result" ]; then
echo "$result"
return 0
fi
return 2
}
get_asdf_config_value() {
local key=$1
local config_path=${ASDF_CONFIG_FILE:-"$HOME/.asdfrc"}
local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
local local_config_path
local_config_path="$(find_file_upwards ".asdfrc")"
get_asdf_config_value_from_file "$local_config_path" "$key" ||
get_asdf_config_value_from_file "$config_path" "$key" ||
get_asdf_config_value_from_file "$default_config_path" "$key"
}
repository_needs_update() {
local update_file_dir
update_file_dir="$(asdf_data_dir)/tmp"
local update_file_name
update_file_name="repo-updated"
# `find` outputs filename if it has not been modified in the last day
local find_result
find_result=$(find "$update_file_dir" -name "$update_file_name" -type f -mtime +1 -print)
[ -n "$find_result" ]
}
initialize_or_update_repository() {
local repository_url
local repository_path
repository_url=$(asdf_repository_url)
repository_path=$(asdf_data_dir)/repository
if [ ! -d "$repository_path" ]; then
echo "initializing plugin repository..."
git clone "$repository_url" "$repository_path"
elif repository_needs_update; then
echo "updating plugin repository..."
(cd "$repository_path" && git fetch && git reset --hard origin/master)
fi
mkdir -p "$(asdf_data_dir)/tmp"
touch "$(asdf_data_dir)/tmp/repo-updated"
}
get_plugin_source_url() {
local plugin_name=$1
local plugin_config
plugin_config="$(asdf_data_dir)/repository/plugins/$plugin_name"
if [ -f "$plugin_config" ]; then
grep "repository" "$plugin_config" | awk -F'=' '{print $2}' | sed 's/ //'
fi
}
find_tool_versions() {
find_file_upwards ".tool-versions"
}
find_file_upwards() {
local name="$1"
local search_path
search_path=$(pwd)
while [ "$search_path" != "/" ]; do
if [ -f "$search_path/$name" ]; then
echo "${search_path}/$name"
return 0
fi
search_path=$(dirname "$search_path")
done
}
resolve_symlink() {
local symlink
symlink="$1"
# This seems to be the only cross-platform way to resolve symlink paths to
# the real file path.
# shellcheck disable=SC2012
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|')
# Check if resolved path is relative or not by looking at the first character.
# If it is a slash we can assume it's root and absolute. Otherwise we treat it
# as relative
case $resolved_path in
/*)
echo "$resolved_path"
;;
*)
echo "$PWD/$resolved_path"
;;
esac
}
list_plugin_bin_paths() {
local plugin_name=$1
local version=$2
local install_type=$3
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
if [ -f "${plugin_path}/bin/list-bin-paths" ]; then
local space_separated_list_of_bin_paths
# shellcheck disable=SC2030
space_separated_list_of_bin_paths=$(
export ASDF_INSTALL_TYPE=$install_type
export ASDF_INSTALL_VERSION=$version
export ASDF_INSTALL_PATH=$install_path
bash "${plugin_path}/bin/list-bin-paths"
)
else
local space_separated_list_of_bin_paths="bin"
fi
echo "$space_separated_list_of_bin_paths"
}
list_plugin_exec_paths() {
local plugin_name=$1
local full_version=$2
check_if_plugin_exists "$plugin_name"
IFS=':' read -r -a version_info <<<"$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
local plugin_shims_path
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
if [ -d "$plugin_shims_path" ]; then
echo "$plugin_shims_path"
fi
space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")"
IFS=' ' read -r -a all_bin_paths <<<"$space_separated_list_of_bin_paths"
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
for bin_path in "${all_bin_paths[@]}"; do
echo "$install_path/$bin_path"
done
}
with_plugin_env() {
local plugin_name=$1
local full_version=$2
local callback=$3
IFS=':' read -r -a version_info <<<"$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
if [ "$version" = "system" ]; then
# execute as is for system
"$callback"
return $?
fi
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
# add the plugin listed exec paths to PATH
local path exec_paths
exec_paths="$(list_plugin_exec_paths "$plugin_name" "$full_version")"
# exec_paths contains a trailing newline which is converted to a colon, so no
# colon is needed between the subshell and the PATH variable in this string
path="$(echo "$exec_paths" | tr '\n' ':')$PATH"
# If no custom exec-env transform, just execute callback
if [ ! -f "${plugin_path}/bin/exec-env" ]; then
PATH=$path "$callback"
return $?
fi
# Load the plugin custom environment
local install_path
install_path=$(find_install_path "$plugin_name" "$full_version")
# shellcheck source=/dev/null
ASDF_INSTALL_TYPE=$install_type \
ASDF_INSTALL_VERSION=$version \
ASDF_INSTALL_PATH=$install_path \
source "${plugin_path}/bin/exec-env"
PATH=$path "$callback"
}
plugin_executables() {
local plugin_name=$1
local full_version=$2
for bin_path in $(list_plugin_exec_paths "$plugin_name" "$full_version"); do
for executable_file in "$bin_path"/*; do
if is_executable "$executable_file"; then
echo "$executable_file"
fi
done
done
}
is_executable() {
local executable_path=$1
if [[ (-f "$executable_path") && (-x "$executable_path") ]]; then
return 0
fi
return 1
}
plugin_shims() {
local plugin_name=$1
local full_version=$2
grep -lx "# asdf-plugin: $plugin_name $full_version" "$(asdf_data_dir)/shims"/* 2>/dev/null
}
shim_plugin_versions() {
local executable_name
executable_name=$(basename "$1")
local shim_path
shim_path="$(asdf_data_dir)/shims/${executable_name}"
if [ -x "$shim_path" ]; then
grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq
else
echo "asdf: unknown shim $executable_name"
return 1
fi
}
strip_tool_version_comments() {
local tool_version_path="$1"
while IFS= read -r tool_line || [ -n "$tool_line" ]; do
# Remove whitespace before pound sign, the pound sign, and everything after it
new_line="$(echo "$tool_line" | cut -f1 -d"#" | sed -e 's/[[:space:]]*$//')"
# Only print the line if it is not empty
if [[ -n "$new_line" ]]; then
echo "$new_line"
fi
done <"$tool_version_path"
}
asdf_run_hook() {
local hook_name=$1
local hook_cmd
hook_cmd="$(get_asdf_config_value "$hook_name")"
if [ -n "$hook_cmd" ]; then
asdf_hook_fun() {
unset asdf_hook_fun
ev'al' "$hook_cmd" # ignore banned command just here
}
asdf_hook_fun "${@:2}"
fi
}
with_shim_executable() {
get_shim_versions() {
shim_plugin_versions "${shim_name}"
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}'
}
preset_versions() {
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c "source $(asdf_dir)/lib/utils.sh; echo PLUGIN \$(get_preset_version_for PLUGIN)"
}
select_from_preset_version() {
grep -f <(get_shim_versions) <(preset_versions) | head -n 1 | xargs echo
}
select_version() {
# First, we get the all the plugins where the
# current shim is available.
# Then, we iterate on all versions set for each plugin
# Note that multiple plugin versions can be set for a single plugin.
# These are separated by a space. e.g. python 3.7.2 2.7.15
# For each plugin/version pair, we check if it is present in the shim
local search_path
search_path=$(pwd)
local shim_versions
IFS=$'\n' read -rd '' -a shim_versions <<<"$(get_shim_versions)"
local plugins
plugins=()
for plugin_and_version in "${shim_versions[@]}"; do
local plugin_name
local plugin_shim_version
IFS=' ' read -r plugin_name _plugin_shim_version <<<"$plugin_and_version"
if ! [[ " ${plugins[*]} " == *" $plugin_name "* ]]; then
plugins+=("$plugin_name")
fi
done
for plugin_name in "${plugins[@]}"; do
local version_and_path
local version_string
local usable_plugin_versions
local _path
version_and_path=$(find_versions "$plugin_name" "$search_path")
IFS='|' read -r version_string _path <<<"$version_and_path"
IFS=' ' read -r -a usable_plugin_versions <<<"$version_string"
for plugin_version in "${usable_plugin_versions[@]}"; do
for plugin_and_version in "${shim_versions[@]}"; do
local plugin_shim_name
local plugin_shim_version
IFS=' ' read -r plugin_shim_name plugin_shim_version <<<"$plugin_and_version"
if [[ "$plugin_name" == "$plugin_shim_name" ]] &&
[[ "$plugin_version" == "$plugin_shim_version" ]]; then
echo "$plugin_name $plugin_version"
return
fi
done
done
done
}
local shim_name
shim_name=$(basename "$1")
local shim_exec="${2}"
if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then
echo "unknown command: ${shim_name}. Perhaps you have to reshim?" >&2
return 1
fi
local selected_version
selected_version="$(select_version)"
if [ -z "$selected_version" ]; then
selected_version=$(select_from_preset_version)
fi
if [ -n "$selected_version" ]; then
local plugin_name
local full_version
local plugin_path
IFS=' ' read -r plugin_name full_version <<<"$selected_version"
plugin_path=$(get_plugin_path "$plugin_name")
run_within_env() {
local path=$PATH
if [ "system" == "$full_version" ]; then
path=$(echo "$PATH" | sed -e "s|$(asdf_data_dir)/shims||g; s|::|:|g")
fi
executable_path=$(PATH=$path command -v "$shim_name")
if [ -x "${plugin_path}/bin/exec-path" ]; then
install_path=$(find_install_path "$plugin_name" "$full_version")
executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path:-${shim_name}}")
fi
"$shim_exec" "$plugin_name" "$full_version" "$executable_path"
}
with_plugin_env "$plugin_name" "$full_version" run_within_env
return $?
fi
(
echo "asdf: No version set for command ${shim_name}"
echo "you might want to add one of the following in your .tool-versions file:"
echo ""
shim_plugin_versions "${shim_name}"
) >&2
return 126
}