[Refactor] Use awk for path stripping

This works with paths which contains regex meta characters.
Kaito Udagawa 2016-12-23 01:03:41 +09:00 committed by Jordan Harband
parent 07c02d43fe
commit 53f9ba8336
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
2 changed files with 13 additions and 7 deletions

13
nvm.sh
View File

@ -698,13 +698,12 @@ nvm_strip_path() {
nvm_err '${NVM_DIR} not set!' nvm_err '${NVM_DIR} not set!'
return 1 return 1
fi fi
nvm_echo "${1-}" | command sed \ command printf %s "${1-}" | command awk -v NVM_DIR="${NVM_DIR}" -v RS=: '
-e "s#${NVM_DIR}/[^/]*${2-}[^:]*:##g" \ index($0, NVM_DIR) == 1 {
-e "s#:${NVM_DIR}/[^/]*${2-}[^:]*##g" \ path = substr($0, length(NVM_DIR) + 1)
-e "s#${NVM_DIR}/[^/]*${2-}[^:]*##g" \ if (path ~ "^(/versions/[^/]*)?/[^/]*'"${2-}"'.*$") { next }
-e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*:##g" \ }
-e "s#:${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*##g" \ { print }' | command paste -s -d: -
-e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*##g"
} }
nvm_change_path() { nvm_change_path() {

View File

@ -9,3 +9,10 @@ TEST_PATH=$NVM_DIR/v0.10.5/bin:/usr/bin:$NVM_DIR/v0.11.5/bin:$NVM_DIR/v0.9.5/bin
STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"` STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"`
[ "$STRIPPED_PATH" = "/usr/bin:/usr/local/bin" ] || die "Not correctly stripped: $STRIPPED_PATH " [ "$STRIPPED_PATH" = "/usr/bin:/usr/local/bin" ] || die "Not correctly stripped: $STRIPPED_PATH "
NVM_DIR='/#*.^$[]'
TEST_PATH="$NVM_DIR/v0.10.5/bin:/usr/bin:$NVM_DIR/v0.11.5/bin:$NVM_DIR/v0.9.5/bin:/usr/local/bin:$NVM_DIR/v0.2.5/bin:$NVM_DIR/versions/node/v0.12.0/bin:$NVM_DIR/versions/io.js/v1.0.0/bin"
STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"`
[ "$STRIPPED_PATH" = "/usr/bin:/usr/local/bin" ] || die "Not correctly stripped: $STRIPPED_PATH "