Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 616 lines (539 sloc) 14.56 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
bce616a @isaacs Add NAVE_DEBUG, and use ~/.nave instead of the nave dir
authored
14 # nave use <version> program.js
1137c97 @isaacs more stuff
authored
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
bce616a @isaacs Add NAVE_DEBUG, and use ~/.nave instead of the nave dir
authored
22 if [ "$NAVE_DEBUG" != "" ]; then
23 set -x
24 fi
25
8f7057b @isaacs Fix #36 Use $BASH, not $SHELL
authored
26 if [ -z "$BASH" ]; then
27 echo "Nave only works with bash. Sorry." >&2
28 exit 1
29 fi
30
daefc95 @isaacs Be more correct about handling version families.
authored
31 # Use fancy pants globs
32 shopt -s extglob
33
2ba30ed @isaacs Try to use binary dist if available
authored
34 # Try to figure out the os and arch for binary fetching
35 uname="$(uname -a)"
36 os=
37 arch=x86
38 case "$uname" in
39 Linux\ *) os=linux ;;
40 Darwin\ *) os=darwin ;;
41 SunOS\ *) os=sunos ;;
42 esac
43 case "$uname" in
44 *x86_64*) arch=x64 ;;
45 esac
46
d1115b0 @isaacs Respect the TAR env, if set
authored
47 tar=${TAR-tar}
48
1137c97 @isaacs more stuff
authored
49 main () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
50 local SELF_PATH DIR SYM
1137c97 @isaacs more stuff
authored
51 # get the absolute path of the executable
eeb9654 @isaacs Get the SELF_PATH better when run without the ./ on teh relative path
authored
52 SELF_PATH="$0"
53 if [ "${SELF_PATH:0:1}" != "." ] && [ "${SELF_PATH:0:1}" != "/" ]; then
54 SELF_PATH=./"$SELF_PATH"
55 fi
56 SELF_PATH=$( cd -P -- "$(dirname -- "$SELF_PATH")" \
1137c97 @isaacs more stuff
authored
57 && pwd -P \
58 ) && SELF_PATH=$SELF_PATH/$(basename -- "$0")
59
60 # resolve symlinks
61 while [ -h "$SELF_PATH" ]; do
62 DIR=$(dirname -- "$SELF_PATH")
63 SYM=$(readlink -- "$SELF_PATH")
64 SELF_PATH=$( cd -- "$DIR" \
65 && cd -- $(dirname -- "$SYM") \
66 && pwd \
67 )/$(basename -- "$SYM")
68 done
69
bce616a @isaacs Add NAVE_DEBUG, and use ~/.nave instead of the nave dir
authored
70 if ! [ -d "$NAVE_DIR" ]; then
71 if [ -d "$HOME" ]; then
72 NAVE_DIR="$HOME"/.nave
73 else
4e689f6 @isaacs Don't put stuff directly in /usr/local.
authored
74 NAVE_DIR=/usr/local/lib/nave
bce616a @isaacs Add NAVE_DEBUG, and use ~/.nave instead of the nave dir
authored
75 fi
76 fi
77 if ! [ -d "$NAVE_DIR" ] && ! mkdir -p -- "$NAVE_DIR"; then
78 NAVE_DIR="$(dirname -- "$SELF_PATH")"
79 fi
80
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
81 # set up the naverc init file.
81b3b04 @isaacs Only execute .bashrc if not a login shell
authored
82 # Important! Update this number any time the init content is changed.
83 local rcversion="#2"
84 if ! [ -f "$NAVE_DIR/naverc" ] \
85 || [ "$(head -n1 "$NAVE_DIR/naverc")" != "$rcversion" ]; then
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
86 cat > "$NAVE_DIR/naverc" <<RC
81b3b04 @isaacs Only execute .bashrc if not a login shell
authored
87 $rcversion
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
88 [ "\$NAVE_DEBUG" != "" ] && set -x || true
89 if [ "\$NAVE_LOGIN" != "" ]; then
90 [ -f ~/.bash_profile ] && . ~/.bash_profile || true
91 [ -f ~/.bash_login ] && . ~/.bash_login || true
92 [ -f ~/.profile ] && . ~/.profile || true
81b3b04 @isaacs Only execute .bashrc if not a login shell
authored
93 else
94 [ -f ~/.bashrc ] && . ~/.bashrc || true
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
95 fi
96 export PATH=\$NAVEPATH:\$PATH
97 [ -f ~/.naverc ] && . ~/.naverc || true
98 RC
99 fi
100
101 # couldn't write file
81b3b04 @isaacs Only execute .bashrc if not a login shell
authored
102 if ! [ -f "$NAVE_DIR/naverc" ] \
103 || [ "$(head -n1 "$NAVE_DIR/naverc")" != "$rcversion" ]; then
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
104 fail "Nave dir $NAVE_DIR is not writable."
105 fi
106
bce616a @isaacs Add NAVE_DEBUG, and use ~/.nave instead of the nave dir
authored
107 export NAVE_DIR
108 export NAVE_SRC="$NAVE_DIR/src"
109 export NAVE_ROOT="$NAVE_DIR/installed"
1137c97 @isaacs more stuff
authored
110 ensure_dir "$NAVE_SRC"
111 ensure_dir "$NAVE_ROOT"
0d79e42 @isaacs whitespace
authored
112
1137c97 @isaacs more stuff
authored
113 local cmd="$1"
114 shift
115 case $cmd in
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
116 ls-remote | ls-all)
117 cmd="nave_${cmd/-/_}"
1f67500 @tj Added ls-remote
tj authored
118 ;;
e850ed1 @isaacs rough sketch of named envs
authored
119 # use)
120 # cmd="nave_named"
121 # ;;
122 install | fetch | use | clean | test | named | \
fd89663 @welwood08 Expose nave_has and nave_installed functions as commands for easier s…
welwood08 authored
123 ls | uninstall | usemain | latest | stable | has | installed )
1137c97 @isaacs more stuff
authored
124 cmd="nave_$cmd"
125 ;;
126 * )
127 cmd="nave_help"
128 ;;
129 esac
791edbd @isaacs Exit from nave_use with actual exit code
authored
130 $cmd "$@"
131 local ret=$?
132 if [ $ret -eq 0 ]; then
133 exit 0
134 else
135 echo "failed with code=$ret" >&2
136 exit $ret
137 fi
1137c97 @isaacs more stuff
authored
138 }
139
140 function enquote_all () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
141 local ARG ARGS
1137c97 @isaacs more stuff
authored
142 ARGS=""
143 for ARG in "$@"; do
144 [ -n "$ARGS" ] && ARGS="$ARGS "
145 ARGS="$ARGS'""$( echo " $ARG" \
146 | cut -c 2- \
147 | sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g' \
148 )""'"
149 done
150 echo "$ARGS"
151 }
152
153 ensure_dir () {
154 if ! [ -d "$1" ]; then
155 mkdir -p -- "$1" || fail "couldn't create $1"
156 fi
157 }
c160e5e @isaacs Spacing
authored
158
1137c97 @isaacs more stuff
authored
159 remove_dir () {
160 if [ -d "$1" ]; then
161 rm -rf -- "$1" || fail "Could not remove $1"
162 fi
163 }
164
165 fail () {
166 echo "$@" >&2
167 exit 1
168 }
169
170 nave_fetch () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
171 local version=$(ver "$1")
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
172 if nave_has "$version"; then
1137c97 @isaacs more stuff
authored
173 echo "already fetched $version" >&2
174 return 0
175 fi
176
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
177 local src="$NAVE_SRC/$version"
1137c97 @isaacs more stuff
authored
178 remove_dir "$src"
179 ensure_dir "$src"
923c16f @isaacs Close #19 new url scheme for 0.5
authored
180
6c9e612 @welwood08 Abstract out build() method
welwood08 authored
181 local url
182 local urls=(
183 "http://nodejs.org/dist/v$version/node-v$version.tar.gz"
184 "http://nodejs.org/dist/node-v$version.tar.gz"
185 "http://nodejs.org/dist/node-$version.tar.gz"
186 )
187 for url in "${urls[@]}"; do
52c1451 @isaacs Handle curl failures properly
authored
188 curl -#Lf "$url" > "$src".tgz
6c9e612 @welwood08 Abstract out build() method
welwood08 authored
189 if [ $? -eq 0 ]; then
52c1451 @isaacs Handle curl failures properly
authored
190 $tar xzf "$src".tgz -C "$src" --strip-components=1
191 if [ $? -eq 0 ]; then
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
192 echo "fetched from $url" >&2
52c1451 @isaacs Handle curl failures properly
authored
193 return 0
194 fi
6c9e612 @welwood08 Abstract out build() method
welwood08 authored
195 fi
196 done
197
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
198 rm "$src".tgz
6c9e612 @welwood08 Abstract out build() method
welwood08 authored
199 remove_dir "$src"
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
200 echo "Couldn't fetch $version" >&2
201 return 1
1137c97 @isaacs more stuff
authored
202 }
203
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
204 build () {
205 local version="$1"
2ba30ed @isaacs Try to use binary dist if available
authored
206
207 # shortcut - try the binary if possible.
208 if [ -n "$os" ]; then
209 local binavail
210 # binaries started with node 0.8.6
211 case "$version" in
212 0.8.[012345]) binavail=0 ;;
213 0.[1234567]) binavail=0 ;;
214 *) binavail=1 ;;
215 esac
216 if [ $binavail -eq 1 ]; then
217 local t="$version-$os-$arch"
218 local url="http://nodejs.org/dist/v$version/node-v${t}.tar.gz"
219 local tgz="$NAVE_SRC/$t.tgz"
220 curl -#Lf "$url" > "$tgz"
221 if [ $? -ne 0 ]; then
222 # binary download failed. oh well. cleanup, and proceed.
223 rm "$tgz"
224 echo "Binary download failed, trying source." >&2
225 else
226 # unpack straight into the build target.
227 $tar xzf "$tgz" -C "$2" --strip-components 1
228 if [ $? -ne 0 ]; then
229 rm "$tgz"
230 nave_uninstall "$version"
231 echo "Binary unpack failed, trying source." >&2
232 fi
233 # it worked!
234 echo "installed from binary" >&2
235 return 0
236 fi
237 fi
238 fi
239
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
240 nave_fetch "$version"
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
241 if [ $? != 0 ]; then
242 # fetch failed, don't continue and try to build it.
243 return 1
244 fi
245
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
246 local src="$NAVE_SRC/$version"
5bc6eac @isaacs Default jobs to number of CPUs
authored
247 local jobs=$NAVE_JOBS
248 jobs=${jobs:-$JOBS}
249 jobs=${jobs:-$(sysctl -n hw.ncpu)}
250 jobs=${jobs:-2}
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
251
252 ( cd -- "$src"
d0af355 @isaacs Read NAVE_CONFIG array from env when building
authored
253 [ -f ~/.naverc ] && . ~/.naverc || true
254 if [ "$NAVE_CONFIG" == "" ]; then
bd1e9bb @isaacs Don't use --debug flag by default
authored
255 NAVE_CONFIG=()
d0af355 @isaacs Read NAVE_CONFIG array from env when building
authored
256 fi
5bc6eac @isaacs Default jobs to number of CPUs
authored
257 JOBS=$jobs ./configure "${NAVE_CONFIG[@]}" --prefix="$2" \
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
258 || fail "Failed to configure $version"
5bc6eac @isaacs Default jobs to number of CPUs
authored
259 JOBS=$jobs make -j$jobs \
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
260 || fail "Failed to make $version"
261 make install || fail "Failed to install $version"
262 ) || fail "fail"
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
263 return $?
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
264 }
265
20740ec @isaacs Add usemain, since TJ asked so nice, and it actually is quite handy.
authored
266 nave_usemain () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
267 if [ ${NAVELVL-0} -gt 0 ]; then
268 fail "Can't usemain inside a nave subshell. Exit to main shell."
269 fi
270 local version=$(ver "$1")
20740ec @isaacs Add usemain, since TJ asked so nice, and it actually is quite handy.
authored
271 local current=$(node -v || true)
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
272 local wn=$(which node || true)
273 local prefix="/usr/local"
274 if [ "x$wn" != "x" ]; then
275 prefix="${wn/\/bin\/node/}"
276 if [ "x$prefix" == "x" ]; then
277 prefix="/usr/local"
278 fi
2e69cc0 @isaacs Add support for 'latest' as a version string
authored
279 fi
20740ec @isaacs Add usemain, since TJ asked so nice, and it actually is quite handy.
authored
280 current="${current/v/}"
281 if [ "$current" == "$version" ]; then
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
282 echo "$version already installed" >&2
20740ec @isaacs Add usemain, since TJ asked so nice, and it actually is quite handy.
authored
283 return 0
284 fi
8d709bb @isaacs Liberal use of hash -r whenever the PATH changes, and respect JOBS env
authored
285
6c9e612 @welwood08 Abstract out build() method
welwood08 authored
286 build "$version" "$prefix"
20740ec @isaacs Add usemain, since TJ asked so nice, and it actually is quite handy.
authored
287 }
288
1137c97 @isaacs more stuff
authored
289 nave_install () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
290 local version=$(ver "$1")
8a02f53 @isaacs Handle HEAD as a version. Use 8 jobs. Clean up messes.
authored
291 if nave_installed "$version"; then
1137c97 @isaacs more stuff
authored
292 echo "Already installed: $version" >&2
293 return 0
294 fi
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
295 local install="$NAVE_ROOT/$version"
1137c97 @isaacs more stuff
authored
296 ensure_dir "$install"
218dfd4 @welwood08 Re-use build code across install and usemain functions.
welwood08 authored
297
298 build "$version" "$install"
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
299 local ret=$?
300 if [ $ret -ne 0 ]; then
301 remove_dir "$install"
302 return $ret
303 fi
1137c97 @isaacs more stuff
authored
304 }
c160e5e @isaacs Spacing
authored
305
1137c97 @isaacs more stuff
authored
306 nave_test () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
307 local version=$(ver "$1")
1137c97 @isaacs more stuff
authored
308 nave_fetch "$version"
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
309 local src="$NAVE_SRC/$version"
1137c97 @isaacs more stuff
authored
310 ( cd -- "$src"
d0af355 @isaacs Read NAVE_CONFIG array from env when building
authored
311 [ -f ~/.naverc ] && . ~/.naverc || true
312 if [ "$NAVE_CONFIG" == "" ]; then
bd1e9bb @isaacs Don't use --debug flag by default
authored
313 NAVE_CONFIG=()
d0af355 @isaacs Read NAVE_CONFIG array from env when building
authored
314 fi
bd1e9bb @isaacs Don't use --debug flag by default
authored
315 ./configure "${NAVE_CONFIG[@]}" || fail "failed to ./configure"
1137c97 @isaacs more stuff
authored
316 make test-all || fail "Failed tests"
317 ) || fail "failed"
318 }
319
320 nave_ls () {
8b86b4a @kaichen fix the path issue
kaichen authored
321 ls -- $NAVE_SRC | version_list "src" \
322 && ls -- $NAVE_ROOT | version_list "installed" \
3d4ff49 @isaacs Support for named envs
authored
323 && nave_ls_named \
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
324 || return 1
325 }
c160e5e @isaacs Spacing
authored
326
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
327 nave_ls_remote () {
328 curl -s http://nodejs.org/dist/ \
329 | version_list "remote" \
330 || return 1
331 }
c160e5e @isaacs Spacing
authored
332
3d4ff49 @isaacs Support for named envs
authored
333 nave_ls_named () {
334 echo "named:"
335 ls -- "$NAVE_ROOT" \
336 | egrep -v '[0-9]+\.[0-9]+\.[0-9]+' \
337 | sort \
338 | while read name; do
339 echo "$name: $(ver $($NAVE_ROOT/$name/bin/node -v 2>/dev/null))"
340 done
341 }
342
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
343 nave_ls_all () {
344 nave_ls \
d2397c9 @isaacs Skip a space before the remote: list on ls-all
authored
345 && (echo ""; nave_ls_remote) \
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
346 || return 1
347 }
c160e5e @isaacs Spacing
authored
348
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
349 ver () {
350 local version="$1"
3d4ff49 @isaacs Support for named envs
authored
351 local nonames="$2"
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
352 version="${version/v/}"
353 case $version in
354 latest | stable) nave_$version ;;
daefc95 @isaacs Be more correct about handling version families.
authored
355 +([0-9])\.+([0-9])) nave_version_family "$version" ;;
3d4ff49 @isaacs Support for named envs
authored
356 +([0-9])\.+([0-9])\.+([0-9])) echo $version ;;
357 *) [ "$nonames" = "" ] && echo $version ;;
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
358 esac
359 }
7d5ac87 @isaacs Support x.y version family as a version
authored
360
361 nave_version_family () {
362 local family="$1"
363 family="${family/v/}"
364 curl -s http://nodejs.org/dist/ \
365 | egrep -o $family'\.[0-9]+' \
366 | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
367 | tail -n1
368 }
369
2e69cc0 @isaacs Add support for 'latest' as a version string
authored
370 nave_latest () {
371 curl -s http://nodejs.org/dist/ \
372 | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
373 | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
374 | tail -n1
375 }
c160e5e @isaacs Spacing
authored
376
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
377 nave_stable () {
378 curl -s http://nodejs.org/dist/ \
a17416a @welwood08 Recognise all even minor versions as stable
welwood08 authored
379 | egrep -o '[0-9]+\.[0-9]*[02468]\.[0-9]+' \
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
380 | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
381 | tail -n1
382 }
383
3d4ff49 @isaacs Support for named envs
authored
384 version_list_named () {
385 egrep -v '[0-9]+\.[0-9]+\.[0-9]+' \
386 | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
387 | organize_version_list \
388 || return 1
389 }
390
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
391 version_list () {
392 echo "$1:"
393 egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
394 | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
395 | organize_version_list \
396 || return 1
1137c97 @isaacs more stuff
authored
397 }
398
3d645fc @isaacs Refactor ls-* functions to all use the same sort, and to sort and pre…
authored
399 organize_version_list () {
400 local i=0
401 local v
402 while read v; do
403 if [ $i -eq 8 ]; then
404 i=0
405 echo "$v"
406 else
407 let 'i = i + 1'
408 echo -ne "$v\t"
409 fi
410 done
411 echo ""
412 [ $i -ne 0 ] && echo ""
413 return 0
414 }
415
1137c97 @isaacs more stuff
authored
416 nave_has () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
417 local version=$(ver "$1")
b1d8060 @isaacs Avoid unnecessary src dir removal
authored
418 [ -x "$NAVE_SRC/$version/configure" ] || return 1
1137c97 @isaacs more stuff
authored
419 }
c160e5e @isaacs Spacing
authored
420
1137c97 @isaacs more stuff
authored
421 nave_installed () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
422 local version=$(ver "$1")
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
423 [ -x "$NAVE_ROOT/$version/bin/node" ] || return 1
0d79e42 @isaacs whitespace
authored
424 }
1137c97 @isaacs more stuff
authored
425
426 nave_use () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
427 local version=$(ver "$1")
3d4ff49 @isaacs Support for named envs
authored
428
429 # if it's not a version number, then treat as a name.
430 case "$version" in
431 +([0-9])\.+([0-9])\.+([0-9])) ;;
432 *)
433 nave_named "$@"
434 return $?
435 ;;
436 esac
437
0e1b749 @isaacs add a check when 'nave use' is called with no args
authored
438 if [ -z "$version" ]; then
439 fail "Must supply a version"
440 fi
3d4ff49 @isaacs Support for named envs
authored
441
442 if [ "$version" == "$NAVENAME" ]; then
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
443 echo "already using $version" >&2
1137c97 @isaacs more stuff
authored
444 if [ $# -gt 1 ]; then
445 shift
2123dda @isaacs Don't run programs as node. Just run them.
authored
446 "$@"
1137c97 @isaacs more stuff
authored
447 fi
e850ed1 @isaacs rough sketch of named envs
authored
448 return $?
1137c97 @isaacs more stuff
authored
449 fi
3d4ff49 @isaacs Support for named envs
authored
450
451 nave_install "$version" || fail "failed to install $version"
e94d80f @isaacs Set global prefix, as well as binroot/etc.
authored
452 local prefix="$NAVE_ROOT/$version"
453 local bin="$prefix/bin"
454 local lib="$prefix/lib/node"
455 local man="$prefix/share/man"
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
456 local lvl=$[ ${NAVELVL-0} + 1 ]
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
457 echo "using $version" >&2
1137c97 @isaacs more stuff
authored
458 if [ $# -gt 1 ]; then
459 shift
8d709bb @isaacs Liberal use of hash -r whenever the PATH changes, and respect JOBS env
authored
460 hash -r
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
461 NAVELVL=$lvl NAVE="$version" \
462 NAVEPATH="$bin" \
e850ed1 @isaacs rough sketch of named envs
authored
463 NAVEVERSION="$version" \
3d4ff49 @isaacs Support for named envs
authored
464 NAVENAME="$version" \
0e1b749 @isaacs add a check when 'nave use' is called with no args
authored
465 npm_config_binroot="$bin" npm_config_root="$lib" \
e850ed1 @isaacs rough sketch of named envs
authored
466 npm_config_manroot="$man" \
e94d80f @isaacs Set global prefix, as well as binroot/etc.
authored
467 npm_config_prefix="$prefix" \
33367eb @isaacs Set npm configs and NODE_PATH in the env.
authored
468 NODE_PATH="$lib" \
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
469 NAVE_LOGIN="" \
8f7057b @isaacs Fix #36 Use $BASH, not $SHELL
authored
470 "$BASH" -c ". $(enquote_all $NAVE_DIR/naverc); $(enquote_all "$@")"
4306309 @niallo correctly pass-thu exit status with `nave use`.
niallo authored
471 exit_code=$?
8d709bb @isaacs Liberal use of hash -r whenever the PATH changes, and respect JOBS env
authored
472 hash -r
1137c97 @isaacs more stuff
authored
473 else
8d709bb @isaacs Liberal use of hash -r whenever the PATH changes, and respect JOBS env
authored
474 hash -r
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
475 NAVELVL=$lvl NAVE="$version" \
476 NAVEPATH="$bin" \
e850ed1 @isaacs rough sketch of named envs
authored
477 NAVEVERSION="$version" \
3d4ff49 @isaacs Support for named envs
authored
478 NAVENAME="$version" \
0e1b749 @isaacs add a check when 'nave use' is called with no args
authored
479 npm_config_binroot="$bin" npm_config_root="$lib" \
e850ed1 @isaacs rough sketch of named envs
authored
480 npm_config_manroot="$man" \
e94d80f @isaacs Set global prefix, as well as binroot/etc.
authored
481 npm_config_prefix="$prefix" \
33367eb @isaacs Set npm configs and NODE_PATH in the env.
authored
482 NODE_PATH="$lib" \
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
483 NAVE_LOGIN="1" \
8f7057b @isaacs Fix #36 Use $BASH, not $SHELL
authored
484 "$BASH" --rcfile "$NAVE_DIR/naverc"
4306309 @niallo correctly pass-thu exit status with `nave use`.
niallo authored
485 exit_code=$?
8d709bb @isaacs Liberal use of hash -r whenever the PATH changes, and respect JOBS env
authored
486 hash -r
1137c97 @isaacs more stuff
authored
487 fi
4306309 @niallo correctly pass-thu exit status with `nave use`.
niallo authored
488 return $exit_code
1137c97 @isaacs more stuff
authored
489 }
490
e850ed1 @isaacs rough sketch of named envs
authored
491 nave_named () {
492 local name="$1"
493 shift
3d4ff49 @isaacs Support for named envs
authored
494
495 local version=$(ver "$1" NONAMES)
496 if [ "$version" != "" ]; then
497 shift
498 fi
499
500 add_named_env "$name" "$version" || fail "failed to create $name env"
501
502 if [ "$name" == "$NAVENAME" ] && [ "$version" == "$NAVEVERSION" ]; then
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
503 echo "already using $name" >&2
e850ed1 @isaacs rough sketch of named envs
authored
504 if [ $# -gt 0 ]; then
2123dda @isaacs Don't run programs as node. Just run them.
authored
505 "$@"
e850ed1 @isaacs rough sketch of named envs
authored
506 fi
507 return $?
508 fi
3d4ff49 @isaacs Support for named envs
authored
509
510 if [ "$version" = "" ]; then
511 version="$(ver "$("$NAVE_ROOT/$name/bin/node" -v 2>/dev/null)")"
e850ed1 @isaacs rough sketch of named envs
authored
512 fi
3d4ff49 @isaacs Support for named envs
authored
513
e850ed1 @isaacs rough sketch of named envs
authored
514 local bin="$NAVE_ROOT/$name/bin"
515 local lib="$NAVE_ROOT/$name/lib/node"
516 local man="$NAVE_ROOT/$name/share/man"
517 ensure_dir bin
518 ensure_dir lib
519 ensure_dir man
3d4ff49 @isaacs Support for named envs
authored
520
e850ed1 @isaacs rough sketch of named envs
authored
521 local lvl=$[ ${NAVELVL-0} + 1 ]
522 # get the version
523 if [ $# -gt 0 ]; then
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
524 NAVEPATH="$bin" \
e850ed1 @isaacs rough sketch of named envs
authored
525 NAVELVL=$lvl \
3d4ff49 @isaacs Support for named envs
authored
526 NAVE="$version-$name" \
e850ed1 @isaacs rough sketch of named envs
authored
527 NAVEVERSION="$version" \
528 NAVENAME="$name" \
529 NODE_PATH="$lib" \
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
530 NAVE_LOGIN="" \
8f7057b @isaacs Fix #36 Use $BASH, not $SHELL
authored
531 "$BASH" -c ". $(enquote_all $NAVE_DIR/naverc); $(enquote_all "$@")"
e850ed1 @isaacs rough sketch of named envs
authored
532 else
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
533 NAVEPATH="$bin" \
e850ed1 @isaacs rough sketch of named envs
authored
534 NAVELVL=$lvl \
3d4ff49 @isaacs Support for named envs
authored
535 NAVE="$version-$name" \
e850ed1 @isaacs rough sketch of named envs
authored
536 NAVEVERSION="$version" \
537 NAVENAME="$name" \
538 NODE_PATH="$lib" \
7666933 @isaacs Work around cases where PATH is set explicitly in .bashrc
authored
539 NAVE_LOGIN="1" \
8f7057b @isaacs Fix #36 Use $BASH, not $SHELL
authored
540 "$BASH" --rcfile "$NAVE_DIR/naverc"
e850ed1 @isaacs rough sketch of named envs
authored
541 fi
542 return $?
543 }
544
545 add_named_env () {
546 local name="$1"
3d4ff49 @isaacs Support for named envs
authored
547 local version="$2"
548 local cur="$(ver "$($NAVE_ROOT/$name/bin/node -v 2>/dev/null)" "NONAMES")"
549
550 if [ "$version" != "" ]; then
551 version="$(ver "$version" "NONAMES")"
552 else
553 version="$cur"
554 fi
555
556 if [ "$version" = "" ]; then
557 echo "What version of node?"
558 read -p "stable, latest, x.y, or x.y.z > " version
559 version=$(ver "$version")
560 fi
561
562 # if that version is already there, then nothing to do.
563 if [ "$cur" = "$version" ]; then
564 return 0
565 fi
566
530e86f @isaacs Fix #32 Print informational stuff to stderr
authored
567 echo "Creating new env named '$name' using node $version" >&2
3d4ff49 @isaacs Support for named envs
authored
568
e850ed1 @isaacs rough sketch of named envs
authored
569 nave_install "$version" || fail "failed to install $version"
3d4ff49 @isaacs Support for named envs
authored
570 ensure_dir "$NAVE_ROOT/$name/bin"
571 ensure_dir "$NAVE_ROOT/$name/lib/node"
572 ensure_dir "$NAVE_ROOT/$name/lib/node_modules"
573 ensure_dir "$NAVE_ROOT/$name/share/man"
e850ed1 @isaacs rough sketch of named envs
authored
574
3d4ff49 @isaacs Support for named envs
authored
575 ln -sf -- "$NAVE_ROOT/$version/bin/node" "$NAVE_ROOT/$name/bin/node"
576 ln -sf -- "$NAVE_ROOT/$version/bin/node-waf" "$NAVE_ROOT/$name/bin/node-waf"
577 }
e850ed1 @isaacs rough sketch of named envs
authored
578
1137c97 @isaacs more stuff
authored
579 nave_clean () {
87a7ac3 @isaacs Fix #41 Better cleanup and error detection
authored
580 rm -rf "$NAVE_SRC/$(ver "$1")" "$NAVE_SRC/$(ver "$1")".tgz
1137c97 @isaacs more stuff
authored
581 }
3d4ff49 @isaacs Support for named envs
authored
582
1137c97 @isaacs more stuff
authored
583 nave_uninstall () {
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
584 remove_dir "$NAVE_ROOT/$(ver "$1")"
1137c97 @isaacs more stuff
authored
585 }
586
587 nave_help () {
2da1027 @tj Populated help
tj authored
588 cat <<EOF
589
590 Usage: nave <cmd>
591
592 Commands:
593
3d4ff49 @isaacs Support for named envs
authored
594 install <version> Install the version passed (ex: 0.1.103)
595 use <version> Enter a subshell where <version> is being used
2123dda @isaacs Don't run programs as node. Just run them.
authored
596 use <ver> <program> Enter a subshell, and run "<program>", then exit
3d4ff49 @isaacs Support for named envs
authored
597 use <name> <ver> Create a named env, using the specified version.
598 If the name already exists, but the version differs,
599 then it will update the link.
600 usemain <version> Install in /usr/local/bin (ie, use as your main nodejs)
601 clean <version> Delete the source code for <version>
602 uninstall <version> Delete the install for <version>
603 ls List versions currently installed
604 ls-remote List remote node versions
605 ls-all List remote and local node versions
606 latest Show the most recent dist version
607 help Output help information
2da1027 @tj Populated help
tj authored
608
ada82fa @isaacs Support "stable" as a shorthand for "latest 0.2.x"
authored
609 <version> can be the string "latest" to get the latest distribution.
610 <version> can be the string "stable" to get the latest stable version.
2e69cc0 @isaacs Add support for 'latest' as a version string
authored
611
2da1027 @tj Populated help
tj authored
612 EOF
1137c97 @isaacs more stuff
authored
613 }
614
615 main "$@"
Something went wrong with that request. Please try again.