activator-default 9.3 KB

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