PageRenderTime 66ms CodeModel.GetById 18ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 1ms

/sbt

http://github.com/scalaz/scalaz
#! | 543 lines | 467 code | 76 blank | 0 comment | 0 complexity | 7907dc867a3c1872c2ceaafe6b14c967 MD5 | raw file
  1#!/usr/bin/env bash
  2#
  3# A more capable sbt runner, coincidentally also called sbt.
  4# Author: Paul Phillips <paulp@improving.org>
  5
  6# todo - make this dynamic
  7declare -r sbt_release_version="0.13.8"
  8declare -r sbt_unreleased_version="0.13.9-M1"
  9declare -r buildProps="project/build.properties"
 10
 11declare sbt_jar sbt_dir sbt_create sbt_version
 12declare scala_version sbt_explicit_version
 13declare verbose noshare batch trace_level log_level
 14declare sbt_saved_stty debugUs
 15
 16echoerr () { echo >&2 "$@"; }
 17vlog ()    { [[ -n "$verbose" ]] && echoerr "$@"; }
 18
 19# spaces are possible, e.g. sbt.version = 0.13.0
 20build_props_sbt () {
 21  [[ -r "$buildProps" ]] && \
 22    grep '^sbt\.version' "$buildProps" | tr '=\r' ' ' | awk '{ print $2; }'
 23}
 24
 25update_build_props_sbt () {
 26  local ver="$1"
 27  local old="$(build_props_sbt)"
 28
 29  [[ -r "$buildProps" ]] && [[ "$ver" != "$old" ]] && {
 30    perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps"
 31    grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps"
 32
 33    vlog "!!!"
 34    vlog "!!! Updated file $buildProps setting sbt.version to: $ver"
 35    vlog "!!! Previous value was: $old"
 36    vlog "!!!"
 37  }
 38}
 39
 40set_sbt_version () {
 41  sbt_version="${sbt_explicit_version:-$(build_props_sbt)}"
 42  [[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version
 43  export sbt_version
 44}
 45
 46# restore stty settings (echo in particular)
 47onSbtRunnerExit() {
 48  [[ -n "$sbt_saved_stty" ]] || return
 49  vlog ""
 50  vlog "restoring stty: $sbt_saved_stty"
 51  stty "$sbt_saved_stty"
 52  unset sbt_saved_stty
 53}
 54
 55# save stty and trap exit, to ensure echo is reenabled if we are interrupted.
 56trap onSbtRunnerExit EXIT
 57sbt_saved_stty="$(stty -g 2>/dev/null)"
 58vlog "Saved stty: $sbt_saved_stty"
 59
 60# this seems to cover the bases on OSX, and someone will
 61# have to tell me about the others.
 62get_script_path () {
 63  local path="$1"
 64  [[ -L "$path" ]] || { echo "$path" ; return; }
 65
 66  local target="$(readlink "$path")"
 67  if [[ "${target:0:1}" == "/" ]]; then
 68    echo "$target"
 69  else
 70    echo "${path%/*}/$target"
 71  fi
 72}
 73
 74die() {
 75  echo "Aborting: $@"
 76  exit 1
 77}
 78
 79make_url () {
 80  version="$1"
 81
 82  case "$version" in
 83        0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;;
 84      0.10.* ) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
 85    0.11.[12]) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
 86            *) echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;;
 87  esac
 88}
 89
 90init_default_option_file () {
 91  local overriding_var="${!1}"
 92  local default_file="$2"
 93  if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then
 94    local envvar_file="${BASH_REMATCH[1]}"
 95    if [[ -r "$envvar_file" ]]; then
 96      default_file="$envvar_file"
 97    fi
 98  fi
 99  echo "$default_file"
100}
101
102declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
103declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation"
104declare -r default_jvm_opts_common="-Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts"
105declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy"
106declare -r latest_28="2.8.2"
107declare -r latest_29="2.9.3"
108declare -r latest_210="2.10.5"
109declare -r latest_211="2.11.7"
110
111declare -r script_path="$(get_script_path "$BASH_SOURCE")"
112declare -r script_name="${script_path##*/}"
113
114# some non-read-onlies set with defaults
115declare java_cmd="java"
116declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)"
117declare jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)"
118declare sbt_launch_repo="http://repo.typesafe.com/typesafe/ivy-releases"
119
120# pull -J and -D options to give to java.
121declare -a residual_args
122declare -a java_args
123declare -a scalac_args
124declare -a sbt_commands
125
126# args to jvm/sbt via files or environment variables
127declare -a extra_jvm_opts extra_sbt_opts
128
129addJava () {
130  vlog "[addJava] arg = '$1'"
131  java_args+=("$1")
132}
133addSbt () {
134  vlog "[addSbt] arg = '$1'"
135  sbt_commands+=("$1")
136}
137setThisBuild () {
138  vlog "[addBuild] args = '$@'"
139  local key="$1" && shift
140  addSbt "set $key in ThisBuild := $@"
141}
142addScalac () {
143  vlog "[addScalac] arg = '$1'"
144  scalac_args+=("$1")
145}
146addResidual () {
147  vlog "[residual] arg = '$1'"
148  residual_args+=("$1")
149}
150addResolver () {
151  addSbt "set resolvers += $1"
152}
153addDebugger () {
154  addJava "-Xdebug"
155  addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
156}
157setScalaVersion () {
158  [[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
159  addSbt "++ $1"
160}
161setJavaHome () {
162  java_cmd="$1/bin/java"
163  setThisBuild javaHome "Some(file(\"$1\"))"
164  export JAVA_HOME="$1"
165  export JDK_HOME="$1"
166  export PATH="$JAVA_HOME/bin:$PATH"
167}
168setJavaHomeQuietly () {
169  addSbt warn
170  setJavaHome "$1"
171  addSbt info
172}
173
174# if set, use JDK_HOME/JAVA_HOME over java found in path
175if [[ -e "$JDK_HOME/lib/tools.jar" ]]; then
176  setJavaHomeQuietly "$JDK_HOME"
177elif [[ -e "$JAVA_HOME/bin/java" ]]; then
178  setJavaHomeQuietly "$JAVA_HOME"
179fi
180
181# directory to store sbt launchers
182declare sbt_launch_dir="$HOME/.sbt/launchers"
183[[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir"
184[[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)"
185
186java_version () {
187  local version=$("$java_cmd" -version 2>&1 | grep -E -e '(java|openjdk) version' | awk '{ print $3 }' | tr -d \")
188  vlog "Detected Java version: $version"
189  echo "${version:2:1}"
190}
191
192# MaxPermSize critical on pre-8 jvms but incurs noisy warning on 8+
193default_jvm_opts () {
194  local v="$(java_version)"
195  if [[ $v -ge 8 ]]; then
196    echo "$default_jvm_opts_common"
197  else
198    echo "-XX:MaxPermSize=384m $default_jvm_opts_common"
199  fi
200}
201
202build_props_scala () {
203  if [[ -r "$buildProps" ]]; then
204    versionLine="$(grep '^build.scala.versions' "$buildProps")"
205    versionString="${versionLine##build.scala.versions=}"
206    echo "${versionString%% .*}"
207  fi
208}
209
210execRunner () {
211  # print the arguments one to a line, quoting any containing spaces
212  vlog "# Executing command line:" && {
213    for arg; do
214      if [[ -n "$arg" ]]; then
215        if printf "%s\n" "$arg" | grep -q ' '; then
216          printf >&2 "\"%s\"\n" "$arg"
217        else
218          printf >&2 "%s\n" "$arg"
219        fi
220      fi
221    done
222    vlog ""
223  }
224
225  [[ -n "$batch" ]] && exec </dev/null
226  exec "$@"
227}
228
229jar_url () {
230  make_url "$1"
231}
232
233jar_file () {
234  echo "$sbt_launch_dir/$1/sbt-launch.jar"
235}
236
237download_url () {
238  local url="$1"
239  local jar="$2"
240
241  echoerr "Downloading sbt launcher for $sbt_version:"
242  echoerr "  From  $url"
243  echoerr "    To  $jar"
244
245  mkdir -p "${jar%/*}" && {
246    if which curl >/dev/null; then
247      curl --fail --silent --location "$url" --output "$jar"
248    elif which wget >/dev/null; then
249      wget --quiet -O "$jar" "$url"
250    fi
251  } && [[ -r "$jar" ]]
252}
253
254acquire_sbt_jar () {
255  sbt_url="$(jar_url "$sbt_version")"
256  sbt_jar="$(jar_file "$sbt_version")"
257
258  [[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar"
259}
260
261usage () {
262  cat <<EOM
263Usage: $script_name [options]
264
265Note that options which are passed along to sbt begin with -- whereas
266options to this runner use a single dash. Any sbt command can be scheduled
267to run first by prefixing the command with --, so --warn, --error and so on
268are not special.
269
270Output filtering: if there is a file in the home directory called .sbtignore
271and this is not an interactive sbt session, the file is treated as a list of
272bash regular expressions. Output lines which match any regex are not echoed.
273One can see exactly which lines would have been suppressed by starting this
274runner with the -x option.
275
276  -h | -help         print this message
277  -v                 verbose operation (this runner is chattier)
278  -d, -w, -q         aliases for --debug, --warn, --error (q means quiet)
279  -x                 debug this script
280  -trace <level>     display stack traces with a max of <level> frames (default: -1, traces suppressed)
281  -debug-inc         enable debugging log for the incremental compiler
282  -no-colors         disable ANSI color codes
283  -sbt-create        start sbt even if current directory contains no sbt project
284  -sbt-dir   <path>  path to global settings/plugins directory (default: ~/.sbt/<version>)
285  -sbt-boot  <path>  path to shared boot directory (default: ~/.sbt/boot in 0.11+)
286  -ivy       <path>  path to local Ivy repository (default: ~/.ivy2)
287  -no-share          use all local caches; no sharing
288  -offline           put sbt in offline mode
289  -jvm-debug <port>  Turn on JVM debugging, open at the given port.
290  -batch             Disable interactive mode
291  -prompt <expr>     Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted
292
293  # sbt version (default: sbt.version from $buildProps if present, otherwise $sbt_release_version)
294  -sbt-force-latest         force the use of the latest release of sbt: $sbt_release_version
295  -sbt-version  <version>   use the specified version of sbt (default: $sbt_release_version)
296  -sbt-dev                  use the latest pre-release version of sbt: $sbt_unreleased_version
297  -sbt-jar      <path>      use the specified jar as the sbt launcher
298  -sbt-launch-dir <path>    directory to hold sbt launchers (default: ~/.sbt/launchers)
299  -sbt-launch-repo <url>    repo url for downloading sbt launcher jar (default: $sbt_launch_repo)
300
301  # scala version (default: as chosen by sbt)
302  -28                       use $latest_28
303  -29                       use $latest_29
304  -210                      use $latest_210
305  -211                      use $latest_211
306  -scala-home <path>        use the scala build at the specified directory
307  -scala-version <version>  use the specified version of scala
308  -binary-version <version> use the specified scala version when searching for dependencies
309
310  # java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
311  -java-home <path>         alternate JAVA_HOME
312
313  # passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution
314  # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found
315  <default>        $(default_jvm_opts)
316  JVM_OPTS         environment variable holding either the jvm args directly, or
317                   the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts')
318                   Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument.
319  -jvm-opts <path> file containing jvm args (if not given, .jvmopts in project root is used if present)
320  -Dkey=val        pass -Dkey=val directly to the jvm
321  -J-X             pass option -X directly to the jvm (-J is stripped)
322
323  # passing options to sbt, OR to this runner
324  SBT_OPTS         environment variable holding either the sbt args directly, or
325                   the reference to a file containing sbt args if given path is prepended by '@' (e.g. '@/etc/sbtopts')
326                   Note: "@"-file is overridden by local '.sbtopts' or '-sbt-opts' argument.
327  -sbt-opts <path> file containing sbt args (if not given, .sbtopts in project root is used if present)
328  -S-X             add -X to sbt's scalacOptions (-S is stripped)
329EOM
330}
331
332process_args ()
333{
334  require_arg () {
335    local type="$1"
336    local opt="$2"
337    local arg="$3"
338
339    if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
340      die "$opt requires <$type> argument"
341    fi
342  }
343  while [[ $# -gt 0 ]]; do
344    case "$1" in
345          -h|-help) usage; exit 1 ;;
346                -v) verbose=true && shift ;;
347                -d) addSbt "--debug" && addSbt debug && shift ;;
348                -w) addSbt "--warn"  && addSbt warn  && shift ;;
349                -q) addSbt "--error" && addSbt error && shift ;;
350                -x) debugUs=true && shift ;;
351            -trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;;
352              -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
353        -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;;
354         -no-share) noshare=true && shift ;;
355         -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;;
356          -sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;;
357        -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;;
358          -offline) addSbt "set offline := true" && shift ;;
359        -jvm-debug) require_arg port "$1" "$2" && addDebugger "$2" && shift 2 ;;
360            -batch) batch=true && shift ;;
361           -prompt) require_arg "expr" "$1" "$2" && setThisBuild shellPrompt "(s => { val e = Project.extract(s) ; $2 })" && shift 2 ;;
362
363       -sbt-create) sbt_create=true && shift ;;
364          -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
365      -sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;;
366 -sbt-force-latest) sbt_explicit_version="$sbt_release_version" && shift ;;
367          -sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;;
368   -sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;;
369  -sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;;
370    -scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;;
371   -binary-version) require_arg version "$1" "$2" && setThisBuild scalaBinaryVersion "\"$2\"" && shift 2 ;;
372       -scala-home) require_arg path "$1" "$2" && setThisBuild scalaHome "Some(file(\"$2\"))" && shift 2 ;;
373        -java-home) require_arg path "$1" "$2" && setJavaHome "$2" && shift 2 ;;
374         -sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;;
375         -jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;;
376
377               -D*) addJava "$1" && shift ;;
378               -J*) addJava "${1:2}" && shift ;;
379               -S*) addScalac "${1:2}" && shift ;;
380               -28) setScalaVersion "$latest_28" && shift ;;
381               -29) setScalaVersion "$latest_29" && shift ;;
382              -210) setScalaVersion "$latest_210" && shift ;;
383              -211) setScalaVersion "$latest_211" && shift ;;
384
385           --debug) addSbt debug && addResidual "$1" && shift ;;
386            --warn) addSbt warn  && addResidual "$1" && shift ;;
387           --error) addSbt error && addResidual "$1" && shift ;;
388                 *) addResidual "$1" && shift ;;
389    esac
390  done
391}
392
393# process the direct command line arguments
394process_args "$@"
395
396# skip #-styled comments and blank lines
397readConfigFile() {
398  while read line; do
399    [[ $line =~ ^# ]] || [[ -z $line ]] || echo "$line"
400  done < "$1"
401}
402
403# if there are file/environment sbt_opts, process again so we
404# can supply args to this runner
405if [[ -r "$sbt_opts_file" ]]; then
406  vlog "Using sbt options defined in file $sbt_opts_file"
407  while read opt; do extra_sbt_opts+=("$opt"); done < <(readConfigFile "$sbt_opts_file")
408elif [[ -n "$SBT_OPTS" && ! ("$SBT_OPTS" =~ ^@.*) ]]; then
409  vlog "Using sbt options defined in variable \$SBT_OPTS"
410  extra_sbt_opts=( $SBT_OPTS )
411else
412  vlog "No extra sbt options have been defined"
413fi
414
415[[ -n "${extra_sbt_opts[*]}" ]] && process_args "${extra_sbt_opts[@]}"
416
417# reset "$@" to the residual args
418set -- "${residual_args[@]}"
419argumentCount=$#
420
421# set sbt version
422set_sbt_version
423
424# only exists in 0.12+
425setTraceLevel() {
426  case "$sbt_version" in
427    "0.7."* | "0.10."* | "0.11."* ) echoerr "Cannot set trace level in sbt version $sbt_version" ;;
428                                 *) setThisBuild traceLevel $trace_level ;;
429  esac
430}
431
432# set scalacOptions if we were given any -S opts
433[[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[@]}\""
434
435# Update build.properties on disk to set explicit version - sbt gives us no choice
436[[ -n "$sbt_explicit_version" ]] && update_build_props_sbt "$sbt_explicit_version"
437vlog "Detected sbt version $sbt_version"
438
439[[ -n "$scala_version" ]] && vlog "Overriding scala version to $scala_version"
440
441# no args - alert them there's stuff in here
442(( argumentCount > 0 )) || {
443  vlog "Starting $script_name: invoke with -help for other options"
444  residual_args=( shell )
445}
446
447# verify this is an sbt dir or -create was given
448[[ -r ./build.sbt || -d ./project || -n "$sbt_create" ]] || {
449  cat <<EOM
450$(pwd) doesn't appear to be an sbt project.
451If you want to start sbt anyway, run:
452  $0 -sbt-create
453
454EOM
455  exit 1
456}
457
458# pick up completion if present; todo
459[[ -r .sbt_completion.sh ]] && source .sbt_completion.sh
460
461# no jar? download it.
462[[ -r "$sbt_jar" ]] || acquire_sbt_jar || {
463  # still no jar? uh-oh.
464  echo "Download failed. Obtain the jar manually and place it at $sbt_jar"
465  exit 1
466}
467
468if [[ -n "$noshare" ]]; then
469  for opt in ${noshare_opts}; do
470    addJava "$opt"
471  done
472else
473  case "$sbt_version" in
474    "0.7."* | "0.10."* | "0.11."* | "0.12."* )
475      [[ -n "$sbt_dir" ]] || {
476        sbt_dir="$HOME/.sbt/$sbt_version"
477        vlog "Using $sbt_dir as sbt dir, -sbt-dir to override."
478      }
479    ;;
480  esac
481
482  if [[ -n "$sbt_dir" ]]; then
483    addJava "-Dsbt.global.base=$sbt_dir"
484  fi
485fi
486
487if [[ -r "$jvm_opts_file" ]]; then
488  vlog "Using jvm options defined in file $jvm_opts_file"
489  while read opt; do extra_jvm_opts+=("$opt"); done < <(readConfigFile "$jvm_opts_file")
490elif [[ -n "$JVM_OPTS" && ! ("$JVM_OPTS" =~ ^@.*) ]]; then
491  vlog "Using jvm options defined in \$JVM_OPTS variable"
492  extra_jvm_opts=( $JVM_OPTS )
493else
494  vlog "Using default jvm options"
495  extra_jvm_opts=( $(default_jvm_opts) )
496fi
497
498# traceLevel is 0.12+
499[[ -n "$trace_level" ]] && setTraceLevel
500
501main () {
502  execRunner "$java_cmd" \
503    "${extra_jvm_opts[@]}" \
504    "${java_args[@]}" \
505    -jar "$sbt_jar" \
506    "${sbt_commands[@]}" \
507    "${residual_args[@]}"
508}
509
510# sbt inserts this string on certain lines when formatting is enabled:
511#   val OverwriteLine = "\r\u001BM\u001B[2K"
512# ...in order not to spam the console with a million "Resolving" lines.
513# Unfortunately that makes it that much harder to work with when
514# we're not going to print those lines anyway. We strip that bit of
515# line noise, but leave the other codes to preserve color.
516mainFiltered () {
517  local ansiOverwrite='\r\x1BM\x1B[2K'
518  local excludeRegex=$(egrep -v '^#|^$' ~/.sbtignore | paste -sd'|' -)
519
520  echoLine () {
521    local line="$1"
522    local line1="$(echo "$line" | sed -r 's/\r\x1BM\x1B\[2K//g')"       # This strips the OverwriteLine code.
523    local line2="$(echo "$line1" | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')" # This strips all codes - we test regexes against this.
524
525    if [[ $line2 =~ $excludeRegex ]]; then
526      [[ -n $debugUs ]] && echo "[X] $line1"
527    else
528      [[ -n $debugUs ]] && echo "    $line1" || echo "$line1"
529    fi
530  }
531
532  echoLine "Starting sbt with output filtering enabled."
533  main | while read -r line; do echoLine "$line"; done
534}
535
536# Only filter if there's a filter file and we don't see a known interactive command.
537# Obviously this is super ad hoc but I don't know how to improve on it. Testing whether
538# stdin is a terminal is useless because most of my use cases for this filtering are
539# exactly when I'm at a terminal, running sbt non-interactively.
540shouldFilter () { [[ -f ~/.sbtignore ]] && ! egrep -q '\b(shell|console|consoleProject)\b' <<<"${residual_args[@]}"; }
541
542# run sbt
543if shouldFilter; then mainFiltered; else main; fi