Skip to content

HTTPS clone URL

Subversion checkout URL

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