Merge branch 'feature/develop_2.0/scala_2.11' of github.com:berngp/swagger-codegen into berngp-feature/develop_2.0/scala_2.11

This commit is contained in:
Tony Tam 2014-10-17 14:28:58 -07:00
commit bf3eb94c51
7 changed files with 392 additions and 273 deletions

49
pom.xml
View File

@ -191,7 +191,7 @@
<artifactId>maven-release-plugin</artifactId> <artifactId>maven-release-plugin</artifactId>
<version>2.1</version> <version>2.1</version>
</plugin> </plugin>
</plugins> </plugins>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
@ -290,6 +290,51 @@
</plugins> </plugins>
</build> </build>
</profile> </profile>
<profile>
<id>gen-scalatra</id>
<activation>
<property>
<name>template</name>
<value>scalatra</value>
</property>
</activation>
<build>
<defaultGoal>compile exec:java</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<mainClass>com.wordnik.swagger.codegen.Codegen</mainClass>
<arguments>
<argument>-l</argument>
<argument>scalatra</argument>
<argument>-i</argument>
<argument>${swagger-spec}</argument>
<argument>-t</argument>
<argument>${swagger-template}</argument>
<argument>-o</argument>
<argument>${swagger-out}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<swagger-spec>samples/yaml/echo.yaml</swagger-spec>
<swagger-template>src/main/resources/scalatra</swagger-template>
<swagger-out>generated-code/scalatra</swagger-out>
</properties>
</profile>
</profiles> </profiles>
<reporting> <reporting>
<outputDirectory>target/site</outputDirectory> <outputDirectory>target/site</outputDirectory>
@ -342,7 +387,6 @@
<version>${swagger-core-version}</version> <version>${swagger-core-version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.samskivert</groupId> <groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId> <artifactId>jmustache</artifactId>
@ -408,7 +452,6 @@
<scala-test-version>2.1.4</scala-test-version> <scala-test-version>2.1.4</scala-test-version>
<commons-io-version>2.3</commons-io-version> <commons-io-version>2.3</commons-io-version>
<commons-cli-version>1.2</commons-cli-version> <commons-cli-version>1.2</commons-cli-version>
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version> <maven-plugin-version>1.0.0</maven-plugin-version>
<commons-lang-version>2.4</commons-lang-version> <commons-lang-version>2.4</commons-lang-version>

51
samples/yaml/echo.yaml Normal file
View File

@ -0,0 +1,51 @@
swagger: 2
info:
title: Echo API
description: Simple Rest Echo
version: "1.0.0"
host: "localhost:8002"
schemes:
- http
basePath: /v1
produces:
- application/json
paths:
/echo:
get:
description: "Returns the 'message' to the caller"
operationId: "echo"
parameters:
#- name: X-header-param
- name: headerParam
in: header
type: string
required: false
- name: message
in: query
type: string
required: true
responses:
200:
description: "Success"
schema:
$ref: EchoResponse
default:
description: "Error"
schema:
$ref: Error
definitions:
EchoResponse:
required:
- message
properties:
message:
type: string
Error:
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string

View File

@ -1,32 +1,43 @@
import org.eclipse.jetty.server.nio.SelectChannelConnector import org.eclipse.jetty.server._
import org.eclipse.jetty.server.{ Server }
import org.eclipse.jetty.server.handler.ContextHandlerCollection
import org.eclipse.jetty.webapp.WebAppContext import org.eclipse.jetty.webapp.WebAppContext
import org.eclipse.jetty.servlet.{ DefaultServlet, ServletContextHandler, ServletHolder } import org.scalatra.servlet.ScalatraListener
object JettyMain { object JettyMain {
object conf {
val port = sys.env.get("PORT") map (_.toInt) getOrElse (8080)
val stopTimeout = sys.env.get("STOP_TIMEOUT") map (_.toInt) getOrElse (5000)
val connectorIdleTimeout = sys.env.get("CONNECTOR_IDLE_TIMEOUT") map (_.toInt) getOrElse (90000)
val webapp = sys.env.get("PUBLIC") getOrElse "webapp"
val contextPath = sys.env.get("CONTEXT_PATH") getOrElse "/"
}
def main(args: Array[String]) = { def main(args: Array[String]) = {
val server: Server = new Server val server: Server = new Server
println("starting jetty") println("starting jetty")
server setGracefulShutdown 5000 server setStopTimeout conf.stopTimeout
server setSendServerVersion false //server setDumpAfterStart true
server setSendDateHeader true
server setStopAtShutdown true server setStopAtShutdown true
val connector = new SelectChannelConnector val httpConfig = new HttpConfiguration()
connector setPort sys.env.get("PORT").map(_.toInt).getOrElse(8080) httpConfig setSendDateHeader true
connector setMaxIdleTime 90000 httpConfig setSendServerVersion false
val connector = new NetworkTrafficServerConnector(server, new HttpConnectionFactory(httpConfig))
connector setPort conf.port
connector setSoLingerTime 0
connector setIdleTimeout conf.connectorIdleTimeout
server addConnector connector server addConnector connector
val webapp = sys.env.get("PUBLIC") getOrElse "webapp" val webapp = conf.webapp
val webApp = new WebAppContext val webApp = new WebAppContext
webApp setContextPath "/" webApp setContextPath conf.contextPath
webApp setResourceBase webapp webApp setResourceBase conf.webapp
webApp setDescriptor (webapp+"/WEB-INF/web.xml"); webApp setEventListeners Array(new ScalatraListener)
server setHandler webApp server setHandler webApp
server.start() server.start()
} }
} }

View File

@ -18,24 +18,27 @@ name := "scalatra-sample"
version := "0.1.0-SNAPSHOT" version := "0.1.0-SNAPSHOT"
scalaVersion := "2.10.0" scalaVersion := "2.11.2"
scalacOptions += "-language:postfixOps"
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.0" % "test", "org.scalatest" %% "scalatest" % "2.2.1" % "test",
"org.scalatra" %% "scalatra" % "2.3.0.RC3", "org.scalatra" %% "scalatra" % "2.3.0.RC3",
"org.scalatra" %% "scalatra-scalate" % "2.3.0.RC3", "org.scalatra" %% "scalatra-scalate" % "2.3.0.RC3",
"org.scalatra" %% "scalatra-json" % "2.3.0.RC3", "org.scalatra" %% "scalatra-json" % "2.3.0.RC3",
"org.scalatra" %% "scalatra-swagger" % "2.3.0.RC3", "org.scalatra" %% "scalatra-swagger" % "2.3.0.RC3",
"org.scalatra" %% "scalatra-swagger-ext" % "2.3.0.RC3", "org.scalatra" %% "scalatra-swagger-ext" % "2.3.0.RC3",
"org.scalatra" %% "scalatra-slf4j" % "2.3.0.RC3", "org.scalatra" %% "scalatra-slf4j" % "2.3.0.RC3",
"org.json4s" %% "json4s-jackson" % "3.1.0", "org.json4s" %% "json4s-jackson" % "3.2.10",
"org.json4s" %% "json4s-ext" % "3.1.0", "org.json4s" %% "json4s-ext" % "3.2.10",
"commons-codec" % "commons-codec" % "1.7", "commons-codec" % "commons-codec" % "1.7",
"net.databinder.dispatch" %% "dispatch-core" % "0.9.5", "net.databinder.dispatch" %% "dispatch-core" % "0.11.2",
"net.databinder.dispatch" %% "json4s-jackson" % "0.9.5", //"net.databinder.dispatch" %% "json4s-jackson" % "0.11.2",
"com.typesafe.akka" %% "akka-actor" % "2.1.0", "net.databinder.dispatch" %% "dispatch-json4s-jackson" % "0.11.2",
"org.eclipse.jetty" % "jetty-server" % "8.1.7.v20120910" % "container;provided", "com.typesafe.akka" %% "akka-actor" % "2.3.6",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container;provided", "org.eclipse.jetty" % "jetty-server" % "9.2.3.v20140905" % "container;compile;test",
"org.eclipse.jetty" % "jetty-webapp" % "9.2.3.v20140905" % "container;compile;test",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;compile;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")) "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;compile;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
) )
@ -51,9 +54,11 @@ ivyXML := <dependencies>
<exclude module="jsr311-api" /> <exclude module="jsr311-api" />
</dependencies> </dependencies>
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) => mergeStrategy in assembly <<= (mergeStrategy in assembly) {
{ (old) => {
case "about.html" => MergeStrategy.discard case "about.html" => MergeStrategy.discard
case x => old(x) case x => old(x)
} }
} }
net.virtualvoid.sbt.graph.Plugin.graphSettings

View File

@ -1 +1 @@
sbt.version=0.13.0 sbt.version=0.13.5

View File

@ -1,7 +1,9 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1") addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.6.4") addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.7.5")
addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.6.0") addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.9.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.1") addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.4")

View File

@ -4,80 +4,58 @@
# Author: Paul Phillips <paulp@typesafe.com> # Author: Paul Phillips <paulp@typesafe.com>
# todo - make this dynamic # todo - make this dynamic
declare -r sbt_release_version=0.12.4 declare -r sbt_release_version="0.13.6"
declare -r sbt_beta_version=0.13.0-RC4 declare -r sbt_unreleased_version="0.13.6"
declare -r sbt_snapshot_version=0.13.0-SNAPSHOT declare -r buildProps="project/build.properties"
declare sbt_jar sbt_dir sbt_create sbt_snapshot sbt_launch_dir declare sbt_jar sbt_dir sbt_create sbt_version
declare scala_version java_home sbt_explicit_version declare scala_version sbt_explicit_version
declare verbose debug quiet noshare batch trace_level log_level declare verbose noshare batch trace_level log_level
declare sbt_saved_stty declare sbt_saved_stty debugUs
echoerr () { [[ -z $quiet ]] && echo "$@" >&2; } echoerr () { echo >&2 "$@"; }
vlog () { [[ -n "$verbose$debug" ]] && echoerr "$@"; } vlog () { [[ -n "$verbose" ]] && 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
# spaces are possible, e.g. sbt.version = 0.13.0
build_props_sbt () { build_props_sbt () {
if [[ -r project/build.properties ]]; then [[ -r "$buildProps" ]] && \
versionLine=$(grep ^sbt.version project/build.properties | tr -d '\r') grep '^sbt\.version' "$buildProps" | tr '=' ' ' | awk '{ print $2; }'
versionString=${versionLine##sbt.version=}
echo "$versionString"
fi
} }
update_build_props_sbt () { update_build_props_sbt () {
local ver="$1" local ver="$1"
local old=$(build_props_sbt) local old="$(build_props_sbt)"
if [[ $ver == $old ]]; then [[ -r "$buildProps" ]] && [[ "$ver" != "$old" ]] && {
return perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps"
elif [[ -r project/build.properties ]]; then grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps"
perl -pi -e "s/^sbt\.version=.*\$/sbt.version=${ver}/" project/build.properties
grep -q '^sbt.version=' project/build.properties || echo "sbt.version=${ver}" >> project/build.properties
echoerr !!! vlog "!!!"
echoerr !!! Updated file project/build.properties setting sbt.version to: $ver vlog "!!! Updated file $buildProps setting sbt.version to: $ver"
echoerr !!! Previous value was: $old vlog "!!! Previous value was: $old"
echoerr !!! vlog "!!!"
fi }
} }
sbt_version () { set_sbt_version () {
if [[ -n $sbt_explicit_version ]]; then sbt_version="${sbt_explicit_version:-$(build_props_sbt)}"
echo $sbt_explicit_version [[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version
else export sbt_version
local v=$(build_props_sbt)
if [[ -n $v ]]; then
echo $v
else
echo $sbt_release_version
fi
fi
} }
# restore stty settings (echo in particular) # restore stty settings (echo in particular)
onSbtRunnerExit() { onSbtRunnerExit() {
[[ -n $sbt_saved_stty ]] || return [[ -n "$sbt_saved_stty" ]] || return
dlog "" vlog ""
dlog "restoring stty: $sbt_saved_stty" vlog "restoring stty: $sbt_saved_stty"
stty $sbt_saved_stty stty "$sbt_saved_stty"
unset sbt_saved_stty unset sbt_saved_stty
} }
# save stty and trap exit, to ensure echo is reenabled if we are interrupted. # save stty and trap exit, to ensure echo is reenabled if we are interrupted.
trap onSbtRunnerExit EXIT trap onSbtRunnerExit EXIT
sbt_saved_stty=$(stty -g 2>/dev/null) sbt_saved_stty="$(stty -g 2>/dev/null)"
dlog "Saved stty: $sbt_saved_stty" vlog "Saved stty: $sbt_saved_stty"
# this seems to cover the bases on OSX, and someone will # this seems to cover the bases on OSX, and someone will
# have to tell me about the others. # have to tell me about the others.
@ -85,11 +63,11 @@ get_script_path () {
local path="$1" local path="$1"
[[ -L "$path" ]] || { echo "$path" ; return; } [[ -L "$path" ]] || { echo "$path" ; return; }
local target=$(readlink "$path") local target="$(readlink "$path")"
if [[ "${target:0:1}" == "/" ]]; then if [[ "${target:0:1}" == "/" ]]; then
echo "$target" echo "$target"
else else
echo "$(dirname $path)/$target" echo "${path%/*}/$target"
fi fi
} }
@ -99,45 +77,45 @@ die() {
} }
make_url () { make_url () {
groupid="$1" version="$1"
category="$2"
version="$3"
echo "http://typesafe.artifactoryonline.com/typesafe/ivy-$category/$groupid/sbt-launch/$version/sbt-launch.jar" case "$version" in
} 0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;;
0.10.* ) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
readarr () { 0.11.[12]) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
while read ; do *) echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;;
eval "$1+=(\"$REPLY\")" esac
done
} }
init_default_option_file () { init_default_option_file () {
local overriding_var=${!1} local overriding_var="${!1}"
local default_file=$2 local default_file="$2"
if [[ ! -r "$default_file" && $overriding_var =~ ^@(.*)$ ]]; then if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then
local envvar_file=${BASH_REMATCH[1]} local envvar_file="${BASH_REMATCH[1]}"
if [[ -r $envvar_file ]]; then if [[ -r "$envvar_file" ]]; then
default_file=$envvar_file default_file="$envvar_file"
fi fi
fi fi
echo $default_file echo "$default_file"
} }
declare -r default_jvm_opts="-Dfile.encoding=UTF8 -XX:MaxPermSize=256m -Xms512m -Xmx1g -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation"
declare -r default_jvm_opts_common="-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 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_28="2.8.2"
declare -r latest_29="2.9.3" declare -r latest_29="2.9.3"
declare -r latest_210="2.10.0" declare -r latest_210="2.10.4"
declare -r latest_211="2.11.2"
declare -r script_path=$(get_script_path "$BASH_SOURCE") declare -r script_path="$(get_script_path "$BASH_SOURCE")"
declare -r script_dir="$(dirname $script_path)" declare -r script_name="${script_path##*/}"
declare -r script_name="$(basename $script_path)"
# some non-read-onlies set with defaults # some non-read-onlies set with defaults
declare java_cmd=java declare java_cmd="java"
declare sbt_opts_file=$(init_default_option_file SBT_OPTS .sbtopts) declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)"
declare jvm_opts_file=$(init_default_option_file JVM_OPTS .jvmopts) 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. # pull -J and -D options to give to java.
declare -a residual_args declare -a residual_args
@ -154,104 +132,68 @@ declare -a extra_jvm_opts extra_sbt_opts
# directory to store sbt launchers # directory to store sbt launchers
declare sbt_launch_dir="$HOME/.sbt/launchers" declare sbt_launch_dir="$HOME/.sbt/launchers"
[[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir" [[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir"
[[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers)" [[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)"
java_version () {
local version=$("$java_cmd" -version 2>&1 | grep -e 'java version' | awk '{ print $3 }' | tr -d \")
vlog "Detected Java version: $version"
echo "${version:2:1}"
}
# MaxPermSize critical on pre-8 jvms but incurs noisy warning on 8+
default_jvm_opts () {
local v="$(java_version)"
if [[ $v -ge 8 ]]; then
echo "$default_jvm_opts_common"
else
echo "-XX:MaxPermSize=384m $default_jvm_opts_common"
fi
}
build_props_scala () { build_props_scala () {
if [[ -r project/build.properties ]]; then if [[ -r "$buildProps" ]]; then
versionLine=$(grep ^build.scala.versions project/build.properties) versionLine="$(grep '^build.scala.versions' "$buildProps")"
versionString=${versionLine##build.scala.versions=} versionString="${versionLine##build.scala.versions=}"
echo ${versionString%% .*} echo "${versionString%% .*}"
fi fi
} }
execRunner () { execRunner () {
# print the arguments one to a line, quoting any containing spaces # print the arguments one to a line, quoting any containing spaces
[[ $verbose || $debug ]] && echo "# Executing command line:" && { vlog "# Executing command line:" && {
for arg; do for arg; do
if [[ -n "$arg" ]]; then if [[ -n "$arg" ]]; then
if printf "%s\n" "$arg" | grep -q ' '; then if printf "%s\n" "$arg" | grep -q ' '; then
printf "\"%s\"\n" "$arg" printf >&2 "\"%s\"\n" "$arg"
else else
printf "%s\n" "$arg" printf >&2 "%s\n" "$arg"
fi fi
fi fi
done done
echo "" vlog ""
} }
if [[ -n $batch ]]; then [[ -n "$batch" ]] && exec </dev/null
# the only effective way I've found to avoid sbt hanging when backgrounded. exec "$@"
exec 0<&-
( "$@" & )
# I'm sure there's some way to get our hands on the pid and wait for it
# but it exceeds my present level of ambition.
else
{ "$@"; }
fi
}
sbt_groupid () {
case $(sbt_version) in
0.7.*) echo org.scala-tools.sbt ;;
0.10.*) echo org.scala-tools.sbt ;;
0.11.[12]) echo org.scala-tools.sbt ;;
*) echo org.scala-sbt ;;
esac
}
sbt_artifactory_list () {
local version0=$(sbt_version)
local version=${version0%-SNAPSHOT}
local url="http://typesafe.artifactoryonline.com/typesafe/ivy-snapshots/$(sbt_groupid)/sbt-launch/"
dlog "Looking for snapshot list at: $url "
curl -s --list-only "$url" | \
grep -F $version | \
perl -e 'print reverse <>' | \
perl -pe 's#^<a href="([^"/]+).*#$1#;'
}
make_release_url () {
make_url $(sbt_groupid) releases $(sbt_version)
}
# argument is e.g. 0.13.0-SNAPSHOT
# finds the actual version (with the build id) at artifactory
make_snapshot_url () {
for ver in $(sbt_artifactory_list); do
local url=$(make_url $(sbt_groupid) snapshots $ver)
dlog "Testing $url"
curl -s --head "$url" >/dev/null
dlog "curl returned: $?"
echo "$url"
return
done
} }
jar_url () { jar_url () {
case $(sbt_version) in make_url "$1"
0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;;
*-SNAPSHOT) make_snapshot_url ;;
*) make_release_url ;;
esac
} }
jar_file () { jar_file () {
case $1 in echo "$sbt_launch_dir/$1/sbt-launch.jar"
0.13.*) echo "$sbt_launch_dir/$1/sbt-launch.jar" ;;
*) echo "$sbt_launch_dir/$sbt_release_version/sbt-launch.jar" ;;
esac
} }
download_url () { download_url () {
local url="$1" local url="$1"
local jar="$2" local jar="$2"
echo "Downloading sbt launcher $(sbt_version):" echoerr "Downloading sbt launcher for $sbt_version:"
echo " From $url" echoerr " From $url"
echo " To $jar" echoerr " To $jar"
mkdir -p $(dirname "$jar") && { mkdir -p "${jar%/*}" && {
if which curl >/dev/null; then if which curl >/dev/null; then
curl --fail --silent "$url" --output "$jar" curl --fail --silent "$url" --output "$jar"
elif which wget >/dev/null; then elif which wget >/dev/null; then
@ -261,8 +203,8 @@ download_url () {
} }
acquire_sbt_jar () { acquire_sbt_jar () {
sbt_url="$(jar_url)" sbt_url="$(jar_url "$sbt_version")"
sbt_jar="$(jar_file $(sbt_version))" sbt_jar="$(jar_file "$sbt_version")"
[[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar" [[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar"
} }
@ -271,11 +213,23 @@ usage () {
cat <<EOM cat <<EOM
Usage: $script_name [options] Usage: $script_name [options]
Note that options which are passed along to sbt begin with -- whereas
options to this runner use a single dash. Any sbt command can be scheduled
to run first by prefixing the command with --, so --warn, --error and so on
are not special.
Output filtering: if there is a file in the home directory called .sbtignore
and this is not an interactive sbt session, the file is treated as a list of
bash regular expressions. Output lines which match any regex are not echoed.
One can see exactly which lines would have been suppressed by starting this
runner with the -x option.
-h | -help print this message -h | -help print this message
-v | -verbose this runner is chattier -v verbose operation (this runner is chattier)
-d | -debug set sbt log level to Debug -d, -w, -q aliases for --debug, --warn, --error (q means quiet)
-q | -quiet set sbt log level to Error -x debug this script
-trace <level> display stack traces with a max of <level> frames (default: -1, traces suppressed) -trace <level> display stack traces with a max of <level> frames (default: -1, traces suppressed)
-debug-inc enable debugging log for the incremental compiler
-no-colors disable ANSI color codes -no-colors disable ANSI color codes
-sbt-create start sbt even if current directory contains no sbt project -sbt-create start sbt even if current directory contains no sbt project
-sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt/<version>) -sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt/<version>)
@ -287,19 +241,19 @@ Usage: $script_name [options]
-batch Disable interactive mode -batch Disable interactive mode
-prompt <expr> Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted -prompt <expr> 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) # sbt version (default: sbt.version from $buildProps if present, otherwise $sbt_release_version)
!!! The only way to accomplish this pre-0.12.0 if there is a build.properties file which -sbt-force-latest force the use of the latest release of sbt: $sbt_release_version
!!! contains an sbt.version property is to update the file on disk. That's what this does.
-sbt-version <version> use the specified version of sbt (default: $sbt_release_version) -sbt-version <version> use the specified version of sbt (default: $sbt_release_version)
-sbt-dev use the latest pre-release version of sbt: $sbt_unreleased_version
-sbt-jar <path> use the specified jar as the sbt launcher -sbt-jar <path> use the specified jar as the sbt launcher
-sbt-beta use a beta version of sbt (currently: $sbt_beta_version) -sbt-launch-dir <path> directory to hold sbt launchers (default: ~/.sbt/launchers)
-sbt-snapshot use a snapshot version of sbt (currently: $sbt_snapshot_version) -sbt-launch-repo <url> repo url for downloading sbt launcher jar (default: $sbt_launch_repo)
-sbt-launch-dir <path> directory to hold sbt launchers (default: $sbt_launch_dir)
# scala version (default: as chosen by sbt) # scala version (default: as chosen by sbt)
-28 use $latest_28 -28 use $latest_28
-29 use $latest_29 -29 use $latest_29
-210 use $latest_210 -210 use $latest_210
-211 use $latest_211
-scala-home <path> use the scala build at the specified directory -scala-home <path> use the scala build at the specified directory
-scala-version <version> use the specified version of scala -scala-version <version> use the specified version of scala
-binary-version <version> use the specified scala version when searching for dependencies -binary-version <version> use the specified scala version when searching for dependencies
@ -309,7 +263,7 @@ Usage: $script_name [options]
# passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution # 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 # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found
<default> $default_jvm_opts <default> $(default_jvm_opts)
JVM_OPTS environment variable holding either the jvm args directly, or 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') 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. Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument.
@ -327,33 +281,37 @@ EOM
} }
addJava () { addJava () {
dlog "[addJava] arg = '$1'" vlog "[addJava] arg = '$1'"
java_args=( "${java_args[@]}" "$1" ) java_args=( "${java_args[@]}" "$1" )
} }
addSbt () { addSbt () {
dlog "[addSbt] arg = '$1'" vlog "[addSbt] arg = '$1'"
sbt_commands=( "${sbt_commands[@]}" "$1" ) sbt_commands=( "${sbt_commands[@]}" "$1" )
} }
setThisBuild () {
vlog "[addBuild] args = '$@'"
local key="$1" && shift
addSbt "set $key in ThisBuild := $@"
}
addScalac () { addScalac () {
dlog "[addScalac] arg = '$1'" vlog "[addScalac] arg = '$1'"
scalac_args=( "${scalac_args[@]}" "$1" ) scalac_args=( "${scalac_args[@]}" "$1" )
} }
addResidual () { addResidual () {
dlog "[residual] arg = '$1'" vlog "[residual] arg = '$1'"
residual_args=( "${residual_args[@]}" "$1" ) residual_args=( "${residual_args[@]}" "$1" )
} }
addResolver () { addResolver () {
addSbt "set resolvers in ThisBuild += $1" addSbt "set resolvers += $1"
} }
addDebugger () { addDebugger () {
addJava "-Xdebug" addJava "-Xdebug"
addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
} }
setScalaVersion () { setScalaVersion () {
addSbt "set scalaVersion in ThisBuild := \"$1\"" [[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
if [[ "$1" == *SNAPSHOT* ]]; then addSbt "++ $1"
addResolver Opts.resolver.sonatypeSnapshots
fi
} }
process_args () process_args ()
@ -369,94 +327,104 @@ process_args ()
} }
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
-h|-help) usage; exit 1 ;; -h|-help) usage; exit 1 ;;
-v|-verbose) verbose=true && log_level=Info && shift ;; -v) verbose=true && shift ;;
-d|-debug) debug=true && log_level=Debug && shift ;; -d) addSbt "--debug" && shift ;;
-q|-quiet) quiet=true && log_level=Error && shift ;; -w) addSbt "--warn" && shift ;;
-q) addSbt "--error" && shift ;;
-x) debugUs=true && 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" && setThisBuild shellPrompt "(s => { val e = Project.extract(s) ; $2 })" && shift 2 ;;
-trace) require_arg integer "$1" "$2" && trace_level=$2 && shift 2 ;; -sbt-create) sbt_create=true && shift ;;
-ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
-no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; -sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;;
-no-share) noshare=true && shift ;; -sbt-force-latest) sbt_explicit_version="$sbt_release_version" && shift ;;
-sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; -sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;;
-sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;; -sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;;
-debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; -sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;;
-offline) addSbt "set offline := true" && shift ;; -scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;;
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; -binary-version) require_arg version "$1" "$2" && setThisBuild scalaBinaryVersion "\"$2\"" && shift 2 ;;
-batch) batch=true && shift ;; -scala-home) require_arg path "$1" "$2" && setThisBuild scalaHome "Some(file(\"$2\"))" && shift 2 ;;
-prompt) require_arg "expr" "$1" "$2" && addSbt "set shellPrompt in ThisBuild := (s => { val e = Project.extract(s) ; $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 ;;
-sbt-create) sbt_create=true && shift ;; -D*) addJava "$1" && shift ;;
-sbt-snapshot) sbt_explicit_version=$sbt_snapshot_version && shift ;; -J*) addJava "${1:2}" && shift ;;
-sbt-beta) sbt_explicit_version=$sbt_beta_version && shift ;; -S*) addScalac "${1:2}" && shift ;;
-sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;; -28) setScalaVersion "$latest_28" && shift ;;
-sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;; -29) setScalaVersion "$latest_29" && shift ;;
-sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;; -210) setScalaVersion "$latest_210" && shift ;;
-scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;; -211) setScalaVersion "$latest_211" && shift ;;
-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 ;; *) addResidual "$1" && shift ;;
-J*) addJava "${1:2}" && shift ;;
-S*) addScalac "${1:2}" && shift ;;
-28) addSbt "++ $latest_28" && shift ;;
-29) addSbt "++ $latest_29" && shift ;;
-210) addSbt "++ $latest_210" && shift ;;
*) addResidual "$1" && shift ;;
esac esac
done done
} }
# process the direct command line arguments # process the direct command line arguments
process_args "$@" process_args "$@"
# skip #-styled comments # skip #-styled comments and blank lines
readConfigFile() { readConfigFile() {
while read line; do echo ${line/\#*/} | grep -vE '^\s*$'; done < $1 while read line; do
[[ $line =~ ^# ]] || [[ -z $line ]] || echo "$line"
done < "$1"
} }
# if there are file/environment sbt_opts, process again so we # if there are file/environment sbt_opts, process again so we
# can supply args to this runner # can supply args to this runner
if [[ -r "$sbt_opts_file" ]]; then if [[ -r "$sbt_opts_file" ]]; then
vlog "Using sbt options defined in file $sbt_opts_file" vlog "Using sbt options defined in file $sbt_opts_file"
readarr extra_sbt_opts < <(readConfigFile "$sbt_opts_file") while read opt; do extra_sbt_opts+=("$opt"); done < <(readConfigFile "$sbt_opts_file")
elif [[ -n "$SBT_OPTS" && !($SBT_OPTS =~ ^@.*) ]]; then elif [[ -n "$SBT_OPTS" && ! ("$SBT_OPTS" =~ ^@.*) ]]; then
vlog "Using sbt options defined in variable \$SBT_OPTS" vlog "Using sbt options defined in variable \$SBT_OPTS"
extra_sbt_opts=( $SBT_OPTS ) extra_sbt_opts=( $SBT_OPTS )
else else
vlog "No extra sbt options have been defined" vlog "No extra sbt options have been defined"
fi fi
[[ -n $extra_sbt_opts ]] && process_args "${extra_sbt_opts[@]}" [[ -n "${extra_sbt_opts[*]}" ]] && process_args "${extra_sbt_opts[@]}"
# reset "$@" to the residual args # reset "$@" to the residual args
set -- "${residual_args[@]}" set -- "${residual_args[@]}"
argumentCount=$# argumentCount=$#
# set sbt version
set_sbt_version
# only exists in 0.12+ # only exists in 0.12+
setTraceLevel() { setTraceLevel() {
case $(sbt_version) in case "$sbt_version" in
0.{7,10,11}.*) echoerr "Cannot set trace level in sbt version $(sbt_version)" ;; "0.7."* | "0.10."* | "0.11."* ) echoerr "Cannot set trace level in sbt version $sbt_version" ;;
*) addSbt "set every traceLevel := $trace_level" ;; *) setThisBuild traceLevel $trace_level ;;
esac esac
} }
# set scalacOptions if we were given any -S opts # set scalacOptions if we were given any -S opts
[[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[@]}\"" [[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[@]}\""
# Update build.properties no disk to set explicit version - sbt gives us no choice # 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" [[ -n "$sbt_explicit_version" ]] && update_build_props_sbt "$sbt_explicit_version"
vlog "Detected sbt version $(sbt_version)" vlog "Detected sbt version $sbt_version"
[[ -n "$scala_version" ]] && echoerr "Overriding scala version to $scala_version" [[ -n "$scala_version" ]] && vlog "Overriding scala version to $scala_version"
# no args - alert them there's stuff in here # no args - alert them there's stuff in here
(( $argumentCount > 0 )) || vlog "Starting $script_name: invoke with -help for other options" (( 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 # verify this is an sbt dir or -create was given
[[ -r ./build.sbt || -d ./project || -n "$sbt_create" ]] || { [[ -r ./build.sbt || -d ./project || -n "$sbt_create" ]] || {
@ -479,40 +447,79 @@ EOM
exit 1 exit 1
} }
if [[ -n $noshare ]]; then if [[ -n "$noshare" ]]; then
addJava "$noshare_opts" for opt in ${noshare_opts}; do
addJava "$opt"
done
else else
[[ -n "$sbt_dir" ]] || { case "$sbt_version" in
sbt_dir=~/.sbt/$(sbt_version) "0.7."* | "0.10."* | "0.11."* | "0.12."* )
vlog "Using $sbt_dir as sbt dir, -sbt-dir to override." [[ -n "$sbt_dir" ]] || {
} sbt_dir="$HOME/.sbt/$sbt_version"
addJava "-Dsbt.global.base=$sbt_dir" vlog "Using $sbt_dir as sbt dir, -sbt-dir to override."
}
;;
esac
if [[ -n "$sbt_dir" ]]; then
addJava "-Dsbt.global.base=$sbt_dir"
fi
fi fi
if [[ -r "$jvm_opts_file" ]]; then if [[ -r "$jvm_opts_file" ]]; then
vlog "Using jvm options defined in file $jvm_opts_file" vlog "Using jvm options defined in file $jvm_opts_file"
readarr extra_jvm_opts < <(readConfigFile "$jvm_opts_file") while read opt; do extra_jvm_opts+=("$opt"); done < <(readConfigFile "$jvm_opts_file")
elif [[ -n "$JVM_OPTS" && !($JVM_OPTS =~ ^@.*) ]]; then elif [[ -n "$JVM_OPTS" && ! ("$JVM_OPTS" =~ ^@.*) ]]; then
vlog "Using jvm options defined in \$JVM_OPTS variable" vlog "Using jvm options defined in \$JVM_OPTS variable"
extra_jvm_opts=( $JVM_OPTS ) extra_jvm_opts=( $JVM_OPTS )
else else
vlog "Using default jvm options" vlog "Using default jvm options"
extra_jvm_opts=( $default_jvm_opts ) extra_jvm_opts=( $(default_jvm_opts) )
fi fi
# since sbt 0.7 doesn't understand iflast
[[ ${#residual_args[@]} -eq 0 ]] && [[ -z "$batch" ]] && residual_args=( "shell" )
# traceLevel is 0.12+ # traceLevel is 0.12+
[[ -n $trace_level ]] && setTraceLevel [[ -n "$trace_level" ]] && setTraceLevel
[[ -n $log_level ]] && [[ $log_level != Info ]] && logLevalArg="set logLevel in Global := Level.$log_level" main () {
execRunner "$java_cmd" \
"${extra_jvm_opts[@]}" \
"${java_args[@]}" \
-jar "$sbt_jar" \
"${sbt_commands[@]}" \
"${residual_args[@]}"
}
# sbt inserts this string on certain lines when formatting is enabled:
# val OverwriteLine = "\r\u001BM\u001B[2K"
# ...in order not to spam the console with a million "Resolving" lines.
# Unfortunately that makes it that much harder to work with when
# we're not going to print those lines anyway. We strip that bit of
# line noise, but leave the other codes to preserve color.
mainFiltered () {
local ansiOverwrite='\r\x1BM\x1B[2K'
local excludeRegex=$(egrep -v '^#|^$' ~/.sbtignore | paste -sd'|' -)
echoLine () {
local line="$1"
local line1="$(echo "$line" | sed -r 's/\r\x1BM\x1B\[2K//g')" # This strips the OverwriteLine code.
local line2="$(echo "$line1" | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')" # This strips all codes - we test regexes against this.
if [[ $line2 =~ $excludeRegex ]]; then
[[ -n $debugUs ]] && echo "[X] $line1"
else
[[ -n $debugUs ]] && echo " $line1" || echo "$line1"
fi
}
echoLine "Starting sbt with output filtering enabled."
main | while read -r line; do echoLine "$line"; done
}
# Only filter if there's a filter file and we don't see a known interactive command.
# Obviously this is super ad hoc but I don't know how to improve on it. Testing whether
# stdin is a terminal is useless because most of my use cases for this filtering are
# exactly when I'm at a terminal, running sbt non-interactively.
shouldFilter () { [[ -f ~/.sbtignore ]] && ! egrep -q '\b(shell|console|consoleProject)\b' <<<"${residual_args[@]}"; }
# run sbt # run sbt
execRunner "$java_cmd" \ if shouldFilter; then mainFiltered; else main; fi
"${extra_jvm_opts[@]}" \
"${java_args[@]}" \
-jar "$sbt_jar" \
"$logLevalArg" \
"${sbt_commands[@]}" \
"${residual_args[@]}"