-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcompile
executable file
·247 lines (201 loc) · 7.59 KB
/
compile
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
#!/usr/bin/env bash
set -e # fail fast
set -o pipefail # don't ignore exit codes when piping output
# set -x # enable debugging
# Enable extended globbing
shopt -s extglob
# Configure directories
cache_dir=$2
env_dir=$3
bp_dir=$(cd $(dirname $0); cd ..; pwd)
# Option [nocl] to disable cleanup for testing
cleanup=$4
# Load some convenience functions like status(), echo(), and indent()
source $bp_dir/bin/common.sh
# Load user config
if [ -e $1/.client ]; then
status "Load config from .client"
source $1/.client
fi
# Client build directory
build_dir=$1/${CLIENT_DIR:-"client"}
# Grunt / Gulp task name
dist_dir=${DIST_DIR:-dist}
status "Client dir: ${CLIENT_DIR:-client}"
status "Dist dir: $dist_dir"
# Fix leak
status "Resetting git environment"
unset GIT_DIR
# Ignore NODE_ENV
unset NODE_ENV
# Output npm debug info on error
trap cat_npm_debug_log ERR
# Create default package.json
if [ ! -e $build_dir/package.json ]; then
status "No package.json found; Adding grunt to new package.json"
cat <<- EOF > $build_dir/package.json
{
"name": "client",
"version": "0.0.1",
"description": "web client",
"private": true,
"devDependencies": {
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13"
},
"engines": {
"node": "~0.10.0"
}
}
EOF
fi
# Add grunt and bower dependecies to package.json to cache them
status "Augmenting package.json with grunt and grunt-cli"
$bp_dir/bin/devdeps.py $build_dir/package.json "grunt" "~0.4.5"
$bp_dir/bin/devdeps.py $build_dir/package.json "grunt-cli" "~0.1.13"
if [ -e $build_dir/bower.json ]; then
status "Augmenting package.json with bower"
$bp_dir/bin/devdeps.py $build_dir/package.json "bower" "~1.3.12"
fi
# Look in package.json's engines.node field for a semver range
semver_range=$(cat $build_dir/package.json | $bp_dir/vendor/jq -r .engines.node)
# Resolve node version using semver.io
node_version=$(curl --silent --get --data-urlencode "range=${semver_range}" https://semver.io/node/resolve)
# Recommend using semver ranges in a safe manner
if [ "$semver_range" == "null" ]; then
protip "Specify a node version in package.json"
semver_range=""
elif [ "$semver_range" == "*" ]; then
protip "Avoid using semver ranges like '*' in engines.node"
elif [ ${semver_range:0:1} == ">" ]; then
protip "Avoid using semver ranges starting with '>' in engines.node"
fi
# Output info about requested range and resolved node version
if [ "$semver_range" == "" ]; then
status "Defaulting to latest stable node: $node_version"
else
status "Requested node range: $semver_range"
status "Resolved node version: $node_version"
fi
# Download node from Heroku's S3 mirror of nodejs.org/dist
status "Downloading and installing node"
node_url="http://s3pository.heroku.com/node/v$node_version/node-v$node_version-linux-x64.tar.gz"
curl $node_url -s -o - | tar xzf - -C $build_dir
# Move node (and npm) into ./vendor and make them executable
mkdir -p $build_dir/vendor
mv $build_dir/node-v$node_version-linux-x64 $build_dir/vendor/node
chmod +x $build_dir/vendor/node/bin/*
PATH=$build_dir/vendor/node/bin:$PATH
# Run subsequent node/npm commands from the build path
cd $build_dir
# Install npm@next
status "Installing the 'next' version of npm"
npm install -g npm@next 2>&1 | indent
npm_version=$(npm -v)
status "Using npm version: $npm_version"
# If node_modules directory is checked into source control then
# rebuild any native deps. Otherwise, restore from the build cache.
if test -d $build_dir/node_modules; then
status "Found existing node_modules directory; skipping cache"
status "Rebuilding any native dependencies"
npm rebuild 2>&1 | indent
elif test -d $cache_dir/node/node_modules; then
status "Restoring node_modules directory from cache"
cp -r $cache_dir/node/node_modules $build_dir/
status "Pruning cached dependencies not specified in package.json"
npm prune 2>&1 | indent
if test -f $cache_dir/node/.heroku/node-version && [ $(cat $cache_dir/node/.heroku/node-version) != "$node_version" ]; then
status "Node version changed since last build; rebuilding dependencies"
npm rebuild 2>&1 | indent
fi
fi
# Scope config var availability only to `npm install`
(
if [ -d "$env_dir" ]; then
status "Exporting config vars to environment"
export_env_dir $env_dir
fi
status "Installing dependencies"
# Make npm output to STDOUT instead of its default STDERR
npm install --userconfig $build_dir/.npmrc 2>&1 | indent
)
# Persist goodies like node-version in the slug
mkdir -p $build_dir/.heroku
# Save resolved node version in the slug for later reference
echo $node_version > $build_dir/.heroku/node-version
# Purge node-related cached content, being careful not to purge the top-level
# cache, for the sake of heroku-buildpack-multi apps.
rm -rf $cache_dir/node_modules # (for apps still on the older caching strategy)
rm -rf $cache_dir/node
mkdir -p $cache_dir/node
# If app has a node_modules directory, cache it.
if test -d $build_dir/node_modules; then
status "Caching node_modules directory for future builds"
cp -r $build_dir/node_modules $cache_dir/node
fi
# Copy goodies to the cache
cp -r $build_dir/.heroku $cache_dir/node
status "Cleaning up node-gyp and npm artifacts"
rm -rf "$build_dir/.node-gyp"
rm -rf "$build_dir/.npm"
# Install compass
status "Installing Compass"
RUBY_VERSION=$(ruby -e "require 'rbconfig';puts \"#{RUBY_ENGINE}/#{RbConfig::CONFIG['ruby_version']}\"")
export GEM_HOME=$build_dir/.gem/$RUBY_VERSION
PATH="$GEM_HOME/bin:$PATH"
# Test if gem home exists and matches current ruby version
if test -d $cache_dir/ruby/.gem/$RUBY_VERSION; then
status "Restoring ruby gems directory from cache"
cp -r $cache_dir/ruby/.gem $build_dir
HOME=$build_dir gem update compass --user-install --no-rdoc --no-ri 2>&1 | indent
# Try to run compass - reinstall if it fails
if compass -v >/dev/null 2>&1; then
status "Compass ok"
else
status "Reinstalling compass"
HOME=$build_dir gem uninstall compass --user-install 2>&1 | indent
HOME=$build_dir gem install compass --user-install --no-rdoc --no-ri 2>&1 | indent
fi
else
HOME=$build_dir gem install compass --user-install --no-rdoc --no-ri 2>&1 | indent
fi
# Cache ruby gems
rm -rf $cache_dir/ruby
mkdir -p $cache_dir/ruby
# If app has a gems directory, cache it.
if test -d $build_dir/.gem; then
status "Caching ruby gems directory for future builds"
cp -r $build_dir/.gem $cache_dir/ruby
fi
# Install bower dependencies
if [ -e $build_dir/bower.json ]; then
status "Install bower dependencies"
if test -d $cache_dir/.bowercache; then
status "Restoring bower components from cache"
cp -r $cache_dir/.bowercache $build_dir
HOME=$build_dir $build_dir/node_modules/.bin/bower install --config.storage.packages=$build_dir/.bowercache 2>&1 | indent
else
HOME=$build_dir $build_dir/node_modules/.bin/bower install --config.storage.packages=$build_dir/.bowercache 2>&1 | indent
fi
# Cache bower
rm -rf $cache_dir/.bowercache
mkdir -p $cache_dir/.bowercache
# If app has a bower directory, cache it.
if test -d $build_dir/.bowercache; then
status "Caching bower cache directory for future builds"
cp -r $build_dir/.bowercache $cache_dir
fi
fi
# Run grunt
status "Running grunt build task"
$build_dir/node_modules/.bin/grunt build 2>&1 | indent
# Update the PATH
status "Building runtime environment"
mkdir -p $build_dir/.profile.d
echo "export PATH=\"\$HOME/vendor/node/bin:\$HOME/bin:\$HOME/node_modules/.bin:\$PATH\";" > $build_dir/.profile.d/nodejs.sh
echo "export PATH=\"\$HOME/.gem/$RUBY_VERSION/bin:\$PATH\"" > $build_dir/.profile.d/ruby.sh
# Cleanup
if [ "$cleanup" != "nocl" ]; then
status "Cleanup"
rm -rf !(${dist_dir})
fi