sbt-launch-lib.bash 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. #
  3. # A library to simplify using the SBT launcher from other packages.
  4. # Note: This should be used by tools like giter8/conscript etc.
  5. # TODO - Should we merge the main SBT script with this library?
  6. if test -z "$HOME"; then
  7. declare -r script_dir="$(dirname $script_path)"
  8. else
  9. declare -r script_dir="$HOME/.sbt"
  10. fi
  11. declare -a residual_args
  12. declare -a java_args
  13. declare -a scalac_args
  14. declare -a sbt_commands
  15. declare java_cmd=java
  16. echoerr () {
  17. echo 1>&2 "$@"
  18. }
  19. vlog () {
  20. [[ $verbose || $debug ]] && echoerr "$@"
  21. }
  22. dlog () {
  23. [[ $debug ]] && echoerr "$@"
  24. }
  25. jar_file () {
  26. echo "$(dirname $(realpath $0))/sbt-launch.jar"
  27. }
  28. acquire_sbt_jar () {
  29. sbt_jar="$(jar_file)"
  30. if [[ ! -f "$sbt_jar" ]]; then
  31. echoerr "Could not find launcher jar: $sbt_jar"
  32. exit 2
  33. fi
  34. }
  35. execRunner () {
  36. # print the arguments one to a line, quoting any containing spaces
  37. [[ $verbose || $debug ]] && echo "# Executing command line:" && {
  38. for arg; do
  39. if printf "%s\n" "$arg" | grep -q ' '; then
  40. printf "\"%s\"\n" "$arg"
  41. else
  42. printf "%s\n" "$arg"
  43. fi
  44. done
  45. echo ""
  46. }
  47. exec "$@"
  48. }
  49. addJava () {
  50. dlog "[addJava] arg = '$1'"
  51. java_args=( "${java_args[@]}" "$1" )
  52. }
  53. addSbt () {
  54. dlog "[addSbt] arg = '$1'"
  55. sbt_commands=( "${sbt_commands[@]}" "$1" )
  56. }
  57. addResidual () {
  58. dlog "[residual] arg = '$1'"
  59. residual_args=( "${residual_args[@]}" "$1" )
  60. }
  61. addDebugger () {
  62. addJava "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
  63. }
  64. # a ham-fisted attempt to move some memory settings in concert
  65. # so they need not be dicked around with individually.
  66. get_mem_opts () {
  67. local mem=${1:-1536}
  68. local perm=$(( $mem / 4 ))
  69. (( $perm > 256 )) || perm=256
  70. (( $perm < 1024 )) || perm=1024
  71. local codecache=$(( $perm / 2 ))
  72. echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m"
  73. }
  74. require_arg () {
  75. local type="$1"
  76. local opt="$2"
  77. local arg="$3"
  78. if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
  79. die "$opt requires <$type> argument"
  80. fi
  81. }
  82. is_function_defined() {
  83. declare -f "$1" > /dev/null
  84. }
  85. process_args () {
  86. while [[ $# -gt 0 ]]; do
  87. case "$1" in
  88. -h|-help) usage; exit 1 ;;
  89. -v|-verbose) verbose=1 && shift ;;
  90. -d|-debug) debug=1 && shift ;;
  91. -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
  92. -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;;
  93. -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
  94. -batch) exec </dev/null && shift ;;
  95. -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
  96. -sbt-version) require_arg version "$1" "$2" && sbt_version="$2" && shift 2 ;;
  97. -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;
  98. -D*) addJava "$1" && shift ;;
  99. -J*) addJava "${1:2}" && shift ;;
  100. *) addResidual "$1" && shift ;;
  101. esac
  102. done
  103. is_function_defined process_my_args && {
  104. myargs=("${residual_args[@]}")
  105. residual_args=()
  106. process_my_args "${myargs[@]}"
  107. }
  108. }
  109. run() {
  110. # no jar? download it.
  111. [[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || {
  112. # still no jar? uh-oh.
  113. echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar"
  114. exit 1
  115. }
  116. # process the combined args, then reset "$@" to the residuals
  117. process_args "$@"
  118. set -- "${residual_args[@]}"
  119. argumentCount=$#
  120. # run sbt
  121. execRunner "$java_cmd" \
  122. ${SBT_OPTS:-$default_sbt_opts} \
  123. $(get_mem_opts $sbt_mem) \
  124. ${java_opts} \
  125. ${java_args[@]} \
  126. -jar "$sbt_jar" \
  127. "${sbt_commands[@]}" \
  128. "${residual_args[@]}"
  129. }
  130. runAlternateBoot() {
  131. local bootpropsfile="$1"
  132. shift
  133. addJava "-Dsbt.boot.properties=$bootpropsfile"
  134. run $@
  135. }