This commit is contained in:
Jason Walton 2013-07-05 18:27:12 -07:00
commit 275ea0da8c
1 changed files with 56 additions and 17 deletions

73
nvm.sh
View File

@ -170,7 +170,8 @@ nvm() {
echo echo
echo "Usage:" echo "Usage:"
echo " nvm help Show this message" echo " nvm help Show this message"
echo " nvm install [-s] <version> Download and install a <version>" echo " nvm install [-s] [-b binary_tarball_url] [-u source_tarball_url] <version>"
echo " Download and install a <version>"
echo " nvm uninstall <version> Uninstall a version" echo " nvm uninstall <version> Uninstall a version"
echo " nvm use <version> Modify PATH to use <version>" echo " nvm use <version> Modify PATH to use <version>"
echo " nvm run <version> [<args>] Run <version> with <args> as arguments" echo " nvm run <version> [<args>] Run <version> with <args> as arguments"
@ -216,14 +217,31 @@ nvm() {
shift shift
# Initialize our own variables:
binary_url=""
source_url=""
nobinary=0 nobinary=0
if [ "$1" = "-s" ]; then
nobinary=1 OPTIND=1
shift while getopts "sb:u:" opt; do
fi case "$opt" in
s) nobinary=1
;;
b) binary_url=$OPTARG
;;
u) source_url=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
nobinary=1 nobinary=1
fi
if [ -z $binary_url ] && [ ! -z $source_url ]; then
nobinary=1
fi fi
VERSION=`nvm_remote_version $1` VERSION=`nvm_remote_version $1`
@ -250,16 +268,23 @@ nvm() {
v0.[1234567].*) binavail=0 ;; v0.[1234567].*) binavail=0 ;;
*) binavail=1 ;; *) binavail=1 ;;
esac esac
if [ $binavail -eq 1 ]; then if [ $binavail -eq 1 ] || [ ! -z $binary_url ]; then
t="$VERSION-$os-$arch" if [ -z "$binary_url" ]; then
url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz" t="$VERSION-$os-$arch"
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | \grep node-${t}.tar.gz | awk '{print $1}'` url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz"
else
url=$binary_url
echo "Fetching binary from $binary_url"
fi
if [ ! -z "$binary_url" ]; then
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | \grep node-${t}.tar.gz | awk '{print $1}'`
fi
local tmpdir="$NVM_DIR/bin/node-${t}" local tmpdir="$NVM_DIR/bin/node-${t}"
local tmptarball="$tmpdir/node-${t}.tar.gz" local tmptarball="$tmpdir/node-${t}.tar.gz"
if ( if (
mkdir -p "$tmpdir" && \ mkdir -p "$tmpdir" && \
curl -C - --progress-bar $url -o "$tmptarball" && \ curl -C - --progress-bar $url -o "$tmptarball" && \
nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum && \ if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum; fi && \
tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \ tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \
mv "$tmpdir" "$NVM_DIR/$VERSION" && \ mv "$tmpdir" "$NVM_DIR/$VERSION" && \
rm -f "$tmptarball" rm -f "$tmptarball"
@ -267,6 +292,10 @@ nvm() {
then then
nvm use $VERSION nvm use $VERSION
return; return;
elif [ -z "$source_url" ]; then
echo "Binary download failed, and no source URL provided. Aborting."
echo "nvm: install $VERSION failed!"
return 1
else else
echo "Binary download failed, trying source." >&2 echo "Binary download failed, trying source." >&2
rm -rf "$tmptarball" "$tmpdir" rm -rf "$tmptarball" "$tmpdir"
@ -281,15 +310,25 @@ nvm() {
sum='' sum=''
make='make' make='make'
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
make='gmake' make='gmake'
fi fi
local tmpdir="$NVM_DIR/src" local tmpdir="$NVM_DIR/src"
local tmptarball="$tmpdir/node-$VERSION.tar.gz" local tmptarball="$tmpdir/node-$VERSION.tar.gz"
if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | \grep '200 OK'`" != '' ]; then if [ ! -z "$source_url" ]; then
tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" if [ "`curl -Is "$source_url" | \grep '200 OK'`" != '' ]; then
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | \grep node-$VERSION.tar.gz | awk '{print $1}'` echo "Fetching source from $source_url"
elif [ "`curl -Is "http://nodejs.org/dist/node-$VERSION.tar.gz" | \grep '200 OK'`" != '' ]; then tarball=$source_url
tarball="http://nodejs.org/dist/node-$VERSION.tar.gz" else
echo "Could not find source tarball: $source_url"
return 1
fi
else
if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | \grep '200 OK'`" != '' ]; then
tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz"
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | \grep node-$VERSION.tar.gz | awk '{print $1}'`
elif [ "`curl -Is "http://nodejs.org/dist/node-$VERSION.tar.gz" | \grep '200 OK'`" != '' ]; then
tarball="http://nodejs.org/dist/node-$VERSION.tar.gz"
fi
fi fi
if ( if (
[ ! -z $tarball ] && \ [ ! -z $tarball ] && \