-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathcommon.sh
338 lines (288 loc) · 8.57 KB
/
common.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
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
#!/usr/bin/env bash
export BUILDPACK_STDLIB_URL="https://lang-common.s3.amazonaws.com/buildpack-stdlib/v7/stdlib.sh"
SBT_0_VERSION_PATTERN='sbt\.version=\(0\.1[1-3]\.[0-9]*\(-[a-zA-Z0-9_]*\)*\)$'
SBT_1_VERSION_PATTERN='sbt\.version=\(1\.[0-9]*\.[0-9]*\(-[a-zA-Z0-9_]*\)*\)$'
## SBT 0.10 allows either *.sbt in the root dir, or project/*.scala or .sbt/*.scala
detect_sbt() {
local ctxDir=$1
if _has_sbtFile $ctxDir || \
_has_projectScalaFile $ctxDir || \
_has_hiddenSbtDir $ctxDir || \
_has_buildPropertiesFile $ctxDir ; then
return 0
else
return 1
fi
}
is_play() {
_has_playConfig $1
}
is_sbt_native_packager() {
local ctxDir=$1
if [ -e "${ctxDir}"/project/plugins.sbt ]; then
pluginVersionLine="$(grep "addSbtPlugin(.\+sbt-native-packager" "${ctxDir}"/project/plugins.sbt)"
test -n "$pluginVersionLine"
else
return 1
fi
}
_has_sbtFile() {
local ctxDir=$1
test -n "$(find $ctxDir -maxdepth 1 -name '*.sbt' -print -quit)"
}
_has_projectScalaFile() {
local ctxDir=$1
test -d $ctxDir/project && test -n "$(find $ctxDir/project -maxdepth 1 -name '*.scala' -print -quit)"
}
_has_hiddenSbtDir() {
local ctxDir=$1
test -d $ctxDir/.sbt && test -n "$(find $ctxDir/.sbt -maxdepth 1 -name '*.scala' -print -quit)"
}
_has_buildPropertiesFile() {
local ctxDir=$1
test -e $ctxDir/project/build.properties
}
_has_playConfig() {
local ctxDir=$1
test -e $ctxDir/conf/application.conf ||
test "$IS_PLAY_APP" = "true" ||
(test -n "$PLAY_CONF_FILE" &&
test -e "$PLAY_CONF_FILE" &&
test "$IS_PLAY_APP" != "false") ||
(# test for default Play 2.3 and 2.4 setup.
test -d $ctxDir/project &&
test -r $ctxDir/project/plugins.sbt &&
test -n "$(grep "addSbtPlugin(\"com.typesafe.play\" % \"sbt-plugin\"" $ctxDir/project/plugins.sbt | grep -v ".*//.*addSbtPlugin")" &&
test -r $ctxDir/build.sbt &&
test -n "$(grep "enablePlugins(Play" $ctxDir/build.sbt | grep -v ".*//.*enablePlugins(Play")" &&
test "$IS_PLAY_APP" != "false")
}
_has_playPluginsFile() {
local ctxDir=$1
test -e $ctxDir/project/plugins.sbt
}
get_scala_version() {
local ctxDir=$1
local sbtUserHome=$2
local launcher=$3
local playVersion=$4
if [ -n "${playVersion}" ]; then
if [ "${playVersion}" = "2.3" ] || [ "${playVersion}" = "2.4" ]; then
# if we don't grep for the version, and instead use `sbt scala-version`,
# then sbt will try to download the internet
scalaVersionLine="$(grep "scalaVersion" "${ctxDir}"/build.sbt | sed -E -e 's/[ \t\r\n]//g')"
scalaVersion=$(expr "$scalaVersionLine" : ".\+\(2\.1[0-1]\)\.[0-9]")
if [ -n "${scalaVersion}" ]; then
echo "$scalaVersion"
else
echo "2.10"
fi
elif [ "${playVersion}" = "2.2" ]; then
echo '2.10'
elif [ "${playVersion}" = "2.1" ]; then
echo '2.10'
elif [ "${playVersion}" = "2.0" ]; then
echo '2.9'
else
echo ''
fi
else
echo ''
fi
}
get_supported_play_version() {
local ctxDir=$1
local sbtUserHome=$2
local launcher=$3
if _has_playPluginsFile $ctxDir; then
pluginVersionLine="$(grep "addSbtPlugin(.\+play.\+sbt-plugin" "${ctxDir}"/project/plugins.sbt | sed -E -e 's/[ \t\r\n]//g')"
pluginVersion=$(expr "$pluginVersionLine" : ".\+\(2\.[0-4]\)\.[0-9]")
if [ "$pluginVersion" != 0 ]; then
echo -n "$pluginVersion"
fi
fi
echo ""
}
get_supported_sbt_version() {
local ctxDir=$1
local sbtVersionPattern=${2:-$SBT_0_VERSION_PATTERN}
if _has_buildPropertiesFile $ctxDir; then
sbtVersionLine="$(grep -P '[ \t]*sbt\.version[ \t]*=' "${ctxDir}"/project/build.properties | sed -E -e 's/[ \t\r\n]//g')"
sbtVersion=$(expr "$sbtVersionLine" : "$sbtVersionPattern")
if [ "$sbtVersion" != 0 ] ; then
echo "$sbtVersion"
else
echo ""
fi
else
echo ""
fi
}
prime_ivy_cache() {
local ctxDir=$1
local sbtUserHome=$2
local launcher=$3
if is_play $ctxDir ; then
playVersion=`get_supported_play_version ${BUILD_DIR} ${sbtUserHome} ${launcher}`
fi
scalaVersion=$(get_scala_version "$ctxDir" "$sbtUserHome" "$launcher" "$playVersion")
if [ -n "$scalaVersion" ]; then
cachePkg=" (Scala-${scalaVersion}"
if [ -n "$playVersion" ]; then
cachePkg="${cachePkg}, Play-${playVersion}"
fi
cachePkg="${cachePkg})"
fi
status_pending "Priming Ivy cache${cachePkg}"
if _download_and_unpack_ivy_cache "$sbtUserHome" "$scalaVersion" "$playVersion"; then
status_done
else
echo " no cache found"
fi
}
_download_and_unpack_ivy_cache() {
local sbtUserHome=$1
local scalaVersion=$2
local playVersion=$3
baseUrl="https://lang-jvm.s3.amazonaws.com/sbt/v8/sbt-cache"
if [ -n "$playVersion" ]; then
ivyCacheUrl="$baseUrl-play-${playVersion}_${scalaVersion}.tar.gz"
else
ivyCacheUrl="$baseUrl-base.tar.gz"
fi
curl --retry 3 --silent --max-time 60 --location $ivyCacheUrl | tar xzm -C $sbtUserHome
if [ $? -eq 0 ]; then
mv $sbtUserHome/.sbt/* $sbtUserHome
rm -rf $sbtUserHome/.sbt
return 0
else
return 1
fi
}
has_supported_sbt_version() {
local ctxDir=$1
local supportedVersion="$(get_supported_sbt_version ${ctxDir} ${SBT_0_VERSION_PATTERN})"
if [ -n "$supportedVersion" ] ; then
return 0
else
return 1
fi
}
has_supported_sbt_1_version() {
local ctxDir=$1
local supportedVersion="$(get_supported_sbt_version ${ctxDir} ${SBT_1_VERSION_PATTERN})"
if [ -n "$supportedVersion" ] ; then
return 0
else
return 1
fi
}
has_old_preset_sbt_opts() {
if [ "$SBT_OPTS" = "-Xmx384m -Xss512k -XX:+UseCompressedOops" ]; then
return 0
else
return 1
fi
}
count_files() {
local location=$1
local pattern=$2
if [ -d ${location} ]; then
find ${location} -name ${pattern} | wc -l | sed 's/ //g'
else
echo "0"
fi
}
detect_play_lang() {
local appDir=$1/app
local num_scala_files=$(count_files ${appDir} '*.scala')
local num_java_files=$(count_files ${appDir} '*.java')
if [ ${num_scala_files} -gt ${num_java_files} ] ; then
echo "Scala"
elif [ ${num_scala_files} -lt ${num_java_files} ] ; then
echo "Java"
else
echo ""
fi
}
is_app_dir() {
test "$1" != "/app"
}
uses_universal_packaging() {
local ctxDir=$1
test -d $ctxDir/target/universal/stage/bin
}
_universal_packaging_procs() {
local ctxDir=$1
(cd $ctxDir; find target/universal/stage/bin -type f -executable)
}
_universal_packaging_proc_count() {
local ctxDir=$1
_universal_packaging_procs $ctxDir | wc -l
}
universal_packaging_default_web_proc() {
local ctxDir=$1
if [ $(_universal_packaging_proc_count $ctxDir) -eq 1 ]; then
echo "web: $(_universal_packaging_procs $ctxDir) -Dhttp.port=\$PORT"
fi
}
# sed -l basically makes sed replace and buffer through stdin to stdout
# so you get updates while the command runs and dont wait for the end
# e.g. sbt stage | indent
output() {
local logfile="$1"
local c='s/^/ /'
case $(uname) in
Darwin) tee -a "$logfile" | sed -l "$c";; # mac/bsd sed: -l buffers on line boundaries
*) tee -a "$logfile" | sed -u "$c";; # unix/gnu sed: -u unbuffered (arbitrary) chunks of data
esac
}
install_sbt_extras() {
local optDir=${1}
local sbtBinDir=${2}
rm -f ${sbtBinDir}/sbt-launch*.jar #legacy launcher
mkdir -p ${sbtBinDir}
cp -p ${optDir}/sbt-extras.sh ${sbtBinDir}/sbt-extras
cp -p ${optDir}/sbt-wrapper.sh ${sbtBinDir}/sbt
chmod 0755 ${sbtBinDir}/sbt-extras
chmod 0755 ${sbtBinDir}/sbt
export PATH="${sbtBinDir}:$PATH"
}
run_sbt() {
local javaVersion=$1
local home=$2
local launcher=$3
local tasks=$4
local buildLogFile=".heroku/sbt-build.log"
echo "" > $buildLogFile
status "Running: sbt $tasks"
SBT_HOME="$home" sbt ${tasks} | output $buildLogFile
if [ "${PIPESTATUS[*]}" != "0 0" ]; then
handle_sbt_errors $buildLogFile
fi
}
cache_copy() {
rel_dir=$1
from_dir=$2
to_dir=$3
rm -rf $to_dir/$rel_dir
if [ -d $from_dir/$rel_dir ]; then
mkdir -p $to_dir/$rel_dir
cp -pr $from_dir/$rel_dir/. $to_dir/$rel_dir
fi
}
install_jdk() {
local install_dir=${1:?}
local cache_dir=${2:?}
let start=$(nowms)
JVM_COMMON_BUILDPACK=${JVM_COMMON_BUILDPACK:-https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/jvm.tgz}
mkdir -p /tmp/jvm-common
curl --retry 3 --silent --location $JVM_COMMON_BUILDPACK | tar xzm -C /tmp/jvm-common --strip-components=1
source /tmp/jvm-common/bin/util
source /tmp/jvm-common/bin/java
source /tmp/jvm-common/opt/jdbc.sh
mtime "jvm-common.install.time" "${start}"
let start=$(nowms)
install_java_with_overlay "${install_dir}" "${cache_dir}"
mtime "jvm.install.time" "${start}"
}