Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 217 lines (188 sloc) 4.932 kb
1137c97 @isaacs more stuff
authored
1 #!/bin/bash
2
3 # This program contains parts of narwhal's "sea" program,
4 # as well as bits borrowed from Tim Caswell's "nvm"
5
6 # nave install <version>
7 # Fetch the version of node and install it in nave's folder.
8
9 # nave use <version>
10 # Install the <version> if it isn't already, and then start
11 # a subshell with that version's folder at the start of the
12 # $PATH
13
14 # nave --version <version> program.js
15 # Like "nave use", but have the subshell start the program.js
16 # immediately.
17
18 # When told to use a version:
19 # Ensure that the version exists, install it, and
20 # then add its prefix to the PATH, and start a subshell.
21
22 main () {
23 # get the absolute path of the executable
eeb9654 @isaacs Get the SELF_PATH better when run without the ./ on teh relative path
authored
24 SELF_PATH="$0"
25 if [ "${SELF_PATH:0:1}" != "." ] && [ "${SELF_PATH:0:1}" != "/" ]; then
26 SELF_PATH=./"$SELF_PATH"
27 fi
28 SELF_PATH=$( cd -P -- "$(dirname -- "$SELF_PATH")" \
1137c97 @isaacs more stuff
authored
29 && pwd -P \
30 ) && SELF_PATH=$SELF_PATH/$(basename -- "$0")
31
32 # resolve symlinks
33 while [ -h "$SELF_PATH" ]; do
34 DIR=$(dirname -- "$SELF_PATH")
35 SYM=$(readlink -- "$SELF_PATH")
36 SELF_PATH=$( cd -- "$DIR" \
37 && cd -- $(dirname -- "$SYM") \
38 && pwd \
39 )/$(basename -- "$SYM")
40 done
41
42 export NAVE_SRC="$(dirname -- "$SELF_PATH")/src"
43 export NAVE_ROOT="$(dirname -- "$SELF_PATH")/installed"
44 ensure_dir "$NAVE_SRC"
45 ensure_dir "$NAVE_ROOT"
46
47 local cmd="$1"
48 shift
49 case $cmd in
1f67500 @tj Added ls-remote
tj authored
50 ls-remote)
51 nave_versions && exit
52 ;;
fd9d6ec @isaacs enable the "uninstall" command
authored
53 install | fetch | use | install | clean | test | ls | uninstall )
1137c97 @isaacs more stuff
authored
54 cmd="nave_$cmd"
55 ;;
56 * )
57 cmd="nave_help"
58 ;;
59 esac
60 $cmd "$@"
61 exit 0
62 }
63
64 function enquote_all () {
65 ARGS=""
66 for ARG in "$@"; do
67 [ -n "$ARGS" ] && ARGS="$ARGS "
68 ARGS="$ARGS'""$( echo " $ARG" \
69 | cut -c 2- \
70 | sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g' \
71 )""'"
72 done
73 echo "$ARGS"
74 }
75
76 ensure_dir () {
77 if ! [ -d "$1" ]; then
78 mkdir -p -- "$1" || fail "couldn't create $1"
79 fi
80 }
81 remove_dir () {
82 if [ -d "$1" ]; then
83 rm -rf -- "$1" || fail "Could not remove $1"
84 fi
85 }
86
87 fail () {
88 echo "$@" >&2
89 exit 1
90 }
91
1f67500 @tj Added ls-remote
tj authored
92 nave_versions() {
b6e64f4 @tj ls-remote using nodejs.org/dist
tj authored
93 curl -s http://nodejs.org/dist/ \
e886f47 @isaacs ls-remote: Just do the uniq-ing in the sort command
authored
94 | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
95 | sort -u -k 1,1n -k 2,2n -k 3,3n -t .
1f67500 @tj Added ls-remote
tj authored
96 }
97
1137c97 @isaacs more stuff
authored
98 nave_fetch () {
99 version="$1"
100 version="${version/v/}"
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
101 if nave_has "$version"; then
1137c97 @isaacs more stuff
authored
102 echo "already fetched $version" >&2
103 return 0
104 fi
105
106 src="$NAVE_SRC/$version"
107 remove_dir "$src"
108 ensure_dir "$src"
0560305 @isaacs Remove the HEAD stuff. That's what git is for.
authored
109 url="http://nodejs.org/dist/node-v$version.tar.gz"
dbb65ad @tj Less verbose install
tj authored
110 curl -# -L "$url" \
111 | tar xz -C "$src" --strip 1 \
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
112 || fail "Couldn't fetch $version"
1137c97 @isaacs more stuff
authored
113 return 0
114 }
115
116 nave_install () {
117 version="$1"
118 version="${version/v/}"
119
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
120 if nave_installed "$version"; then
1137c97 @isaacs more stuff
authored
121 echo "Already installed: $version" >&2
122 return 0
123 fi
124 nave_fetch "$version"
125
126 src="$NAVE_SRC/$version"
127 install="$NAVE_ROOT/$version"
128 remove_dir "$install"
129 ensure_dir "$install"
130 ( cd -- "$src"
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
131 JOBS=8 ./configure --prefix="$install" || fail "Failed to configure $version"
132 JOBS=8 make || fail "Failed to make $version"
1137c97 @isaacs more stuff
authored
133 make install || fail "Failed to install $version"
134 ) || fail "fail"
135 }
136 nave_test () {
137 version="$1"
138 version="${version/v/}"
139 nave_fetch "$version"
140 src="$NAVE_SRC/$version"
141 ( cd -- "$src"
142 ./configure --debug || fail "failed to ./configure --debug"
143 make test-all || fail "Failed tests"
144 ) || fail "failed"
145 }
146
147 nave_ls () {
148 ( cd -- "$(dirname -- "$SELF_PATH")"
149 ls -- {"$(basename "$NAVE_SRC")","$(basename "$NAVE_ROOT")"}
150 )
151 }
152
153 nave_has () {
154 version="$1"
155 version="${version/v/}"
156 [ -d "$NAVE_SRC/$version" ] || return 1
157 }
158 nave_installed () {
159 version="$1"
160 version="${version/v/}"
161 [ -d "$NAVE_ROOT/$version/bin" ] || return 1
162 }
163
164 nave_use () {
165 version="$1"
166 version="${version/v/}"
167 nave_install "$version"
168 bin="$NAVE_ROOT/$version/bin"
169 if [ "$version" == "$NAVE" ]; then
170 echo "already using $NAVE"
171 if [ $# -gt 1 ]; then
172 shift
173 node "$@"
174 fi
175 return
176 fi
177 lvl=$[ ${NAVELVL-0} + 1 ]
178 echo "using $version"
179 if [ $# -gt 1 ]; then
180 shift
181 PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" "$SHELL" -c "$(enquote_all node "$@")"
182 else
183 PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" "$SHELL"
184 fi
185 }
186
187 nave_clean () {
188 version="$1"
189 version="${version/v/}"
190 remove_dir "$NAVE_SRC/$version"
191 }
192 nave_uninstall () {
193 version="$1"
194 version="${version/v/}"
195 remove_dir "$NAVE_ROOT/$version"
196 }
197
198 nave_help () {
2da1027 @tj Populated help
tj authored
199 cat <<EOF
200
201 Usage: nave <cmd>
202
203 Commands:
204
205 install <version> Install the version passed (ex: 0.1.103)
206 use <version> Enter a subshell where <version> is being used
207 clean <version> Delete the source code for <version>
208 uninstall <version> Delete the install for <version>
1f67500 @tj Added ls-remote
tj authored
209 ls-remote List remote node versions
210 ls List versions currently installed
2da1027 @tj Populated help
tj authored
211 help Output help information
212
213 EOF
1137c97 @isaacs more stuff
authored
214 }
215
216 main "$@"
Something went wrong with that request. Please try again.