1
+ #! /bin/bash
2
+
3
+ # ## ------------------------------- ###
4
+ # ## Helper methods for BASH scripts ###
5
+ # ## ------------------------------- ###
6
+
7
+ realpath () {
8
+ (
9
+ TARGET_FILE=" $1 "
10
+
11
+ cd $( dirname " $TARGET_FILE " )
12
+ TARGET_FILE=$( basename " $TARGET_FILE " )
13
+
14
+ COUNT=0
15
+ while [ -L " $TARGET_FILE " -a $COUNT -lt 100 ]
16
+ do
17
+ TARGET_FILE=$( readlink " $TARGET_FILE " )
18
+ cd $( dirname " $TARGET_FILE " )
19
+ TARGET_FILE=$( basename " $TARGET_FILE " )
20
+ COUNT=$(( $COUNT + 1 ))
21
+ done
22
+
23
+ # make sure we grab the actual windows path, instead of cygwin's path.
24
+ echo $( cygwinpath " $( pwd -P) /$TARGET_FILE " )
25
+ )
26
+ }
27
+
28
+ # TODO - Do we need to detect msys?
29
+
30
+ # Uses uname to detect if we're in the odd cygwin environment.
31
+ is_cygwin () {
32
+ local os=$( uname -s)
33
+ case " $os " in
34
+ CYGWIN* ) return 0 ;;
35
+ * ) return 1 ;;
36
+ esac
37
+ }
38
+
39
+ # This can fix cygwin style /cygdrive paths so we get the
40
+ # windows style paths.
41
+ cygwinpath () {
42
+ local file=" $1 "
43
+ if is_cygwin; then
44
+ echo $( cygpath -w $file )
45
+ else
46
+ echo $file
47
+ fi
48
+ }
49
+
50
+ # Make something URI friendly
51
+ make_url () {
52
+ url=" $1 "
53
+ local nospaces=${url// /% 20}
54
+ if is_cygwin; then
55
+ echo " /${nospaces// \\ // } "
56
+ else
57
+ echo " $nospaces "
58
+ fi
59
+ }
60
+
61
+ # Detect if we should use JAVA_HOME or just try PATH.
62
+ get_java_cmd () {
63
+ if [[ -n " $JAVA_HOME " ]] && [[ -x " $JAVA_HOME /bin/java" ]]; then
64
+ echo " $JAVA_HOME /bin/java"
65
+ else
66
+ echo " java"
67
+ fi
68
+ }
69
+
70
+ echoerr () {
71
+ echo 1>&2 " $@ "
72
+ }
73
+ vlog () {
74
+ [[ $verbose || $debug ]] && echoerr " $@ "
75
+ }
76
+ dlog () {
77
+ [[ $debug ]] && echoerr " $@ "
78
+ }
79
+ execRunner () {
80
+ # print the arguments one to a line, quoting any containing spaces
81
+ [[ $verbose || $debug ]] && echo " # Executing command line:" && {
82
+ for arg; do
83
+ if printf " %s\n" " $arg " | grep -q ' ' ; then
84
+ printf " \" %s\" \n" " $arg "
85
+ else
86
+ printf " %s\n" " $arg "
87
+ fi
88
+ done
89
+ echo " "
90
+ }
91
+
92
+ exec " $@ "
93
+ }
94
+ addJava () {
95
+ dlog " [addJava] arg = '$1 '"
96
+ java_args=( " ${java_args[@]} " " $1 " )
97
+ }
98
+ addApp () {
99
+ dlog " [addApp] arg = '$1 '"
100
+ sbt_commands=( " ${app_commands[@]} " " $1 " )
101
+ }
102
+ addResidual () {
103
+ dlog " [residual] arg = '$1 '"
104
+ residual_args=( " ${residual_args[@]} " " $1 " )
105
+ }
106
+ addDebugger () {
107
+ addJava " -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1 "
108
+ }
109
+ # a ham-fisted attempt to move some memory settings in concert
110
+ # so they need not be messed around with individually.
111
+ get_mem_opts () {
112
+ local mem=${1:- 1024}
113
+ local perm=$(( $mem / 4 ))
114
+ (( $perm > 256 )) || perm=256
115
+ (( $perm < 1024 )) || perm=1024
116
+ local codecache=$(( $perm / 2 ))
117
+
118
+ echo " -Xms${mem} m -Xmx${mem} m -XX:MaxPermSize=${perm} m -XX:ReservedCodeCacheSize=${codecache} m"
119
+ }
120
+ require_arg () {
121
+ local type=" $1 "
122
+ local opt=" $2 "
123
+ local arg=" $3 "
124
+ if [[ -z " $arg " ]] || [[ " ${arg: 0: 1} " == " -" ]]; then
125
+ die " $opt requires <$type > argument"
126
+ fi
127
+ }
128
+ require_arg () {
129
+ local type=" $1 "
130
+ local opt=" $2 "
131
+ local arg=" $3 "
132
+ if [[ -z " $arg " ]] || [[ " ${arg: 0: 1} " == " -" ]]; then
133
+ die " $opt requires <$type > argument"
134
+ fi
135
+ }
136
+ is_function_defined () {
137
+ declare -f " $1 " > /dev/null
138
+ }
139
+
140
+ # Attempt to detect if the script is running via a GUI or not
141
+ # TODO - Determine where/how we use this generically
142
+ detect_terminal_for_ui () {
143
+ [[ ! -t 0 ]] && [[ " ${# residual_args} " == " 0" ]] && {
144
+ echo " true"
145
+ }
146
+ # SPECIAL TEST FOR MAC
147
+ [[ " $( uname) " == " Darwin" ]] && [[ " $HOME " == " $PWD " ]] && [[ " ${# residual_args} " == " 0" ]] && {
148
+ echo " true"
149
+ }
150
+ }
151
+
152
+ # Processes incoming arguments and places them in appropriate global variables. called by the run method.
153
+ process_args () {
154
+ while [[ $# -gt 0 ]]; do
155
+ case " $1 " in
156
+ -h|-help) usage; exit 1 ;;
157
+ -v|-verbose) verbose=1 && shift ;;
158
+ -d|-debug) debug=1 && shift ;;
159
+
160
+ -mem) require_arg integer " $1 " " $2 " && app_mem=" $2 " && shift 2 ;;
161
+ -jvm-debug) require_arg port " $1 " " $2 " && addDebugger $2 && shift 2 ;;
162
+
163
+ -java-home) require_arg path " $1 " " $2 " && java_cmd=" $2 /bin/java" && shift 2 ;;
164
+
165
+ -D* ) addJava " $1 " && shift ;;
166
+ -J* ) addJava " ${1: 2} " && shift ;;
167
+ * ) addResidual " $1 " && shift ;;
168
+ esac
169
+ done
170
+
171
+ is_function_defined process_my_args && {
172
+ myargs=(" ${residual_args[@]} " )
173
+ residual_args=()
174
+ process_my_args " ${myargs[@]} "
175
+ }
176
+ }
177
+
178
+ # Actually runs the script.
179
+ run () {
180
+ # TODO - check for sane environment
181
+
182
+ # process the combined args, then reset "$@" to the residuals
183
+ process_args " $@ "
184
+ set -- " ${residual_args[@]} "
185
+ argumentCount=$#
186
+
187
+ # check for jline terminal fixes on cygwin
188
+ if is_cygwin; then
189
+ stty -icanon min 1 -echo > /dev/null 2>&1
190
+ addJava " -Djline.terminal=jline.UnixTerminal"
191
+ addJava " -Dsbt.cygwin=true"
192
+ fi
193
+ # run sbt
194
+ execRunner " $java_cmd " \
195
+ $( get_mem_opts $app_mem ) \
196
+ ${java_opts} \
197
+ ${java_args[@]} \
198
+ -cp " $app_classpath " \
199
+ $app_mainclass \
200
+ " ${app_commands[@]} " \
201
+ " ${residual_args[@]} "
202
+
203
+ local exit_code=$?
204
+ if is_cygwin; then
205
+ stty icanon echo > /dev/null 2>&1
206
+ fi
207
+ exit $exit_code
208
+ }
209
+
210
+ # Loads a configuration file full of default command line options for this script.
211
+ loadConfigFile () {
212
+ cat " $1 " | sed ' /^\#/d'
213
+ }
214
+
215
+ # ## ------------------------------- ###
216
+ # ## Start of customized settings ###
217
+ # ## ------------------------------- ###
218
+ usage () {
219
+ cat << EOM
220
+ Usage: $script_name [options]
221
+
222
+ -h | -help print this message
223
+ -v | -verbose this runner is chattier
224
+ -d | -debug set sbt log level to debug
225
+ -mem <integer> set memory options (default: $sbt_mem , which is $( get_mem_opts $sbt_mem ) )
226
+ -jvm-debug <port> Turn on JVM debugging, open at the given port.
227
+
228
+ # java version (default: java from PATH, currently $( java -version 2>&1 | grep version) )
229
+ -java-home <path> alternate JAVA_HOME
230
+
231
+ # jvm options and output control
232
+ JAVA_OPTS environment variable, if unset uses "$java_opts "
233
+ -Dkey=val pass -Dkey=val directly to the java runtime
234
+ -J-X pass option -X directly to the java runtime
235
+ (-J is stripped)
236
+
237
+ In the case of duplicated or conflicting options, the order above
238
+ shows precedence: JAVA_OPTS lowest, command line options highest.
239
+ EOM
240
+ }
241
+
242
+ # ## ------------------------------- ###
243
+ # ## Main script ###
244
+ # ## ------------------------------- ###
245
+
246
+ declare -a residual_args
247
+ declare -a java_args
248
+ declare -a app_commands
249
+ declare -r app_home=" $( realpath " $( dirname " $0 " ) " ) "
250
+ # TODO - Check whether this is ok in cygwin...
251
+ declare -r lib_dir=" ${app_home} /../lib"
252
+ ${{template_declares} }
253
+ declare -r java_cmd=$( get_java_cmd)
254
+
255
+ # Now check to see if it's a good enough version
256
+ # TODO - Check to see if we have a configured default java version, otherwise use 1.6
257
+ declare -r java_version=$( " $java_cmd " -version 2>&1 | awk -F ' "' ' /version/ {print $2}' )
258
+ if [[ " $java_version " == " " ]]; then
259
+ echo
260
+ echo No java installations was detected.
261
+ echo Please go to http://www.java.com/getjava/ and download
262
+ echo
263
+ exit 1
264
+ elif [[ ! " $java_version " > " 1.6" ]]; then
265
+ echo
266
+ echo The java installation you have is not up to date
267
+ echo $app_name requires at least version 1.6+, you have
268
+ echo version $java_version
269
+ echo
270
+ echo Please go to http://www.java.com/getjava/ and download
271
+ echo a valid Java Runtime and install before running $app_name .
272
+ echo
273
+ exit 1
274
+ fi
275
+
276
+
277
+ # if configuration files exist, prepend their contents to $@ so it can be processed by this runner
278
+ [[ -f " $script_conf_file " ]] && set -- $( loadConfigFile " $script_conf_file " ) " $@ "
279
+
280
+ run " $@ "
0 commit comments