Skip to content

Instantly share code, notes, and snippets.

@timjstewart
Last active December 26, 2015 02:19
Show Gist options
  • Save timjstewart/7077983 to your computer and use it in GitHub Desktop.
Save timjstewart/7077983 to your computer and use it in GitHub Desktop.
Start sbt projects quickly
#! /bin/bash
# newsbt -- create a new sbt project
# Change this to generate projects using a different version of Scala
# You can override it with the -s option
scala_version=2.10.2
# I always use this namespace even though I don't own the domain name :)
# You can override it with the -n option
base_namespace=com.timjstewart
# Creates an sbt sub-project
make_project() {
project=$1
mkdir -p ${name}/${project}/src/{main,test}/scala/${sub_dirs}
# Generate Main.scala
cat <<-EOF >> ${name}/${project}/src/main/scala/${sub_dirs}/Main.scala
package ${base_namespace}
object Main extends App {
override def main(args: Array[String]): Unit = {
println("Hello, World!")
}
}
EOF
# Generate Test.scala
cat <<-EOF >> ${name}/${project}/src/test/scala/${sub_dirs}/Test.scala
package ${base_namespace}
import org.scalacheck._
import org.scalacheck.Prop.forAll
object Test extends Properties("Test") {
property("first") = forAll((n:Int) => n == n)
}
EOF
# Generate project build.sbt
cat <<-EOF >> ${name}/${project}/build.sbt
// build.sbt for ${project}
name := "${project}"
resolvers ++= Seq(
"Sonatype Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots",
"Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases"
)
libraryDependencies ++= Seq(
"org.scalacheck" %% "scalacheck" % "1.10.1" % "test",
"org.scalatest" % "scalatest_2.10" % "2.0.RC2" % "test",
"com.netflix.rxjava" % "rxjava-scala" % "0.14.8",
"log4j" % "log4j" % "1.2.17"
)
EOF
}
while getopts ":s:n:" opt
do
case $opt in
s)
scala_version=${OPTARG}
;;
n)
base_namespace=${OPTARG}
;;
*)
echo "unknown command" 2>&1
exit 2
;;
esac
done
shift $(($OPTIND-1))
# Convert namespace into subdirectories
sub_dirs=$(echo ${base_namespace} | tr '.' '/' | tr ' ' '_' | tr '[:upper:]' '[:lower:]')
if [ $# -lt 2 ]
then
echo "usage: newsbt OPTIONS NAME PROJECT ..." 1>&2
echo " -s SCALA_VERSION" 1>&2
echo " -n NAMESPACE" 1>&2
exit 1
fi
name=$1
shift
if [ -d ./${name} ]
then
echo "A directory named ${name} already exists in this diretory" 1>&2
exit 1
fi
mkdir ${name}
cat << SBT >> ${name}/build.sbt
// build.sbt for ${name}
scalaVersion := "${scala_version}"
sbtVersion := "0.13.0"
SBT
for project in $*
do
make_project ${project}
# Add a reference to each project to the main build.sbt
echo >> ${name}/build.sbt
echo "lazy val ${project} = project" >> ${name}/build.sbt
done
# add a newline to the end of the main build.sbt file
echo >> ${name}/build.sbt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment