66
77set -o pipefail
88
9- declare -r sbt_release_version=" 0.13.18 "
10- declare -r sbt_unreleased_version=" 0.13.18 "
9+ declare -r sbt_release_version=" 1.2.8 "
10+ declare -r sbt_unreleased_version=" 1.2.8 "
1111
1212declare -r latest_213=" 2.13.0-M5"
1313declare -r latest_212=" 2.12.8"
@@ -43,11 +43,12 @@ declare -a extra_jvm_opts extra_sbt_opts
4343
4444echoerr () { echo >&2 " $@ " ; }
4545vlog () { [[ -n " $verbose " ]] && echoerr " $@ " ; }
46- die () { echo " Aborting: $@ " ; exit 1; }
46+ die () { echo " Aborting: $* " ; exit 1; }
4747
4848setTrapExit () {
4949 # save stty and trap exit, to ensure echo is re-enabled if we are interrupted.
50- export SBT_STTY=" $( stty -g 2> /dev/null) "
50+ SBT_STTY=" $( stty -g 2> /dev/null) "
51+ export SBT_STTY
5152
5253 # restore stty settings (echo in particular)
5354 onSbtRunnerExit () {
@@ -67,16 +68,18 @@ get_script_path () {
6768 local path=" $1 "
6869 [[ -L " $path " ]] || { echo " $path " ; return ; }
6970
70- local target=" $( readlink " $path " ) "
71+ local -r target=" $( readlink " $path " ) "
7172 if [[ " ${target: 0: 1} " == " /" ]]; then
7273 echo " $target "
7374 else
7475 echo " ${path%/* } /$target "
7576 fi
7677}
7778
78- declare -r script_path=" $( get_script_path " $BASH_SOURCE " ) "
79- declare -r script_name=" ${script_path##*/ } "
79+ script_path=" $( get_script_path " ${BASH_SOURCE[0]} " ) "
80+ declare -r script_path
81+ script_name=" ${script_path##*/ } "
82+ declare -r script_name
8083
8184init_default_option_file () {
8285 local overriding_var=" ${! 1} "
@@ -90,8 +93,8 @@ init_default_option_file () {
9093 echo " $default_file "
9194}
9295
93- declare sbt_opts_file=" $( init_default_option_file SBT_OPTS .sbtopts) "
94- declare jvm_opts_file=" $( init_default_option_file JVM_OPTS .jvmopts) "
96+ sbt_opts_file=" $( init_default_option_file SBT_OPTS .sbtopts) "
97+ jvm_opts_file=" $( init_default_option_file JVM_OPTS .jvmopts) "
9598
9699build_props_sbt () {
97100 [[ -r " $buildProps " ]] && \
@@ -142,9 +145,9 @@ addResidual () { vlog "[residual] arg = '$1'" ; residual_args+=("$1"); }
142145addResolver () { addSbt " set resolvers += $1 " ; }
143146addDebugger () { addJava " -Xdebug" ; addJava " -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1 " ; }
144147setThisBuild () {
145- vlog " [addBuild] args = '$@ '"
148+ vlog " [addBuild] args = '$* '"
146149 local key=" $1 " && shift
147- addSbt " set $key in ThisBuild := $@ "
150+ addSbt " set $key in ThisBuild := $* "
148151}
149152setScalaVersion () {
150153 [[ " $1 " == * " -SNAPSHOT" ]] && addResolver ' Resolver.sonatypeRepo("snapshots")'
@@ -159,7 +162,7 @@ setJavaHome () {
159162}
160163
161164getJavaVersion () {
162- local str=$( " $1 " -version 2>&1 | grep -E -e ' (java|openjdk) version' | awk ' { print $3 }' | tr -d ' "' )
165+ local -r str=$( " $1 " -version 2>&1 | grep -E -e ' (java|openjdk) version' | awk ' { print $3 }' | tr -d ' "' )
163166
164167 # java -version on java8 says 1.8.x
165168 # but on 9 and 10 it's 9.x.y and 10.x.y.
@@ -191,14 +194,14 @@ checkJava() {
191194}
192195
193196java_version () {
194- local version=$( getJavaVersion " $java_cmd " )
197+ local -r version=$( getJavaVersion " $java_cmd " )
195198 vlog " Detected Java version: $version "
196199 echo " $version "
197200}
198201
199202# MaxPermSize critical on pre-8 JVMs but incurs noisy warning on 8+
200203default_jvm_opts () {
201- local v=" $( java_version) "
204+ local -r v=" $( java_version) "
202205 if [[ $v -ge 8 ]]; then
203206 echo " $default_jvm_opts_common "
204207 else
@@ -240,11 +243,11 @@ execRunner () {
240243
241244jar_url () { make_url " $1 " ; }
242245
243- is_cygwin () [[ " $( uname -a) " == " CYGWIN" * ]]
246+ is_cygwin () { [[ " $( uname -a) " == " CYGWIN" * ]]; }
244247
245248jar_file () {
246249 is_cygwin \
247- && echo " $( cygpath -w $sbt_launch_dir /" $1 " /sbt-launch.jar) " \
250+ && cygpath -w " $sbt_launch_dir /$1 /sbt-launch.jar" \
248251 || echo " $sbt_launch_dir /$1 /sbt-launch.jar"
249252}
250253
@@ -420,7 +423,7 @@ process_args "$@"
420423readConfigFile () {
421424 local end=false
422425 until $end ; do
423- read || end=true
426+ read -r || end=true
424427 [[ $REPLY =~ ^# ]] || [[ -z $REPLY ]] || echo "$REPLY"
425428 done < " $1 "
426429}
@@ -429,10 +432,10 @@ readConfigFile() {
429432# can supply args to this runner
430433if [[ -r " $sbt_opts_file " ]]; then
431434 vlog " Using sbt options defined in file $sbt_opts_file "
432- while read opt; do extra_sbt_opts+=(" $opt " ); done < <( readConfigFile " $sbt_opts_file " )
435+ while read -r opt; do extra_sbt_opts+=(" $opt " ); done < <( readConfigFile " $sbt_opts_file " )
433436elif [[ -n " $SBT_OPTS " && ! (" $SBT_OPTS " =~ ^@.* ) ]]; then
434437 vlog " Using sbt options defined in variable \$ SBT_OPTS"
435- extra_sbt_opts=( $SBT_OPTS )
438+ IFS= " " read -r -a extra_sbt_opts <<< " $SBT_OPTS"
436439else
437440 vlog " No extra sbt options have been defined"
438441fi
@@ -452,18 +455,18 @@ checkJava
452455setTraceLevel () {
453456 case " $sbt_version " in
454457 " 0.7." * | " 0.10." * | " 0.11." * ) echoerr " Cannot set trace level in sbt version $sbt_version " ;;
455- * ) setThisBuild traceLevel $trace_level ;;
458+ * ) setThisBuild traceLevel " $trace_level " ;;
456459 esac
457460}
458461
459462# set scalacOptions if we were given any -S opts
460- [[ ${# scalac_args[@]} -eq 0 ]] || addSbt " set scalacOptions in ThisBuild += \" ${scalac_args[@ ]} \" "
463+ [[ ${# scalac_args[@]} -eq 0 ]] || addSbt " set scalacOptions in ThisBuild += \" ${scalac_args[* ]} \" "
461464
462465[[ -n " $sbt_explicit_version " && -z " $sbt_new " ]] && addJava " -Dsbt.version=$sbt_explicit_version "
463466vlog " Detected sbt version $sbt_version "
464467
465468if [[ -n " $sbt_script " ]]; then
466- residual_args=( $sbt_script ${residual_args[@]} )
469+ residual_args=( " $sbt_script " " ${residual_args[@]} " )
467470else
468471 # no args - alert them there's stuff in here
469472 (( argumentCount > 0 )) || {
484487}
485488
486489# pick up completion if present; todo
490+ # shellcheck disable=SC1091
487491[[ -r .sbt_completion.sh ]] && source .sbt_completion.sh
488492
489493# directory to store sbt launchers
518522
519523if [[ -r " $jvm_opts_file " ]]; then
520524 vlog " Using jvm options defined in file $jvm_opts_file "
521- while read opt; do extra_jvm_opts+=(" $opt " ); done < <( readConfigFile " $jvm_opts_file " )
525+ while read -r opt; do extra_jvm_opts+=(" $opt " ); done < <( readConfigFile " $jvm_opts_file " )
522526elif [[ -n " $JVM_OPTS " && ! (" $JVM_OPTS " =~ ^@.* ) ]]; then
523527 vlog " Using jvm options defined in \$ JVM_OPTS variable"
524- extra_jvm_opts=( $JVM_OPTS )
528+ IFS= " " read -r -a extra_jvm_opts <<< " $JVM_OPTS"
525529else
526530 vlog " Using default jvm options"
527- extra_jvm_opts=( $( default_jvm_opts) )
531+ IFS= " " read -r -a extra_jvm_opts <<< " $(default_jvm_opts)"
528532fi
529533
530534# traceLevel is 0.12+
@@ -546,13 +550,12 @@ main () {
546550# we're not going to print those lines anyway. We strip that bit of
547551# line noise, but leave the other codes to preserve color.
548552mainFiltered () {
549- local ansiOverwrite=' \r\x1BM\x1B[2K'
550- local excludeRegex=$( egrep -v ' ^#|^$' ~ /.sbtignore | paste -sd' |' -)
553+ local -r excludeRegex=$( grep -E -v ' ^#|^$' ~ /.sbtignore | paste -sd' |' -)
551554
552555 echoLine () {
553- local line=" $1 "
554- local line1=" $( echo " $ line" | sed ' s/ \r\x1BM\x1B\[2K//g' ) " # This strips the OverwriteLine code.
555- local line2=" $( echo " $ line1" | sed ' s/ \x1B\[[0-9;]*[JKmsu]//g' ) " # This strips all codes - we test regexes against this.
556+ local -r line=" $1 "
557+ local -r line1=" ${ line// \r\x 1BM\x 1B\[ 2K// g} " # This strips the OverwriteLine code.
558+ local -r line2=" ${ line1// \x 1B\[ [0-9;]* [JKmsu]// g} " # This strips all codes - we test regexes against this.
556559
557560 if [[ $line2 =~ $excludeRegex ]]; then
558561 [[ -n $debugUs ]] && echo " [X] $line1 "
@@ -569,7 +572,7 @@ mainFiltered () {
569572# Obviously this is super ad hoc but I don't know how to improve on it. Testing whether
570573# stdin is a terminal is useless because most of my use cases for this filtering are
571574# exactly when I'm at a terminal, running sbt non-interactively.
572- shouldFilter () { [[ -f ~ /.sbtignore ]] && ! egrep -q ' \b(shell|console|consoleProject)\b' <<< " ${residual_args[@]}" ; }
575+ shouldFilter () { [[ -f ~ /.sbtignore ]] && ! grep -E -q ' \b(shell|console|consoleProject)\b' <<< " ${residual_args[@]}" ; }
573576
574577# run sbt
575578if shouldFilter; then mainFiltered; else main; fi
0 commit comments