source: launcher/sbt @ 86f8d63

Last change on this file since 86f8d63 was 86f8d63, checked in by meeh <meeh@…>, 2 years ago

WIP: Adding most of the launcher code now to the repo, the one for the browser bundle.
Not 100% completely done but ready for commenting/review.

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