forked from cutting-room-floor/tilestream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndistro
executable file
·358 lines (311 loc) · 7.48 KB
/
ndistro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env sh
#
# nDistro - Node distribution manager
#
# Copyright (c) 2010 - TJ Holowaychuk <[email protected]>
# Licensed MIT.
#
# Library version
VERSION="0.4.0"
DIR=${0%/*}
ROOT=$(pwd)
SRC=$ROOT/src
BIN_URL="http://github.com/visionmedia/nodes/raw/master"
GET=
GET_CMD=
# wget support
which wget > /dev/null && GET="wget -q -O-" && GET_CMD="wget"
# curl support
which curl > /dev/null && GET="curl -# -L" && GET_CMD="curl"
#
# Exit with the given <msg ...>
#
abort() {
echo "Error: $@" && exit 1
}
# Ensure we have curl or wget
test -z "$GET" && abort "curl or wget required"
#
# Log the given <msg ...>
#
log() {
echo "... $@"
}
#
# Retrieves the URL and returns the HTTP status code
#
http_code() {
if test "$GET_CMD" = "curl"; then
return $(curl -L -I -w %{http_code} --silent -o /dev/null "$1")
else
return $(wget -q --spider -S "$1" 2>&1 | sed -n -e 's/^ HTTP\/1\.[01] \([0-9]*\) .*$/\1/p' | sed -n '$p')
fi
}
#
# Install <user> <mod> [version] [version]
#
module() {
local user=$1
local mod=$2
local version=${3-master}
local alias_as=$4
local dest="$ROOT/modules/$mod"
local version_file=$dest/.ndistro_module_version
local url="https://github.com/$user/$mod/tarball/$version"
local update_message="update with $ rm -fr modules/$mod && ndistro"
if test -d $dest; then
if test -f $version_file; then
if test "$(cat $version_file)" != "$version"; then
log "outdated module $mod $(cat $version_file) (requested $version)"
log $update_message
else
log "already installed $mod $version"
fi
elif test "$version" != "master"; then
log "already installed $mod, but version is unknown"
log $update_message
fi
else
log "installing $mod $version"
mkdir -p $dest
cd $dest \
&& $GET $url \
| tar -xz --strip 1 \
&& cd ../.. \
&& bin $mod \
&& build $mod \
&& lib $mod $alias_as \
&& echo "$version" > $version_file
fi
}
#
# Install <mod> binaries.
#
# - alters shebang to $ROOT/bin/node
#
bin() {
if test -d $ROOT/modules/$mod/bin; then
cd $ROOT/bin
for bin in ../modules/$mod/bin/*; do
local name=${bin##*/}
log "installing bin/$name"
local shebang=$(head -n 1 $bin)
if test "$shebang" = "#!/usr/bin/env node"; then
sed "s|/usr/bin/env node|$ROOT/bin/node|" $bin \
> ./$name \
&& chmod a+x ./$name
else
ln -sf $bin ./$name
fi
done
cd ..
fi
}
#
# Maybe build <mod>.
#
build() {
local mod=$1
if test -f $ROOT/modules/$mod/wscript; then
if test -f $ROOT/bin/node-waf; then
log "building $mod"
cd $ROOT/modules/$mod \
&& $ROOT/bin/node-waf --prefix=$ROOT configure build
else
log "skipped building $mod, local node-waf not found"
fi
fi
}
#
# Install <mod> library.
#
lib() {
local mod=$1
local alias_as=$2
local moddir=$mod
if test -n "$alias_as"; then
moddir=$alias_as
fi
mkdir -p $ROOT/lib/node
cd $ROOT/lib/node
if test -f $ROOT/modules/$mod/index.js; then
ln -sf ../../modules/$mod ./$moddir
elif test -f $ROOT/modules/$mod/lib/index.js; then
ln -sf ../../modules/$mod/lib ./$moddir
elif test -d $ROOT/modules/$mod/lib; then
# Attempt to support aliasing for module layouts like
# modules/foo/lib/foo -> lib/node/bar. If modules/foo/lib
# contains multiple directories and the module has been
# aliased this will fail.
if test "$moddir" != "$mod"; then
moddir="./$moddir"
else
moddir="./"
fi
ln -sf ../../modules/$mod/lib/* $moddir
fi
cd ../..
}
#
# Install node binary <version> for <machine>.
#
install_node() {
local version=$1
local machine=$2
# download the source code and compile it.
if test $machine = "source"; then
local url="http://nodejs.org/dist/node-v$version.tar.gz"
http_code $url
if [ $? -eq 200 ]; then
mkdir -p $SRC/node-$version && \
cd $SRC/node-$version && \
log "building node-$version"
$GET $url | tar -xz --strip 1 && \
./configure --prefix=$ROOT 1> /dev/null 2> /dev/null && \
make 1> /dev/null && \
make install 1> /dev/null && \
cd $ROOT
else
abort "download node-$version source failed"
fi
# download a node binary
else
local url="$BIN_URL/$version-$machine"
http_code $url
if [ $? -eq 200 ]; then
log "installing node-$version-$machine"
$GET $url > bin/node && chmod 0755 bin/node
else
abort "download node-$version-$machine failed"
fi
fi
}
#
# Install node <version>, where version may
# be a semver such as "0.1.102", or "latest".
#
node() {
if test $2; then
local machine=$2
else
local machine=$(uname -m)
fi
if test $1 = "latest"; then
log "fetching latest node"
local latest=$($GET "$BIN_URL/latest" 2> /dev/null)
if test -f $ROOT/bin/node; then
local version=$($ROOT/bin/node --version)
if test $version = "v$latest"; then
log "already up to date"
else
log "updating $version to v$latest"
install_node $latest $machine
fi
else
install_node $latest $machine
fi
else
if test -f $ROOT/bin/node; then
local version=$($ROOT/bin/node --version)
if test "$version" = "v$1"; then
log "already installed node $1"
else
log "updating from node $version"
install_node $1 $machine
fi
else
install_node $1 $machine
fi
fi
}
#
# Install distro.
#
install() {
mkdir -p $ROOT/bin
if test -f $ROOT/.ndistro; then
. $ROOT/.ndistro
log "installation complete"
else
abort ".ndistro not found in this directory"
fi
}
#
# Install <user> <distro>
#
install_user_distro() {
local user=$1
local distro=$2
log "fetching $user $distro"
$GET "http://github.com/$user/.ndistro/raw/master/$distro" > .ndistro \
&& install
}
#
# List <user> distros.
#
list_user_distros() {
local user=$1
log "fetching $user distro list"
$GET "http://github.com/api/v2/yaml/blob/all/$user/.ndistro/master" \
2> /dev/null \
| tail -n +3 \
| cut -d ':' -f 1
}
#
# Output usage information.
#
usage() {
cat <<-HELP
Usage: ndistro [options] [cmd]
Commands:
edit Opens CWD/.ndistro in $EDITOR
update Update ndistro to the latest version
clean Clean distro to fresh state
<user> <distro> Installs the given <distro> from <user>
Options:
-V, --version Output version number
-h, --help Output help information
HELP
}
# Handle arguments
if test $# -eq 0; then
install
else
case $1 in
-h|--help)
usage && exit 1
;;
-V|--version)
echo $VERSION && exit 1
;;
update)
log "updating $0"
rm -fr /tmp/ndistro \
&& git clone --depth 1 -q git://github.com/visionmedia/ndistro.git /tmp/ndistro \
&& cp /tmp/ndistro/bin/ndistro $0 \
&& log "updated $VERSION to" $($0 --version) \
&& exit
;;
edit)
$EDITOR $ROOT/.ndistro
;;
clean)
if test -f $ROOT/.ndistro; then
log "cleaning build directories"
test -d bin && rm -fr bin
test -d include && rm -fr include
test -d lib && rm -fr lib
test -d modules && rm -fr modules
test -d share && rm -fr share
test -d src && rm -fr src
exit 0
else
abort ".ndistro not found in this directory"
fi
;;
*)
test $# -eq 1 && list_user_distros $1
test $# -eq 2 && install_user_distro $1 $2
;;
esac
fi