-
Notifications
You must be signed in to change notification settings - Fork 8
/
getdeps.sh
executable file
·140 lines (122 loc) · 4 KB
/
getdeps.sh
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
#!/bin/sh
#
## getdeps.sh
#
# This script is included as part of NodeAtlas for retrieving static
# dependencies from content distribution networks (CDNs) or whatever
# permanent home they might have. The purpose of this is to keep them
# out of the Git repository, as per GitHub's recommendations on
# [repository size].
#
# When this script is run, a download tool will be chosen, which will
# default to either `curl` or `wget`, (in that order), depending on
# what is installed. If neither is installed, it will fail, unless a
# preferred command is specified.
#
# Dependencies should be listed in a file called `deps.txt` in the
# following format, one per line.
#
# file.ext http://example.com/downloads/file.min.ext
#
# If the file is downloaded successfully, it will be placed in the
# assets directory, which is `res/web/assets`.
#
# If a dependency cannot be retrieved, its intended filename will be
# given on `stdout` with a warning that it could not be downloaded,
# and the script will continue. When it exits, however, and at least
# one dependency could not be retrieved, it will exit with status 3.
#
# If there is any other sort of error, it will exit with status 1.
#
# [repository size]: http://git.io/2uKFOw
#
#############
# Variables #
#############
DOWNLOADER=
#############
# Constants #
#############
DEFAULT_ASSETSDIR='res/web/assets'
DEFAULT_DEPFILE='depslist'
DEFAULT_DOWNLOADERS='curl -s -o,wget -qO'
COLOR_RED="\033[1;31m"
COLOR_DEFAULT="\033[0m"
#########################
# On to the actual code #
#########################
# Test if the dependency list is available and readable.
if [ ! -f "$DEFAULT_DEPFILE" ]; then
echo "$DEFAULT_DEPFILE is not a regular file - exiting"
exit 1
fi
# Select the first appropriate downloader, but only if DOWNLOADER is
# not specified.
if [ -z "$DOWNLOADER" ]; then
# Set the IFS so that we only split the list on commas, but make
# sure we can restore the old IFS. Note that DEFAULT_DOWNLOADERS
# is not quoted in the for loop declaration.
OLDIFS="$IFS"
IFS=","
for command in $DEFAULT_DOWNLOADERS; do
# Check if the first word of the given download command is on
# the system path. If so, choose it and break from the loop.
which "$(echo $command | cut -d\ -f1)" > /dev/null
if [ "$?" -eq 0 ]; then
echo "Using $command as preferred downloader"
DOWNLOADER="$command"
break
fi
done
# Restore the old IFS so that we don't make hard to find problems
# later.
IFS="$OLDIFS"
# If we reach this point, and the DOWNLOADER is still not set, we
# couldn't find an appropriate downloader, and we should quit.
if [ -z "$DOWNLOADER" ]; then
echo "Could not find an appropriate downloader"
echo "Please specify one in $0"
exit 1
fi
fi
# RETRIEVE_ALL will be used to record the success status.
RETRIEVE_ALL=0
# Once the downloader has been selected, loop through the file line by
# line.
i=0
successes=0
while read outfile url; do
i=$(($i + 1))
# If either field is missing, explain that it is misformatted and
# denote the failure, but continue.
if [ -z "$outfile" -o -z "$url" ]; then
echo " $COLOR_RED MISFORMATTED$COLOR_DEFAULT (line $i)"
RETRIEVE_ALL=1
continue
fi
# Report the status as we go.
echo -n " $outfile..."
# The downloader should function such that '$DOWNLOADER
# $DEFAULT_ASSETSDIR/$outfile $url' will retrieve a file at $url
# and place it in the $DEFAULT_ASSETSDIR at $outfile.
$DOWNLOADER "$DEFAULT_ASSETSDIR/$outfile" "$url" 1>&2
# Check the exit status from the $DOWNLOADER, and make sure it's
# zero. If it's not, denote the failure and continue.
if [ "$?" -ne 0 ]; then
echo " $COLOR_RED FAILED$COLOR_DEFAULT (line $i)"
RETRIEVE_ALL=1
continue
fi
# If all is well, state the success.
echo " downloaded"
successes=$(($successes + 1))
done < "$DEFAULT_DEPFILE"
# Report the status at the end, and exit with an appropriate code.
if [ "$RETRIEVE_ALL" -eq 0 ]; then
echo "All dependencies downloaded: $i"
exit 0
else
echo "Dependencies downloaded: $successes (of $i)"
echo "Errors were encountered during retrieval"
exit 3
fi