-
-
Save broilogabriel/6d24932b8e3f77fed91698f8b7b1286b to your computer and use it in GitHub Desktop.
Bootstrap a Scala project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
re_prj='^[^\\/?%*:|"<>\.]+$' | |
re_pkg='^[^\\/?%*:|"<>]+$' | |
read -p "Project name? " PROJECT_NAME | |
if ! [[ $PROJECT_NAME =~ $re_prj ]] ; then | |
echo "Invalid project name $PROJECT_NAME. Try again without special chars." | |
exit 0 | |
fi | |
read -p "Default package name? " PACKAGE_NAME | |
if ! [[ $PACKAGE_NAME =~ $re_pkg ]] ; then | |
echo "Invalid package name $PACKAGE_NAME. Try again without special chars." | |
exit 0 | |
fi | |
PACKAGE_PATH=$(sed -r 's/\.+/\//g' <<< $PACKAGE_NAME) | |
INIT_SCALA="https://gist.github.com/broilogabriel/6d24932b8e3f77fed91698f8b7b1286b" | |
SCALA_VERSION="2.11.8" | |
SCALATEST_VERSION="3.0.0" | |
LOGBACK_VERSION="1.1.7" | |
SCALA_LOGGING_VERSION="3.5.0" | |
MOCKITO_VERSION="1.10.19" | |
DATE=`date +%d-%m-%Y` | |
mkdir $PROJECT_NAME | |
cd $PROJECT_NAME | |
cat > build.sbt << EOF | |
name := "$PROJECT_NAME" | |
version := "1.0.0" | |
scalaVersion := "$SCALA_VERSION" | |
libraryDependencies += "org.scalatest" %% "scalatest" % "$SCALATEST_VERSION" % "test" | |
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "$SCALA_LOGGING_VERSION" | |
libraryDependencies += "ch.qos.logback" % "logback-classic" % "$LOGBACK_VERSION" | |
libraryDependencies += "org.mockito" % "mockito-core" % "$MOCKITO_VERSION" % "test" | |
EOF | |
SRC="src/main/scala/$PACKAGE_PATH" | |
mkdir -p $SRC | |
cat > "$SRC/Main.scala" << EOF | |
package $PACKAGE_NAME | |
import com.typesafe.scalalogging.LazyLogging | |
/** | |
* Created by init-scala.sh ($INIT_SCALA) on $DATE. | |
*/ | |
object Main extends App with LazyLogging { | |
val text = "Project created with init-scala ($INIT_SCALA)" | |
val signal = minuses(text) | |
logger.info(signal) | |
logger.info(text) | |
logger.info(signal) | |
def minuses(text: String): String = "-" * (if (text == null || text.isEmpty) 1 else text.length) | |
} | |
EOF | |
mkdir -p "src/main/resources" | |
TESTS="src/test/scala/$PACKAGE_PATH" | |
mkdir -p $TESTS | |
cat > "$TESTS/MainTest.scala" << EOF | |
package $PACKAGE_NAME | |
import org.scalatest.FlatSpec | |
/** | |
* Created by init-scala.sh ($INIT_SCALA) on $DATE. | |
*/ | |
class MainTest extends FlatSpec { | |
"An empty String" should "return '-'" in { | |
assert(Main.minuses("") == "-") | |
} | |
"A null input" should "return '-'" in { | |
assert(Main.minuses(null) == "-") | |
} | |
"A String input" should "return a String with the same size" in { | |
val input = "somebiginput" | |
assert(Main.minuses(input).length == input.length) | |
} | |
} | |
EOF | |
mkdir -p "src/test/resources" | |
# .gitignore file | |
touch .gitignore | |
JETBRAINS_IGNORE="https://raw.githubusercontent.com/github/gitignore/master/Global/JetBrains.gitignore" | |
JAVA_IGNORE="https://raw.githubusercontent.com/github/gitignore/master/Java.gitignore" | |
SCALA_IGNORE="https://raw.githubusercontent.com/github/gitignore/master/Scala.gitignore" | |
echo "# Java $JAVA_IGNORE" >> .gitignore | |
curl -0 $JAVA_IGNORE >> .gitignore | |
echo "# Scala $SCALA_IGNORE" >> .gitignore | |
curl -0 $SCALA_IGNORE >> .gitignore | |
echo "# JetBrains $JETBRAINS_IGNORE" >> .gitignore | |
curl -0 $JETBRAINS_IGNORE >> .gitignore | |
# Create README.md | |
cat > README.md << EOF | |
#$PROJECT_NAME | |
Project template generated with [init-scala.sh]($INIT_SCALA) | |
EOF | |
# Initialize git | |
git init | |
git add .gitignore | |
git add build.sbt | |
git add README.md | |
git commit -m 'Project created with init-scala.sh - $INIT_SCALA' | |
# You need to have the idea plugin in the global SBT's plugins/build.sbt | |
# sbt update gen-idea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment