aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/scalariform-example/README.md5
-rw-r--r--examples/scalariform-example/build/build.scala31
-rw-r--r--examples/scalariform-example/build/build/build.scala5
-rw-r--r--examples/scalariform-example/resources/reference.conf8
-rw-r--r--examples/scalariform-example/src/Main.scala11
-rw-r--r--plugins/scalariform/Scalariform.scala62
-rw-r--r--plugins/scalariform/build/build.scala9
-rw-r--r--stage2/BuildBuild.scala1
8 files changed, 132 insertions, 0 deletions
diff --git a/examples/scalariform-example/README.md b/examples/scalariform-example/README.md
new file mode 100644
index 0000000..e599b5b
--- /dev/null
+++ b/examples/scalariform-example/README.md
@@ -0,0 +1,5 @@
+This example shows integration with scalariform plugin.
+Reformat executed on every `cbt compile` call, and affects only *.scala source files.
+You can provide your custom scalariform preferences in build via `scalariformPreferences`.
+To test formatting in action you can execute: `cbt breakFormatting` to break formatting
+and `cbt scalariformReformat` to get formatting back.
diff --git a/examples/scalariform-example/build/build.scala b/examples/scalariform-example/build/build.scala
new file mode 100644
index 0000000..b9caa59
--- /dev/null
+++ b/examples/scalariform-example/build/build.scala
@@ -0,0 +1,31 @@
+import cbt._
+import scalariform.formatter.preferences._
+
+class Build(val context: Context) extends BuildBuild with Scalariform {
+ override def compile = {
+ scalariformFormat
+ super.compile
+ }
+
+ override def scalariformPreferences =
+ FormattingPreferences()
+ .setPreference(SpacesAroundMultiImports, true)
+ .setPreference(DoubleIndentClassDeclaration, true)
+ .setPreference(RewriteArrowSymbols, true)
+
+ def breakFormatting = {
+ import java.nio.file._
+ import scala.collection.JavaConverters._
+ sourceFiles foreach { file =>
+ try {
+ val path = file.toPath
+ val fileLines = Files.readAllLines(path).asScala
+ val brokenLines = fileLines map (_.dropWhile(_ ==' '))
+ Files.write(path, brokenLines.asJava)
+ } catch {
+ case e: Exception => System.err.print(s"Error happend when breaking formatting: ${e}")
+ }
+ }
+ System.err.println("Done breaking formatting")
+ }
+}
diff --git a/examples/scalariform-example/build/build/build.scala b/examples/scalariform-example/build/build/build.scala
new file mode 100644
index 0000000..59ab8d1
--- /dev/null
+++ b/examples/scalariform-example/build/build/build.scala
@@ -0,0 +1,5 @@
+import cbt._
+
+class Build(val context: Context) extends BuildBuild {
+ override def dependencies = super.dependencies :+ plugins.scalariform
+}
diff --git a/examples/scalariform-example/resources/reference.conf b/examples/scalariform-example/resources/reference.conf
new file mode 100644
index 0000000..f3e122d
--- /dev/null
+++ b/examples/scalariform-example/resources/reference.conf
@@ -0,0 +1,8 @@
+// should not reformat this, cause it is not in source files
+some {
+ inside {
+ foo: 22
+ bar: false
+ baz: "hello"
+ }
+}
diff --git a/examples/scalariform-example/src/Main.scala b/examples/scalariform-example/src/Main.scala
new file mode 100644
index 0000000..d299aad
--- /dev/null
+++ b/examples/scalariform-example/src/Main.scala
@@ -0,0 +1,11 @@
+import scala.concurrent.{ Await, Future }
+import scala.concurrent.duration._
+
+object Main extends App {
+ println("fooo")
+ val futureRes = Await.result(Future.successful(1), 5.seconds)
+ List(1, 2, 4, 5, 6) match {
+ case h :: _ ⇒ println("not empty list")
+ case Nil ⇒ println("empty list")
+ }
+}
diff --git a/plugins/scalariform/Scalariform.scala b/plugins/scalariform/Scalariform.scala
new file mode 100644
index 0000000..1f6e51f
--- /dev/null
+++ b/plugins/scalariform/Scalariform.scala
@@ -0,0 +1,62 @@
+package cbt
+
+import java.io.File
+import java.nio.file.FileSystems
+import java.nio.file.Files._
+
+import scala.util.Try
+import scalariform.formatter.ScalaFormatter
+import scalariform.formatter.preferences.FormattingPreferences
+import scalariform.parser.ScalaParserException
+
+trait Scalariform extends BaseBuild {
+ def scalariformFormat: ExitCode = {
+ Scalariform.format(sourceFiles, scalariformPreferences, scalaVersion)
+ ExitCode.Success
+ }
+
+ def scalariformPreferences: FormattingPreferences = Scalariform.defaultPreferences
+}
+
+object Scalariform {
+
+ val defaultPreferences: FormattingPreferences = {
+ import scalariform.formatter.preferences._
+ FormattingPreferences()
+ .setPreference(AlignParameters, true)
+ .setPreference(AlignArguments, true)
+ .setPreference(AlignSingleLineCaseStatements, true)
+ .setPreference(MultilineScaladocCommentsStartOnFirstLine, true)
+ .setPreference(SpaceInsideParentheses, true)
+ .setPreference(SpacesWithinPatternBinders, true)
+ .setPreference(SpacesAroundMultiImports, true)
+ .setPreference(DoubleIndentClassDeclaration, false)
+ }
+
+ private val scalaFileMatcher = FileSystems.getDefault.getPathMatcher("glob:**.scala")
+
+ def format(files: Seq[File], preferences: FormattingPreferences, scalaVersion: String) = {
+ var reformattedCount: Int = 0
+ for (file <- files if file.exists) {
+ val path = file.toPath
+ if(scalaFileMatcher.matches(path)) {
+ try {
+ val sourceCode = new String(readAllBytes(path))
+ val formatted = ScalaFormatter.format(
+ sourceCode,
+ preferences,
+ Some(scalaVersion)
+ )
+ if (sourceCode != formatted) {
+ write(path, formatted.getBytes)
+ reformattedCount += 1
+ }
+ } catch {
+ case e: ScalaParserException => System.err.println(s"Scalariform parser error: ${e.getMessage} when formatting source: $file")
+ }
+ }
+ }
+ System.err.println(s"Reformatted $reformattedCount Scala sources")
+ }
+
+}
diff --git a/plugins/scalariform/build/build.scala b/plugins/scalariform/build/build.scala
new file mode 100644
index 0000000..5910b41
--- /dev/null
+++ b/plugins/scalariform/build/build.scala
@@ -0,0 +1,9 @@
+import cbt._
+
+class Build(val context: Context) extends Plugin {
+ override def dependencies =
+ super.dependencies ++
+ Resolver( mavenCentral ).bind(
+ ScalaDependency("org.scalariform", "scalariform", "0.1.8")
+ )
+}
diff --git a/stage2/BuildBuild.scala b/stage2/BuildBuild.scala
index 4d9c869..83ea7ec 100644
--- a/stage2/BuildBuild.scala
+++ b/stage2/BuildBuild.scala
@@ -11,6 +11,7 @@ trait BuildBuild extends BaseBuild{
final val scalaTest = DirectoryDependency( managedContext.cbtHome ++ "/plugins/scalatest" )
final val sbtLayout = DirectoryDependency( managedContext.cbtHome ++ "/plugins/sbt_layout" )
final val scalaJs = DirectoryDependency( managedContext.cbtHome ++ "/plugins/scalajs" )
+ final val scalariform = DirectoryDependency( managedContext.cbtHome ++ "/plugins/scalariform" )
}
override def dependencies =