github.com/dennwc/enry@v1.6.4-0.20180424151738-42391b8e105b/java/sbt (about)

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