Skip to content

Commit b057b56

Browse files
author
edwmurph
committed
fix zsh incompatibility
1 parent 7c888aa commit b057b56

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

nvm.sh

+48-49
Original file line numberDiff line numberDiff line change
@@ -341,54 +341,53 @@ nvm_normalize_semver() {
341341
if [ -z "$semver" ]; then
342342
return 1
343343
fi
344+
local comparator_set
345+
local validated_comparator_set
344346
local validated_semver
345347
validated_semver='';
346348
while [ -n "$semver" ]; do
347-
local comparator_set
348349
comparator_set=$(command printf "%s" "$semver" | command head -n1)
349350
semver=$(command printf "%s" "$semver" | command tail -n +2)
350351
[ -n "$comparator_set" ] || continue
351352

352353
# convert comparators into required grammar
353-
local validated_comparator_set
354-
validated_comparator_set=$(command printf " %s " "$comparator_set" \
355-
| command sed -E "
356-
# exactly 1 space is needed before and after every comparator (including the first and last comparators)
357-
s/\011/ /g;s/ +/ /g;
358-
359-
# normalize all wildcards to x
360-
s/X|\*/x/g;
361-
362-
# space out numbers surrounding '-'
363-
s/ ?- ?/ - /g;
364-
365-
# ' 1 ' => ' 1.x.x '
366-
# ' x ' => ' x.x.x '
367-
s/ ([0-9]+|x) / \1.x.x /g;
368-
369-
# ' 1.2 ' => ' 1.2.x '
370-
# ' 1.x ' => ' 1.x.x '
371-
# ' x.x ' => ' x.x.x '
372-
s/ (([0-9]+|x)\.([0-9]+|x)) / \1.x /g;
373-
374-
# ' 1.2.3 - 1.2.4 ' => ' >=1.2.3 <=1.2.4 '
375-
s/ (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) ?\- ?(([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / >=\1 <=\5 /g;
376-
377-
# ' > 1.2.3 ' => ' >1.2.3 '
378-
# ' < 1.2.5 ' => ' <1.2.5 '
379-
# ' <= 1.2.3 ' => ' <=1.2.3 '
380-
# ' >= 1.2.3 ' => ' >=1.2.3 '
381-
# ' = 1.2.3 ' => ' =1.2.3 '
382-
# ' ~ 1.2.3 ' => ' ~1.2.3 '
383-
# ' ^ 1.2.3 ' => ' ^1.2.3 '
384-
# ' v 1.2.3 ' => ' v1.2.3 '
385-
s/ (v|<|>|<=|>=|=|~|\^) (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / \1\2 /g;
386-
387-
# ' =1.2.3 ' => ' 1.2.3 '
388-
# ' v1.2.3 ' => ' 1.2.3 '
389-
s/ (=|v)//g;
390-
" \
391-
| command awk '{
354+
validated_comparator_set=$(command printf " %s " "$comparator_set" |
355+
command sed -E "
356+
# exactly 1 space is needed before and after every comparator (including the first and last comparators)
357+
s/\011/ /g;s/ +/ /g;
358+
359+
# normalize all wildcards to x
360+
s/X|\*/x/g;
361+
362+
# space out numbers surrounding '-'
363+
s/ ?- ?/ - /g;
364+
365+
# ' 1 ' => ' 1.x.x '
366+
# ' x ' => ' x.x.x '
367+
s/ ([0-9]+|x) / \1.x.x /g;
368+
369+
# ' 1.2 ' => ' 1.2.x '
370+
# ' 1.x ' => ' 1.x.x '
371+
# ' x.x ' => ' x.x.x '
372+
s/ (([0-9]+|x)\.([0-9]+|x)) / \1.x /g;
373+
374+
# ' 1.2.3 - 1.2.4 ' => ' >=1.2.3 <=1.2.4 '
375+
s/ (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) ?\- ?(([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / >=\1 <=\5 /g;
376+
377+
# ' > 1.2.3 ' => ' >1.2.3 '
378+
# ' < 1.2.5 ' => ' <1.2.5 '
379+
# ' <= 1.2.3 ' => ' <=1.2.3 '
380+
# ' >= 1.2.3 ' => ' >=1.2.3 '
381+
# ' = 1.2.3 ' => ' =1.2.3 '
382+
# ' ~ 1.2.3 ' => ' ~1.2.3 '
383+
# ' ^ 1.2.3 ' => ' ^1.2.3 '
384+
# ' v 1.2.3 ' => ' v1.2.3 '
385+
s/ (v|<|>|<=|>=|=|~|\^) (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / \1\2 /g;
386+
387+
# ' =1.2.3 ' => ' 1.2.3 '
388+
# ' v1.2.3 ' => ' 1.2.3 '
389+
s/ (=|v)//g;" |
390+
command awk '{
392391
# handle conversions of comparators with ^ or ~ or x into required grammar
393392
# ` ^0.0.1 ` => ` >=0.0.1 <0.0.2 `
394393
# ` ^0.1.2 ` => ` >=0.1.2 <0.2.0 `
@@ -462,8 +461,8 @@ nvm_normalize_semver() {
462461
}
463462
}
464463
print output
465-
}' \
466-
| command sed -E 's/^ +//;s/ +$//'
464+
}' |
465+
command sed -E 's/^ +//;s/ +$//'
467466
)
468467

469468
# only comparator_sets composed of the required grammar are marked as valid
@@ -500,13 +499,17 @@ nvm_interpret_complex_semver() {
500499
# - Add the discovered newest compatible version to highest_compatible_versions.
501500
# - Choose the highest version among all the versions collected in highest_compatible_versions.
502501
semver=$(command printf "%s" "$semver" | command tr '||' '\n')
502+
local version_list_copy
503+
local current_comparator_set
504+
local current_version
505+
local current_comparator_set_copy
506+
local current_comparator
507+
local stripped_version_from_comparator
503508
local highest_compatible_versions
504509
highest_compatible_versions=''
505510

506511
while [ -n "$semver" ]; do
507-
local version_list_copy
508512
version_list_copy=$(command printf "%s" "$version_list")
509-
local current_comparator_set
510513
current_comparator_set=$(command printf "%s" "$semver" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
511514
semver=$(command printf "%s" "$semver" | command tail -n +2)
512515
[ -n "$current_comparator_set" ] || continue
@@ -515,22 +518,18 @@ nvm_interpret_complex_semver() {
515518
# - If current_version satisfies all comparators in current_comparator_set, we've found the newest version compatible with all comparators in current current_comparator_set.
516519
# - Add discovered version to highest_compatible_versions and stop iterating through versions for current_comparator_set.
517520
while [ -n "$version_list_copy" ]; do
518-
local current_version
519521
current_version=$(command printf "%s" "$version_list_copy" | command tail -n1 | command sed -E 's/^ +//;s/ +$//' | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
520522
version_list_copy=$(command printf "%s" "$version_list_copy" | command sed '$d')
521523
[ -n "$current_version" ] || continue
522524

523525
# For each comparator in the current_comparator_set_copy:
524526
# - If current_version is compatible with all comparators, we know current_version is the newest compatible version
525-
local current_comparator_set_copy
526527
current_comparator_set_copy=$(command printf "%s" "$current_comparator_set" | command tr ' ' '\n')
527528
while [ -n "$current_comparator_set_copy" ]; do
528-
local current_comparator
529529
current_comparator=$(command printf "%s" "$current_comparator_set_copy" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
530530
current_comparator_set_copy=$(command printf "%s" "$current_comparator_set_copy" | command tail -n +2)
531531
[ -n "$current_comparator" ] || continue
532532

533-
local stripped_version_from_comparator
534533
stripped_version_from_comparator=$(command printf "%s" "$current_comparator" | nvm_grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+$')
535534
if [ -z "$stripped_version_from_comparator" ]; then
536535
return 1
@@ -618,10 +617,10 @@ nvm_interpret_complex_semver() {
618617
# Since comparator sets are separated by '||', choosing any of the highest versions compatible with any of the comparator_sets would be compatible with the whole semver.
619618
# Therefore, we should resolve to the highest version in highest_compatible_versions.
620619
local highest_compatible_version
620+
local compatible_node_version
621621
highest_compatible_version='0.0.0'
622622
highest_compatible_versions=$(command printf "%s" "$highest_compatible_versions" | command tr ' ' '\n')
623623
while [ -n "$highest_compatible_versions" ]; do
624-
local compatible_node_version
625624
compatible_node_version=$(command printf "%s" "$highest_compatible_versions" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
626625
highest_compatible_versions=$(command printf "%s" "$highest_compatible_versions" | command tail -n +2)
627626
[ -n "$compatible_node_version" ] || continue

test/fast/Unit tests/nvm_normalize_semver

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ while [ -n "$test_cases" ]; do
4848
expected_output=$(echo "$line" | awk -F: '{ print $2 }')
4949
actual_output=$(nvm_normalize_semver "$input")
5050
if [ -z "$input" ] || [ "$actual_output" != "$expected_output" ]; then
51-
die "nvm_validate_semver test case failed. Expected output: '$expected_output'. Actual output: '$actual_output'. Input: '$input'.\n"
51+
die "nvm_normalize_semver test case failed. Expected output: '$expected_output'. Actual output: '$actual_output'. Input: '$input'.\n"
5252
fi
5353
test_cases=$(echo "$test_cases" | tail -n +2)
5454
done

0 commit comments

Comments
 (0)