aboutsummaryrefslogtreecommitdiff
path: root/activator
diff options
context:
space:
mode:
Diffstat (limited to 'activator')
-rwxr-xr-xactivator334
1 files changed, 334 insertions, 0 deletions
diff --git a/activator b/activator
new file mode 100755
index 0000000..59c131b
--- /dev/null
+++ b/activator
@@ -0,0 +1,334 @@
1#!/bin/bash
2
3### ------------------------------- ###
4### Helper methods for BASH scripts ###
5### ------------------------------- ###
6
7realpath () {
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 if [ "$TARGET_FILE" == "." -o "$TARGET_FILE" == ".." ]; then
24 cd "$TARGET_FILE"
25 TARGET_FILEPATH=
26 else
27 TARGET_FILEPATH=/$TARGET_FILE
28 fi
29
30 # make sure we grab the actual windows path, instead of cygwin's path.
31 if ! is_cygwin; then
32 echo "$(pwd -P)/$TARGET_FILE"
33 else
34 echo $(cygwinpath "$(pwd -P)/$TARGET_FILE")
35 fi
36)
37}
38
39# TODO - Do we need to detect msys?
40
41# Uses uname to detect if we're in the odd cygwin environment.
42is_cygwin() {
43 local os=$(uname -s)
44 case "$os" in
45 CYGWIN*) return 0 ;;
46 *) return 1 ;;
47 esac
48}
49
50# This can fix cygwin style /cygdrive paths so we get the
51# windows style paths.
52cygwinpath() {
53 local file="$1"
54 if is_cygwin; then
55 echo $(cygpath -w $file)
56 else
57 echo $file
58 fi
59}
60
61# Make something URI friendly
62make_url() {
63 url="$1"
64 local nospaces=${url// /%20}
65 if is_cygwin; then
66 echo "/${nospaces//\\//}"
67 else
68 echo "$nospaces"
69 fi
70}
71
72# Detect if we should use JAVA_HOME or just try PATH.
73get_java_cmd() {
74 if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
75 echo "$JAVA_HOME/bin/java"
76 else
77 echo "java"
78 fi
79}
80
81echoerr () {
82 echo 1>&2 "$@"
83}
84vlog () {
85 [[ $verbose || $debug ]] && echoerr "$@"
86}
87dlog () {
88 [[ $debug ]] && echoerr "$@"
89}
90execRunner () {
91 # print the arguments one to a line, quoting any containing spaces
92 [[ $verbose || $debug ]] && echo "# Executing command line:" && {
93 for arg; do
94 if printf "%s\n" "$arg" | grep -q ' '; then
95 printf "\"%s\"\n" "$arg"
96 else
97 printf "%s\n" "$arg"
98 fi
99 done
100 echo ""
101 }
102
103 exec "$@"
104}
105addJava () {
106 dlog "[addJava] arg = '$1'"
107 java_args=( "${java_args[@]}" "$1" )
108}
109addApp () {
110 dlog "[addApp] arg = '$1'"
111 sbt_commands=( "${app_commands[@]}" "$1" )
112}
113addResidual () {
114 dlog "[residual] arg = '$1'"
115 residual_args=( "${residual_args[@]}" "$1" )
116}
117addDebugger () {
118 addJava "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
119}
120addConfigOpts () {
121 dlog "[addConfigOpts] arg = '$*'"
122 for item in $*
123 do
124 addJava "$item"
125 done
126}
127# a ham-fisted attempt to move some memory settings in concert
128# so they need not be messed around with individually.
129get_mem_opts () {
130 local mem=${1:-1024}
131 local meta=$(( $mem / 4 ))
132 (( $meta > 256 )) || meta=256
133 (( $meta < 1024 )) || meta=1024
134
135 # default is to set memory options but this can be overridden by code section below
136 memopts="-Xms${mem}m -Xmx${mem}m"
137 if [[ "${java_version}" > "1.8" ]]; then
138 extmemopts="-XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=${meta}m"
139 else
140 extmemopts="-XX:PermSize=64m -XX:MaxPermSize=${meta}m"
141 fi
142
143 if [[ "${java_opts}" == *-Xmx* ]] || [[ "${java_opts}" == *-Xms* ]] || [[ "${java_opts}" == *-XX:MaxPermSize* ]] || [[ "${java_opts}" == *-XX:ReservedCodeCacheSize* ]] || [[ "${java_opts}" == *-XX:MaxMetaspaceSize* ]]; then
144 # if we detect any of these settings in ${java_opts} we need to NOT output our settings.
145 # The reason is the Xms/Xmx, if they don't line up, cause errors.
146 memopts=""
147 extmemopts=""
148 fi
149
150 echo "${memopts} ${extmemopts}"
151}
152require_arg () {
153 local type="$1"
154 local opt="$2"
155 local arg="$3"
156 if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
157 die "$opt requires <$type> argument"
158 fi
159}
160is_function_defined() {
161 declare -f "$1" > /dev/null
162}
163
164# If we're *not* running in a terminal, and we don't have any arguments, then we need to add the 'ui' parameter
165detect_terminal_for_ui() {
166 [[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && {
167 addResidual "ui"
168 }
169 # SPECIAL TEST FOR MAC
170 [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && {
171 echo "Detected MAC OSX launched script...."
172 echo "Swapping to UI"
173 addResidual "ui"
174 }
175}
176
177# Processes incoming arguments and places them in appropriate global variables. called by the run method.
178process_args () {
179 while [[ $# -gt 0 ]]; do
180 case "$1" in
181 -h|-help) usage; exit 1 ;;
182 -v|-verbose) verbose=1 && shift ;;
183 -d|-debug) debug=1 && shift ;;
184 -mem) require_arg integer "$1" "$2" && app_mem="$2" && shift 2 ;;
185 -jvm-debug)
186 if echo "$2" | grep -E ^[0-9]+$ > /dev/null; then
187 addDebugger "$2" && shift
188 else
189 addDebugger 9999
190 fi
191 shift ;;
192 -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;
193 -D*) addJava "$1" && shift ;;
194 -J*) addJava "${1:2}" && shift ;;
195 *) addResidual "$1" && shift ;;
196 esac
197 done
198
199 is_function_defined process_my_args && {
200 myargs=("${residual_args[@]}")
201 residual_args=()
202 process_my_args "${myargs[@]}"
203 }
204}
205
206# Actually runs the script.
207run() {
208 # TODO - check for sane environment
209
210 # process the combined args, then reset "$@" to the residuals
211 process_args "$@"
212 detect_terminal_for_ui
213 set -- "${residual_args[@]}"
214 argumentCount=$#
215
216 #check for jline terminal fixes on cygwin
217 if is_cygwin; then
218 stty -icanon min 1 -echo > /dev/null 2>&1
219 addJava "-Djline.terminal=jline.UnixTerminal"
220 addJava "-Dsbt.cygwin=true"
221 fi
222
223 # run sbt
224 execRunner "$java_cmd" \
225 "-Dactivator.home=$(make_url "$activator_home")" \
226 $(get_mem_opts $app_mem) \
227 ${java_opts[@]} \
228 ${java_args[@]} \
229 -jar "$app_launcher" \
230 "${app_commands[@]}" \
231 "${residual_args[@]}"
232
233 local exit_code=$?
234 if is_cygwin; then
235 stty icanon echo > /dev/null 2>&1
236 fi
237 exit $exit_code
238}
239
240# Loads a configuration file full of default command line options for this script.
241loadConfigFile() {
242 cat "$1" | sed '/^\#/d'
243}
244
245### ------------------------------- ###
246### Start of customized settings ###
247### ------------------------------- ###
248usage() {
249 cat <<EOM
250Usage: $script_name <command> [options]
251
252 Command:
253 ui Start the Activator UI
254 new [name] [template-id] Create a new project with [name] using template [template-id]
255 list-templates Print all available template names
256 -h | -help Print this message
257
258 Options:
259 -v | -verbose Make this runner chattier
260 -d | -debug Set sbt log level to debug
261 -mem <integer> Set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem))
262 -jvm-debug <port> Turn on JVM debugging, open at the given port.
263
264 # java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
265 -java-home <path> Alternate JAVA_HOME
266
267 # jvm options and output control
268 -Dkey=val Pass -Dkey=val directly to the java runtime
269 -J-X Pass option -X directly to the java runtime
270 (-J is stripped)
271
272 # environment variables (read from context)
273 JAVA_OPTS Environment variable, if unset uses ""