diff --git a/CHANGES.md b/CHANGES.md index e1106fa4e..a2fbc27a3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,4 +1,12 @@ # Bijection # + +### 0.6.1 +* Add the sbt version helper script: https://github.com/twitter/bijection/pull/160 +* Added Json4s Injections: https://github.com/twitter/bijection/pull/157 +* Added url encoded String Inection: https://github.com/twitter/bijection/pull/156 +* Added bytes2bytesWritable bijection: https://github.com/twitter/bijection/pull/154 +* Update README.md: https://github.com/twitter/bijection/pull/153 + ### 0.6.0 * Update Avro: https://github.com/twitter/bijection/pull/147/files * Fix a thread-safety bug with collection Bufferables: https://github.com/twitter/bijection/pull/150 diff --git a/README.md b/README.md index 353a117df..4d2cea7f2 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Discussion occurs primarily on the [Bijection mailing list](https://groups.googl ## Maven -Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.6.0`. +Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.6.1`. Current published artifacts are diff --git a/bijection-core/src/main/scala/com/twitter/bijection/StringInjections.scala b/bijection-core/src/main/scala/com/twitter/bijection/StringInjections.scala index 5cc3675ed..920230487 100644 --- a/bijection-core/src/main/scala/com/twitter/bijection/StringInjections.scala +++ b/bijection-core/src/main/scala/com/twitter/bijection/StringInjections.scala @@ -16,13 +16,14 @@ limitations under the License. package com.twitter.bijection -import java.net.URL +import java.net.{URLDecoder, URLEncoder, URL} import java.util.UUID import scala.annotation.tailrec import scala.collection.generic.CanBuildFrom import com.twitter.bijection.Inversion.attempt +import scala.util.Try trait StringInjections extends NumericInjections { implicit val utf8: Injection[String, Array[Byte]] = withEncoding("UTF-8") @@ -46,6 +47,14 @@ trait StringInjections extends NumericInjections { def apply(uuid: UUID) = uuid.toString override def invert(s: String) = attempt(s)(UUID.fromString(_)) } + + case class URLEncodedString(encodedString: String) + + implicit val string2UrlEncodedString: Injection[String, URLEncodedString] = new AbstractInjection[String, URLEncodedString] { + override def apply(a: String): URLEncodedString = URLEncodedString(URLEncoder.encode(a, "UTF-8")) + + override def invert(b: URLEncodedString): Try[String] = attempt(b)(s => URLDecoder.decode(s.encodedString, "UTF-8")) + } } object StringCodec extends StringInjections diff --git a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseBijections.scala b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseBijections.scala index f8678d68a..43a5f7d0d 100644 --- a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseBijections.scala +++ b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseBijections.scala @@ -114,8 +114,12 @@ object HBaseBijections { implicit lazy val short2BytesWritable = ImmutableBytesWritableBijection[Short] implicit lazy val boolean2BytesWritable = ImmutableBytesWritableBijection[Boolean] implicit lazy val bigDecimal2BytesWritable = ImmutableBytesWritableBijection[BigDecimal] + implicit lazy val bytes2BytesWritable = new AbstractBijection[Array[Byte], ImmutableBytesWritable] { + override def apply(a: Array[Byte]): ImmutableBytesWritable = new ImmutableBytesWritable(a) + + override def invert(b: ImmutableBytesWritable): Array[Byte] = b.get() + } - @implicitNotFound(msg = "Cannot find Bijection type class between ${T} and [Array[Byte] @@ Rep[${T}]") object ImmutableBytesWritableBijection { def apply[T](implicit bijection: Bijection[T, Array[Byte] @@ Rep[T]]) = new ImmutableBytesWritableBijection[T](bijection) } diff --git a/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/Json4sInjections.scala b/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/Json4sInjections.scala new file mode 100644 index 000000000..17866be43 --- /dev/null +++ b/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/Json4sInjections.scala @@ -0,0 +1,60 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.twitter.bijection.json4s + +import org.json4s._ +import com.twitter.bijection.{Injection, AbstractInjection} +import org.json4s.native.JsonMethods._ +import scala.util.Try +import com.twitter.bijection.Inversion._ +import org.json4s.native.Serialization._ + +/** + * @author Mansur Ashraf + * @since 1/10/14 + */ +object Json4sInjections { + + /** + * JValue to Json Injection + */ + implicit val jvalue2Json: Injection[JValue, String] = new AbstractInjection[JValue, String] { + override def apply(a: JValue): String = compact(render(a)) + + override def invert(b: String): Try[JValue] = attempt(b)(parse(_)) + } + + /** + * Case Class to Json Injection + * @tparam A Case Class + * @return Json String + */ + implicit def caseClass2Json[A <: AnyRef](implicit mf:Manifest[A],fmt:Formats): Injection[A, String] = new AbstractInjection[A, String] { + override def apply(a: A): String = write(a) + + override def invert(b: String): Try[A] = attempt(b)(read[A]) + } + + /** + * Case Class to JValue Injection + * @tparam A Case Class + * @return JValue + */ + implicit def caseClass2JValue[A <: AnyRef ](implicit mf:Manifest[A],fmt:Formats): Injection[A, JValue] = new AbstractInjection[A, JValue] { + override def apply(a: A): JValue = Extraction.decompose(a) + + override def invert(b: JValue): Try[A] = attempt(b)(_.extract[A]) + } +} diff --git a/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/package.scala b/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/package.scala new file mode 100644 index 000000000..feb63cfc4 --- /dev/null +++ b/bijection-json4s/src/main/scala/com/twitter/bijection/json4s/package.scala @@ -0,0 +1,25 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.twitter.bijection + +import org.json4s.{NoTypeHints, native} + +/** + * @author Mansur Ashraf + * @since 1/18/14 + */ +package object json4s { + implicit val formats = native.Serialization.formats(NoTypeHints) +} diff --git a/bijection-json4s/src/test/scala/com/twitter/bijection/json4s/Json4sInjectionLaws.scala b/bijection-json4s/src/test/scala/com/twitter/bijection/json4s/Json4sInjectionLaws.scala new file mode 100644 index 000000000..3ebb20769 --- /dev/null +++ b/bijection-json4s/src/test/scala/com/twitter/bijection/json4s/Json4sInjectionLaws.scala @@ -0,0 +1,57 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.twitter.bijection.json4s + +import org.scalacheck.Properties +import com.twitter.bijection.{Injection, BaseProperties} +import Json4sInjections._ +import org.json4s.JsonAST._ +import org.json4s.JsonAST.JString + +/** + * @author Mansur Ashraf + * @since 1/10/14 + */ +object Json4sInjectionLaws extends Properties("Json4sInjection") +with BaseProperties { + case class Twit(name: String, id: Int, id_str: String, indices: List[Int], screen_name: String) + + def createTwit(i: (String, Int, String, List[Int], String)): Twit = Twit(i._1, i._2, i._3, i._4, i._5) + + implicit val testCaseClassToJson = arbitraryViaFn { + in: (String, Int, String, List[Int], String) => createTwit(in) + } + + implicit val testJValueToJson = arbitraryViaFn[(String, Int, String, List[Int], String),JValue] { + in: (String, Int, String, List[Int], String) => JObject( + List( + JField("name", JString(in._1)), + JField("id", JInt(in._2)), + JField("id_String", JString(in._3)), + JField("indices", JArray(in._4.map(JInt(_)))), + JField("screen_name", JString(in._5)) + )) + } + + def roundTripCaseClassToJson(implicit inj: Injection[Twit, String], mn: Manifest[Twit]) = isLooseInjection[Twit, String] + + def roundTripCaseClassToJValue(implicit inj: Injection[Twit, JValue], mn: Manifest[Twit]) = isLooseInjection[Twit, JValue] + + def roundTripJValueToString(implicit inj: Injection[JValue, String]) = isLooseInjection[JValue, String] + + property("round trip Case Class to Json") = roundTripCaseClassToJson + property("round trip Case Class to JValue") = roundTripCaseClassToJValue + property("round trip JValue to String") = roundTripJValueToString +} diff --git a/project/Build.scala b/project/Build.scala index 29c1c5c7d..97d726674 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -104,7 +104,7 @@ object BijectionBuild extends Build { def youngestForwardCompatible(subProj: String) = Some(subProj) .filterNot(unreleasedModules.contains(_)) - .map { s => "com.twitter" % ("bijection-" + s + "_2.9.3") % "0.5.4" } + .map { s => "com.twitter" % ("bijection-" + s + "_2.9.3") % "0.6.0" } def osgiExportAll(packs: String*) = OsgiKeys.exportPackage := packs.map(_ + ".*;version=${Bundle-Version}") @@ -237,5 +237,13 @@ object BijectionBuild extends Build { ) ).dependsOn(bijectionCore % "test->test;compile->compile") + lazy val bijectionJson4s = module("json4s").settings( + osgiExportAll("com.twitter.bijection.json4s"), + libraryDependencies ++= Seq( + "org.json4s" %% "json4s-native" % "3.2.6", + "org.json4s" %% "json4s-ext" % "3.2.6" + ) + ).dependsOn(bijectionCore % "test->test;compile->compile") + } diff --git a/sbt b/sbt new file mode 100755 index 000000000..040ce8012 --- /dev/null +++ b/sbt @@ -0,0 +1,474 @@ +#!/usr/bin/env bash +# +# A more capable sbt runner, coincidentally also called sbt. +# Author: Paul Phillips + +# todo - make this dynamic +declare -r sbt_release_version=0.13.0 + +declare sbt_jar sbt_dir sbt_create sbt_launch_dir +declare scala_version java_home sbt_explicit_version +declare verbose debug quiet noshare batch trace_level log_level +declare sbt_saved_stty + +echoerr () { [[ -z $quiet ]] && echo "$@" >&2; } +vlog () { [[ -n "$verbose$debug" ]] && echoerr "$@"; } +dlog () { [[ -n $debug ]] && echoerr "$@"; } + +# we'd like these set before we get around to properly processing arguments +for arg in "$@"; do + case $arg in + -q|-quiet) quiet=true ;; + -d|-debug) debug=true ;; + -v|-verbose) verbose=true ;; + *) ;; + esac +done + +build_props_sbt () { + if [[ -r project/build.properties ]]; then + versionLine=$(grep ^sbt.version project/build.properties | tr -d ' \r') + versionString=${versionLine##sbt.version=} + echo "$versionString" + fi +} + +update_build_props_sbt () { + local ver="$1" + local old=$(build_props_sbt) + + if [[ $ver == $old ]]; then + return + elif [[ -r project/build.properties ]]; then + perl -pi -e "s/^sbt\.version[ ]*=.*\$/sbt.version=${ver}/" project/build.properties + grep -q '^sbt.version[ ]*=' project/build.properties || printf "\nsbt.version=${ver}\n" >> project/build.properties + + echoerr !!! + echoerr !!! Updated file project/build.properties setting sbt.version to: $ver + echoerr !!! Previous value was: $old + echoerr !!! + fi +} + +sbt_version () { + if [[ -n $sbt_explicit_version ]]; then + echo $sbt_explicit_version + else + local v=$(build_props_sbt) + if [[ -n $v ]]; then + echo $v + else + echo $sbt_release_version + fi + fi +} + +# restore stty settings (echo in particular) +onSbtRunnerExit() { + [[ -n $sbt_saved_stty ]] || return + dlog "" + dlog "restoring stty: $sbt_saved_stty" + stty $sbt_saved_stty + unset sbt_saved_stty +} + +# save stty and trap exit, to ensure echo is reenabled if we are interrupted. +trap onSbtRunnerExit EXIT +sbt_saved_stty=$(stty -g 2>/dev/null) +dlog "Saved stty: $sbt_saved_stty" + +# this seems to cover the bases on OSX, and someone will +# have to tell me about the others. +get_script_path () { + local path="$1" + [[ -L "$path" ]] || { echo "$path" ; return; } + + local target=$(readlink "$path") + if [[ "${target:0:1}" == "/" ]]; then + echo "$target" + else + echo "$(dirname $path)/$target" + fi +} + +die() { + echo "Aborting: $@" + exit 1 +} + +make_url () { + version="$1" + + echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" +} + +readarr () { + while read ; do + eval "$1+=(\"$REPLY\")" + done +} + +init_default_option_file () { + local overriding_var=${!1} + local default_file=$2 + if [[ ! -r "$default_file" && $overriding_var =~ ^@(.*)$ ]]; then + local envvar_file=${BASH_REMATCH[1]} + if [[ -r $envvar_file ]]; then + default_file=$envvar_file + fi + fi + echo $default_file +} + +declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" +declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation" +declare -r default_jvm_opts="-Dfile.encoding=UTF8 -XX:MaxPermSize=384m -Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts" +declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" +declare -r latest_28="2.8.2" +declare -r latest_29="2.9.3" +declare -r latest_210="2.10.3" +declare -r latest_211="2.11.0-M5" + +declare -r script_path=$(get_script_path "$BASH_SOURCE") +declare -r script_dir="$(dirname $script_path)" +declare -r script_name="$(basename $script_path)" + +# some non-read-onlies set with defaults +declare java_cmd=java +declare sbt_opts_file=$(init_default_option_file SBT_OPTS .sbtopts) +declare jvm_opts_file=$(init_default_option_file JVM_OPTS .jvmopts) +declare sbt_launch_repo="http://typesafe.artifactoryonline.com/typesafe/ivy-releases" + +# pull -J and -D options to give to java. +declare -a residual_args +declare -a java_args +declare -a scalac_args +declare -a sbt_commands + +# args to jvm/sbt via files or environment variables +declare -a extra_jvm_opts extra_sbt_opts + +# if set, use JAVA_HOME over java found in path +[[ -e "$JAVA_HOME/bin/java" ]] && java_cmd="$JAVA_HOME/bin/java" + +# directory to store sbt launchers +declare sbt_launch_dir="$HOME/.sbt/launchers" +[[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir" +[[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers)" + +build_props_scala () { + if [[ -r project/build.properties ]]; then + versionLine=$(grep ^build.scala.versions project/build.properties) + versionString=${versionLine##build.scala.versions=} + echo ${versionString%% .*} + fi +} + +execRunner () { + # print the arguments one to a line, quoting any containing spaces + [[ $verbose || $debug ]] && echo "# Executing command line:" && { + for arg; do + if [[ -n "$arg" ]]; then + if printf "%s\n" "$arg" | grep -q ' '; then + printf "\"%s\"\n" "$arg" + else + printf "%s\n" "$arg" + fi + fi + done + echo "" + } + + if [[ -n $batch ]]; then + exec /dev/null; then + curl --fail --silent "$url" --output "$jar" + elif which wget >/dev/null; then + wget --quiet -O "$jar" "$url" + fi + } && [[ -r "$jar" ]] +} + +acquire_sbt_jar () { + for_sbt_version="$(sbt_version)" + sbt_url="$(jar_url $for_sbt_version)" + sbt_jar="$(jar_file $for_sbt_version)" + + [[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar" +} + +usage () { + cat < display stack traces with a max of frames (default: -1, traces suppressed) + -no-colors disable ANSI color codes + -sbt-create start sbt even if current directory contains no sbt project + -sbt-dir path to global settings/plugins directory (default: ~/.sbt/) + -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11+) + -ivy path to local Ivy repository (default: ~/.ivy2) + -no-share use all local caches; no sharing + -offline put sbt in offline mode + -jvm-debug Turn on JVM debugging, open at the given port. + -batch Disable interactive mode + -prompt Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted + + # sbt version (default: from project/build.properties if present, else latest release) + !!! The only way to accomplish this pre-0.12.0 if there is a build.properties file which + !!! contains an sbt.version property is to update the file on disk. That's what this does. + -sbt-version use the specified version of sbt (default: $sbt_release_version) + -sbt-jar use the specified jar as the sbt launcher + -sbt-launch-dir directory to hold sbt launchers (default: $sbt_launch_dir) + -sbt-launch-repo repo url for downloading sbt launcher jar (default: $sbt_launch_repo) + + # scala version (default: as chosen by sbt) + -28 use $latest_28 + -29 use $latest_29 + -210 use $latest_210 + -211 use $latest_211 + -scala-home use the scala build at the specified directory + -scala-version use the specified version of scala + -binary-version use the specified scala version when searching for dependencies + + # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) + -java-home alternate JAVA_HOME + + # passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution + # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found + $default_jvm_opts + JVM_OPTS environment variable holding either the jvm args directly, or + the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts') + Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument. + -jvm-opts file containing jvm args (if not given, .jvmopts in project root is used if present) + -Dkey=val pass -Dkey=val directly to the jvm + -J-X pass option -X directly to the jvm (-J is stripped) + + # passing options to sbt, OR to this runner + SBT_OPTS environment variable holding either the sbt args directly, or + the reference to a file containing sbt args if given path is prepended by '@' (e.g. '@/etc/sbtopts') + Note: "@"-file is overridden by local '.sbtopts' or '-sbt-opts' argument. + -sbt-opts file containing sbt args (if not given, .sbtopts in project root is used if present) + -S-X add -X to sbt's scalacOptions (-S is stripped) +EOM +} + +addJava () { + dlog "[addJava] arg = '$1'" + java_args=( "${java_args[@]}" "$1" ) +} +addSbt () { + dlog "[addSbt] arg = '$1'" + sbt_commands=( "${sbt_commands[@]}" "$1" ) +} +addScalac () { + dlog "[addScalac] arg = '$1'" + scalac_args=( "${scalac_args[@]}" "$1" ) +} +addResidual () { + dlog "[residual] arg = '$1'" + residual_args=( "${residual_args[@]}" "$1" ) +} +addResolver () { + addSbt "set resolvers += $1" +} +addDebugger () { + addJava "-Xdebug" + addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" +} +setScalaVersion () { + [[ "$1" == *-SNAPSHOT ]] && addResolver 'Resolver.sonatypeRepo("snapshots")' + addSbt "++ $1" +} + +process_args () +{ + require_arg () { + local type="$1" + local opt="$2" + local arg="$3" + + if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then + die "$opt requires <$type> argument" + fi + } + while [[ $# -gt 0 ]]; do + case "$1" in + -h|-help) usage; exit 1 ;; + -v|-verbose) verbose=true && log_level=Info && shift ;; + -d|-debug) debug=true && log_level=Debug && shift ;; + -q|-quiet) quiet=true && log_level=Error && shift ;; + + -trace) require_arg integer "$1" "$2" && trace_level=$2 && shift 2 ;; + -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; + -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; + -no-share) noshare=true && shift ;; + -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; + -sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;; + -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; + -offline) addSbt "set offline := true" && shift ;; + -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; + -batch) batch=true && shift ;; + -prompt) require_arg "expr" "$1" "$2" && addSbt "set shellPrompt in ThisBuild := (s => { val e = Project.extract(s) ; $2 })" && shift 2 ;; + + -sbt-create) sbt_create=true && shift ;; + -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;; + -sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;; +-sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;; +-sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;; + -scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;; +-binary-version) require_arg version "$1" "$2" && addSbt "set scalaBinaryVersion in ThisBuild := \"$2\"" && shift 2 ;; + -scala-home) require_arg path "$1" "$2" && addSbt "set every scalaHome := Some(file(\"$2\"))" && shift 2 ;; + -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;; + -sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;; + -jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;; + + -D*) addJava "$1" && shift ;; + -J*) addJava "${1:2}" && shift ;; + -S*) addScalac "${1:2}" && shift ;; + -28) setScalaVersion $latest_28 && shift ;; + -29) setScalaVersion $latest_29 && shift ;; + -210) setScalaVersion $latest_210 && shift ;; + -211) setScalaVersion $latest_211 && shift ;; + + *) addResidual "$1" && shift ;; + esac + done +} + +# process the direct command line arguments +process_args "$@" + +# skip #-styled comments +readConfigFile() { + while read line; do echo ${line/\#*/} | grep -vE '^\s*$'; done < $1 +} + +# if there are file/environment sbt_opts, process again so we +# can supply args to this runner +if [[ -r "$sbt_opts_file" ]]; then + vlog "Using sbt options defined in file $sbt_opts_file" + readarr extra_sbt_opts < <(readConfigFile "$sbt_opts_file") +elif [[ -n "$SBT_OPTS" && !($SBT_OPTS =~ ^@.*) ]]; then + vlog "Using sbt options defined in variable \$SBT_OPTS" + extra_sbt_opts=( $SBT_OPTS ) +else + vlog "No extra sbt options have been defined" +fi + +[[ -n $extra_sbt_opts ]] && process_args "${extra_sbt_opts[@]}" + +# reset "$@" to the residual args +set -- "${residual_args[@]}" +argumentCount=$# + +# only exists in 0.12+ +setTraceLevel() { + case $(sbt_version) in + 0.{7,10,11}.*) echoerr "Cannot set trace level in sbt version $(sbt_version)" ;; + *) addSbt "set every traceLevel := $trace_level" ;; + esac +} + +# set scalacOptions if we were given any -S opts +[[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[@]}\"" + +# Update build.properties on disk to set explicit version - sbt gives us no choice +[[ -n "$sbt_explicit_version" ]] && update_build_props_sbt "$sbt_explicit_version" +vlog "Detected sbt version $(sbt_version)" + +[[ -n "$scala_version" ]] && echoerr "Overriding scala version to $scala_version" + +# no args - alert them there's stuff in here +(( $argumentCount > 0 )) || { + vlog "Starting $script_name: invoke with -help for other options" + residual_args=( shell ) +} + +# verify this is an sbt dir or -create was given +[[ -r ./build.sbt || -d ./project || -n "$sbt_create" ]] || { + cat <