|
| 1 | +#!/usr/bin/env 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 | + 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. |
| 42 | +is_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. |
| 52 | +cygwinpath() { |
| 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 |
| 62 | +make_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. |
| 73 | +get_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 | + |
| 81 | +echoerr () { |
| 82 | + echo 1>&2 "$@" |
| 83 | +} |
| 84 | +vlog () { |
| 85 | + [[ $verbose || $debug ]] && echoerr "$@" |
| 86 | +} |
| 87 | +dlog () { |
| 88 | + [[ $debug ]] && echoerr "$@" |
| 89 | +} |
| 90 | +execRunner () { |
| 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 | +} |
| 105 | +addJava () { |
| 106 | + dlog "[addJava] arg = '$1'" |
| 107 | + java_args=( "${java_args[@]}" "$1" ) |
| 108 | +} |
| 109 | +addApp () { |
| 110 | + dlog "[addApp] arg = '$1'" |
| 111 | + sbt_commands=( "${app_commands[@]}" "$1" ) |
| 112 | +} |
| 113 | +addResidual () { |
| 114 | + dlog "[residual] arg = '$1'" |
| 115 | + residual_args=( "${residual_args[@]}" "$1" ) |
| 116 | +} |
| 117 | +addDebugger () { |
| 118 | + addJava "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" |
| 119 | +} |
| 120 | +addConfigOpts () { |
| 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. |
| 129 | +get_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 | +} |
| 152 | +require_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 | +} |
| 160 | +is_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 |
| 165 | +detect_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. |
| 178 | +process_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. |
| 207 | +run() { |
| 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. |
| 241 | +loadConfigFile() { |
| 242 | + cat "$1" | sed '/^\#/d' |
| 243 | +} |
| 244 | + |
| 245 | +### ------------------------------- ### |
| 246 | +### Start of customized settings ### |
| 247 | +### ------------------------------- ### |
| 248 | +usage() { |
| 249 | + cat <<EOM |
| 250 | +Usage: $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 "" |
| 274 | + SBT_OPTS Environment variable, if unset uses "" |
| 275 | + ACTIVATOR_OPTS Environment variable, if unset uses "" |
| 276 | +
|
| 277 | +In the case of duplicated or conflicting options, the order above |
| 278 | +shows precedence: environment variables lowest, command line options highest. |
| 279 | +EOM |
| 280 | +} |
| 281 | + |
| 282 | +### ------------------------------- ### |
| 283 | +### Main script ### |
| 284 | +### ------------------------------- ### |
| 285 | + |
| 286 | +declare -a residual_args |
| 287 | +declare -a java_args |
| 288 | +declare -a app_commands |
| 289 | +declare -r real_script_path="$(realpath "$0")" |
| 290 | +declare -r activator_home="$(realpath "$(dirname "$real_script_path")")" |
| 291 | +declare -r app_version="1.3.2" |
| 292 | + |
| 293 | +declare -r app_launcher="${activator_home}/activator-launch-${app_version}.jar" |
| 294 | +declare -r script_name=activator |
| 295 | +java_cmd=$(get_java_cmd) |
| 296 | +declare -r java_opts=( "${ACTIVATOR_OPTS[@]}" "${SBT_OPTS[@]}" "${JAVA_OPTS[@]}" "${java_opts[@]}" ) |
| 297 | +userhome="$HOME" |
| 298 | +if is_cygwin; then |
| 299 | + # cygwin sets home to something f-d up, set to real windows homedir |
| 300 | + userhome="$USERPROFILE" |
| 301 | +fi |
| 302 | +declare -r activator_user_home_dir="${userhome}/.activator" |
| 303 | +declare -r java_opts_config_home="${activator_user_home_dir}/activatorconfig.txt" |
| 304 | +declare -r java_opts_config_version="${activator_user_home_dir}/${app_version}/activatorconfig.txt" |
| 305 | + |
| 306 | +# Now check to see if it's a good enough version |
| 307 | +declare -r java_version=$("$java_cmd" -version 2>&1 | awk -F '"' '/version/ {print $2}') |
| 308 | +if [[ "$java_version" == "" ]]; then |
| 309 | + echo |
| 310 | + echo No java installations was detected. |
| 311 | + echo Please go to http://www.java.com/getjava/ and download |
| 312 | + echo |
| 313 | + exit 1 |
| 314 | +elif [[ ! "$java_version" > "1.6" ]]; then |
| 315 | + echo |
| 316 | + echo The java installation you have is not up to date |
| 317 | + echo Activator requires at least version 1.6+, you have |
| 318 | + echo version $java_version |
| 319 | + echo |
| 320 | + echo Please go to http://www.java.com/getjava/ and download |
| 321 | + echo a valid Java Runtime and install before running Activator. |
| 322 | + echo |
| 323 | + exit 1 |
| 324 | +fi |
| 325 | + |
| 326 | +# if configuration files exist, prepend their contents to the java args so it can be processed by this runner |
| 327 | +# a "versioned" config trumps one on the top level |
| 328 | +if [[ -f "$java_opts_config_version" ]]; then |
| 329 | + addConfigOpts $(loadConfigFile "$java_opts_config_version") |
| 330 | +elif [[ -f "$java_opts_config_home" ]]; then |
| 331 | + addConfigOpts $(loadConfigFile "$java_opts_config_home") |
| 332 | +fi |
| 333 | + |
| 334 | +run "$@" |
0 commit comments