|
| 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 | +# If we're *not* running in a terminal, and we don't have any arguments, then we need to add the 'ui' parameter |
| 141 | +detect_terminal_for_ui() { |
| 142 | + [[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && { |
| 143 | + addResidual "ui" |
| 144 | + } |
| 145 | + # SPECIAL TEST FOR MAC |
| 146 | + [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && { |
| 147 | + echo "Detected MAC OSX launched script...." |
| 148 | + echo "Swapping to UI" |
| 149 | + addResidual "ui" |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +# Processes incoming arguments and places them in appropriate global variables. called by the run method. |
| 154 | +process_args () { |
| 155 | + while [[ $# -gt 0 ]]; do |
| 156 | + case "$1" in |
| 157 | + -h|-help) usage; exit 1 ;; |
| 158 | + -v|-verbose) verbose=1 && shift ;; |
| 159 | + -d|-debug) debug=1 && shift ;; |
| 160 | + |
| 161 | + -mem) require_arg integer "$1" "$2" && app_mem="$2" && shift 2 ;; |
| 162 | + -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; |
| 163 | + |
| 164 | + -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;; |
| 165 | + |
| 166 | + -D*) addJava "$1" && shift ;; |
| 167 | + -J*) addJava "${1:2}" && shift ;; |
| 168 | + *) addResidual "$1" && shift ;; |
| 169 | + esac |
| 170 | + done |
| 171 | + |
| 172 | + is_function_defined process_my_args && { |
| 173 | + myargs=("${residual_args[@]}") |
| 174 | + residual_args=() |
| 175 | + process_my_args "${myargs[@]}" |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +# Actually runs the script. |
| 180 | +run() { |
| 181 | + # TODO - check for sane environment |
| 182 | + |
| 183 | + # process the combined args, then reset "$@" to the residuals |
| 184 | + process_args "$@" |
| 185 | + detect_terminal_for_ui |
| 186 | + set -- "${residual_args[@]}" |
| 187 | + argumentCount=$# |
| 188 | + |
| 189 | + #check for jline terminal fixes on cygwin |
| 190 | + if is_cygwin; then |
| 191 | + stty -icanon min 1 -echo > /dev/null 2>&1 |
| 192 | + addJava "-Djline.terminal=jline.UnixTerminal" |
| 193 | + addJava "-Dsbt.cygwin=true" |
| 194 | + fi |
| 195 | + # run sbt |
| 196 | + execRunner "$java_cmd" \ |
| 197 | + "-Dactivator.home=$(make_url "$activator_home")" \ |
| 198 | + $(get_mem_opts $app_mem) \ |
| 199 | + ${java_opts} \ |
| 200 | + ${java_args[@]} \ |
| 201 | + -jar "$app_launcher" \ |
| 202 | + "${app_commands[@]}" \ |
| 203 | + "${residual_args[@]}" |
| 204 | + |
| 205 | + local exit_code=$? |
| 206 | + if is_cygwin; then |
| 207 | + stty icanon echo > /dev/null 2>&1 |
| 208 | + fi |
| 209 | + exit $exit_code |
| 210 | +} |
| 211 | + |
| 212 | +# Loads a configuration file full of default command line options for this script. |
| 213 | +loadConfigFile() { |
| 214 | + cat "$1" | sed '/^\#/d' |
| 215 | +} |
| 216 | + |
| 217 | +### ------------------------------- ### |
| 218 | +### Start of customized settings ### |
| 219 | +### ------------------------------- ### |
| 220 | +usage() { |
| 221 | + cat <<EOM |
| 222 | +Usage: $script_name [ui] [options] |
| 223 | +
|
| 224 | + ui starts the Activator UI |
| 225 | + new Create a new project from a template |
| 226 | + -h | -help print this message |
| 227 | + -v | -verbose this runner is chattier |
| 228 | + -d | -debug set sbt log level to debug |
| 229 | + -mem <integer> set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem)) |
| 230 | + -jvm-debug <port> Turn on JVM debugging, open at the given port. |
| 231 | +
|
| 232 | + # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) |
| 233 | + -java-home <path> alternate JAVA_HOME |
| 234 | +
|
| 235 | + # jvm options and output control |
| 236 | + JAVA_OPTS environment variable, if unset uses "$java_opts" |
| 237 | + -Dkey=val pass -Dkey=val directly to the java runtime |
| 238 | + -J-X pass option -X directly to the java runtime |
| 239 | + (-J is stripped) |
| 240 | +
|
| 241 | +In the case of duplicated or conflicting options, the order above |
| 242 | +shows precedence: JAVA_OPTS lowest, command line options highest. |
| 243 | +EOM |
| 244 | +} |
| 245 | + |
| 246 | +### ------------------------------- ### |
| 247 | +### Main script ### |
| 248 | +### ------------------------------- ### |
| 249 | + |
| 250 | +declare -a residual_args |
| 251 | +declare -a java_args |
| 252 | +declare -a app_commands |
| 253 | +declare -r activator_home="$(realpath "$(dirname "$0")")" |
| 254 | +declare -r app_version="1.0.0" |
| 255 | + |
| 256 | +declare -r app_launcher="${activator_home}/activator-launch-${app_version}.jar" |
| 257 | +declare -r script_name=activator |
| 258 | +declare -r java_cmd=$(get_java_cmd) |
| 259 | +userhome="$HOME" |
| 260 | +if is_cygwin; then |
| 261 | + # cygwin sets home to something f-d up, set to real windows homedir |
| 262 | + userhome="$USERPROFILE" |
| 263 | +fi |
| 264 | +declare -r activator_user_home_dir="${userhome}/.activator" |
| 265 | +declare -r java_opts_config="${activator_user_home_dir}/activatorconfig.txt" |
| 266 | + |
| 267 | +# Now check to see if it's a good enough version |
| 268 | +declare -r java_version=$("$java_cmd" -version 2>&1 | awk -F '"' '/version/ {print $2}') |
| 269 | +if [[ "$java_version" == "" ]]; then |
| 270 | + echo |
| 271 | + echo No java installations was detected. |
| 272 | + echo Please go to http://www.java.com/getjava/ and download |
| 273 | + echo |
| 274 | + exit 1 |
| 275 | +elif [[ ! "$java_version" > "1.6" ]]; then |
| 276 | + echo |
| 277 | + echo The java installation you have is not up to date |
| 278 | + echo Activator requires at least version 1.6+, you have |
| 279 | + echo version $java_version |
| 280 | + echo |
| 281 | + echo Please go to http://www.java.com/getjava/ and download |
| 282 | + echo a valid Java Runtime and install before running Activator. |
| 283 | + echo |
| 284 | + exit 1 |
| 285 | +fi |
| 286 | + |
| 287 | +# if configuration files exist, prepend their contents to the java args so it can be processed by this runner |
| 288 | +if [[ -f "$java_opts_config" ]]; then |
| 289 | + config_opts=$(loadConfigFile "$java_opts_config") |
| 290 | + for item in $config_opts |
| 291 | + do |
| 292 | + addJava "$item" |
| 293 | + done |
| 294 | +fi |
| 295 | + |
| 296 | +run "$@" |
0 commit comments