|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | + |
| 4 | +# A library to simplify using the SBT launcher from other packages. |
| 5 | +# Note: This should be used by tools like giter8/conscript etc. |
| 6 | + |
| 7 | +# TODO - Should we merge the main SBT script with this library? |
| 8 | + |
| 9 | +if test -z "$HOME"; then |
| 10 | + declare -r script_dir="$(dirname $script_path)" |
| 11 | +else |
| 12 | + declare -r script_dir="$HOME/.sbt" |
| 13 | +fi |
| 14 | + |
| 15 | +declare -a residual_args |
| 16 | +declare -a java_args |
| 17 | +declare -a scalac_args |
| 18 | +declare -a sbt_commands |
| 19 | +declare -a maven_profiles |
| 20 | + |
| 21 | +if test -x "$JAVA_HOME/bin/java"; then |
| 22 | + echo -e "Using $JAVA_HOME as default JAVA_HOME." |
| 23 | + echo "Note, this will be overridden by -java-home if it is set." |
| 24 | + declare java_cmd="$JAVA_HOME/bin/java" |
| 25 | +else |
| 26 | + declare java_cmd=java |
| 27 | +fi |
| 28 | + |
| 29 | +echoerr () { |
| 30 | + echo 1>&2 "$@" |
| 31 | +} |
| 32 | +vlog () { |
| 33 | + [[ $verbose || $debug ]] && echoerr "$@" |
| 34 | +} |
| 35 | +dlog () { |
| 36 | + [[ $debug ]] && echoerr "$@" |
| 37 | +} |
| 38 | + |
| 39 | +acquire_sbt_jar () { |
| 40 | + SBT_VERSION=`awk -F "=" '/sbt\\.version/ {print $2}' ./project/build.properties` |
| 41 | + URL1=https://dl.bintray.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/${SBT_VERSION}/sbt-launch.jar |
| 42 | + JAR=build/sbt-launch-${SBT_VERSION}.jar |
| 43 | + |
| 44 | + sbt_jar=$JAR |
| 45 | + |
| 46 | + if [[ ! -f "$sbt_jar" ]]; then |
| 47 | + # Download sbt launch jar if it hasn't been downloaded yet |
| 48 | + if [ ! -f ${JAR} ]; then |
| 49 | + # Download |
| 50 | + printf "Attempting to fetch sbt\n" |
| 51 | + JAR_DL=${JAR}.part |
| 52 | + if hash curl 2>/dev/null; then |
| 53 | + curl --fail --location --silent ${URL1} > "${JAR_DL}" &&\ |
| 54 | + mv "${JAR_DL}" "${JAR}" |
| 55 | + elif hash wget 2>/dev/null; then |
| 56 | + wget --quiet ${URL1} -O "${JAR_DL}" &&\ |
| 57 | + mv "${JAR_DL}" "${JAR}" |
| 58 | + else |
| 59 | + printf "You do not have curl or wget installed, please install sbt manually from http://www.scala-sbt.org/\n" |
| 60 | + exit -1 |
| 61 | + fi |
| 62 | + fi |
| 63 | + if [ ! -f ${JAR} ]; then |
| 64 | + # We failed to download |
| 65 | + printf "Our attempt to download sbt locally to ${JAR} failed. Please install sbt manually from http://www.scala-sbt.org/\n" |
| 66 | + exit -1 |
| 67 | + fi |
| 68 | + printf "Launching sbt from ${JAR}\n" |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +execRunner () { |
| 73 | + # print the arguments one to a line, quoting any containing spaces |
| 74 | + [[ $verbose || $debug ]] && echo "# Executing command line:" && { |
| 75 | + for arg; do |
| 76 | + if printf "%s\n" "$arg" | grep -q ' '; then |
| 77 | + printf "\"%s\"\n" "$arg" |
| 78 | + else |
| 79 | + printf "%s\n" "$arg" |
| 80 | + fi |
| 81 | + done |
| 82 | + echo "" |
| 83 | + } |
| 84 | + |
| 85 | + exec "$@" |
| 86 | +} |
| 87 | + |
| 88 | +addJava () { |
| 89 | + dlog "[addJava] arg = '$1'" |
| 90 | + java_args=( "${java_args[@]}" "$1" ) |
| 91 | +} |
| 92 | + |
| 93 | +enableProfile () { |
| 94 | + dlog "[enableProfile] arg = '$1'" |
| 95 | + maven_profiles=( "${maven_profiles[@]}" "$1" ) |
| 96 | + export SBT_MAVEN_PROFILES="${maven_profiles[@]}" |
| 97 | +} |
| 98 | + |
| 99 | +addSbt () { |
| 100 | + dlog "[addSbt] arg = '$1'" |
| 101 | + sbt_commands=( "${sbt_commands[@]}" "$1" ) |
| 102 | +} |
| 103 | +addResidual () { |
| 104 | + dlog "[residual] arg = '$1'" |
| 105 | + residual_args=( "${residual_args[@]}" "$1" ) |
| 106 | +} |
| 107 | +addDebugger () { |
| 108 | + addJava "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" |
| 109 | +} |
| 110 | + |
| 111 | +# a ham-fisted attempt to move some memory settings in concert |
| 112 | +# so they need not be dicked around with individually. |
| 113 | +get_mem_opts () { |
| 114 | + local mem=${1:-2048} |
| 115 | + local perm=$(( $mem / 4 )) |
| 116 | + (( $perm > 256 )) || perm=256 |
| 117 | + (( $perm < 4096 )) || perm=4096 |
| 118 | + local codecache=$(( $perm / 2 )) |
| 119 | + |
| 120 | + echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m" |
| 121 | +} |
| 122 | + |
| 123 | +require_arg () { |
| 124 | + local type="$1" |
| 125 | + local opt="$2" |
| 126 | + local arg="$3" |
| 127 | + if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then |
| 128 | + die "$opt requires <$type> argument" |
| 129 | + fi |
| 130 | +} |
| 131 | + |
| 132 | +is_function_defined() { |
| 133 | + declare -f "$1" > /dev/null |
| 134 | +} |
| 135 | + |
| 136 | +process_args () { |
| 137 | + while [[ $# -gt 0 ]]; do |
| 138 | + case "$1" in |
| 139 | + -h|-help) usage; exit 1 ;; |
| 140 | + -v|-verbose) verbose=1 && shift ;; |
| 141 | + -d|-debug) debug=1 && shift ;; |
| 142 | + |
| 143 | + -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; |
| 144 | + -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;; |
| 145 | + -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; |
| 146 | + -batch) exec </dev/null && shift ;; |
| 147 | + |
| 148 | + -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;; |
| 149 | + -sbt-version) require_arg version "$1" "$2" && sbt_version="$2" && shift 2 ;; |
| 150 | + -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && export JAVA_HOME=$2 && shift 2 ;; |
| 151 | + |
| 152 | + -D*) addJava "$1" && shift ;; |
| 153 | + -J*) addJava "${1:2}" && shift ;; |
| 154 | + -P*) enableProfile "$1" && shift ;; |
| 155 | + *) addResidual "$1" && shift ;; |
| 156 | + esac |
| 157 | + done |
| 158 | + |
| 159 | + is_function_defined process_my_args && { |
| 160 | + myargs=("${residual_args[@]}") |
| 161 | + residual_args=() |
| 162 | + process_my_args "${myargs[@]}" |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +run() { |
| 167 | + # no jar? download it. |
| 168 | + [[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || { |
| 169 | + # still no jar? uh-oh. |
| 170 | + echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar" |
| 171 | + exit 1 |
| 172 | + } |
| 173 | + |
| 174 | + # process the combined args, then reset "$@" to the residuals |
| 175 | + process_args "$@" |
| 176 | + set -- "${residual_args[@]}" |
| 177 | + argumentCount=$# |
| 178 | + |
| 179 | + # run sbt |
| 180 | + execRunner "$java_cmd" \ |
| 181 | + ${SBT_OPTS:-$default_sbt_opts} \ |
| 182 | + $(get_mem_opts $sbt_mem) \ |
| 183 | + ${java_opts} \ |
| 184 | + ${java_args[@]} \ |
| 185 | + -jar "$sbt_jar" \ |
| 186 | + "${sbt_commands[@]}" \ |
| 187 | + "${residual_args[@]}" |
| 188 | +} |
| 189 | + |
| 190 | +runAlternateBoot() { |
| 191 | + local bootpropsfile="$1" |
| 192 | + shift |
| 193 | + addJava "-Dsbt.boot.properties=$bootpropsfile" |
| 194 | + run $@ |
| 195 | +} |
0 commit comments