aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNikolay Tatarinov <5min4eq.unity@gmail.com>2016-06-24 02:48:45 +0300
committerJan Christopher Vogt <oss.nsp@cvogt.org>2016-06-23 19:48:45 -0400
commit2cbc42fd0809b60b1ee2116657d18b3f44f8aef1 (patch)
treee894a040137e591c4a7664b7353d709298a44f65 /examples
parent75c32537cd8f29f9d12db37bf06ad942806f0239 (diff)
downloadcbt-2cbc42fd0809b60b1ee2116657d18b3f44f8aef1.tar.gz
cbt-2cbc42fd0809b60b1ee2116657d18b3f44f8aef1.tar.bz2
cbt-2cbc42fd0809b60b1ee2116657d18b3f44f8aef1.zip
Scalafmt plugin implementation (#156)
* scalariform: improve logging, declare tasks final * scalafmt plugin implementation * add scalafmt and scalariform plugins and examples to tests * fix logging guarded logging behaviour * add notes about formatting check to README * fix compilation error in examples
Diffstat (limited to 'examples')
-rw-r--r--examples/scalafmt-example/README.md16
-rw-r--r--examples/scalafmt-example/build/build.scala29
-rw-r--r--examples/scalafmt-example/build/build/build.scala5
-rw-r--r--examples/scalafmt-example/resources/reference.conf8
-rw-r--r--examples/scalafmt-example/src/Main.scala16
-rw-r--r--examples/scalariform-example/README.md15
-rw-r--r--examples/scalariform-example/build/build.scala10
7 files changed, 93 insertions, 6 deletions
diff --git a/examples/scalafmt-example/README.md b/examples/scalafmt-example/README.md
new file mode 100644
index 0000000..0a59f96
--- /dev/null
+++ b/examples/scalafmt-example/README.md
@@ -0,0 +1,16 @@
+This example shows integration with scalafmt plugin.
+
+Reformat executed on every `cbt compile` call, and affects only *.scala source files.
+
+You can provide your custom scalfmt preferences in build via `scalafmtConfig`.
+
+To see formatting in action: execute `cbt breakFormatting` to break formatting and then execute`cbt scalafmt` to get formatting back.
+
+To check if your code is properly formatted(for example as part of CI validation), you can execute:
+
+```
+cbt scalafmt
+git diff --exit-code
+```
+
+Last command will return non-zero code, if your code isn't properly formatted.
diff --git a/examples/scalafmt-example/build/build.scala b/examples/scalafmt-example/build/build.scala
new file mode 100644
index 0000000..6f77108
--- /dev/null
+++ b/examples/scalafmt-example/build/build.scala
@@ -0,0 +1,29 @@
+import cbt._
+import org.scalafmt.ScalafmtStyle
+
+class Build(val context: Context) extends BuildBuild with Scalafmt {
+ override def compile = {
+ scalafmt
+ super.compile
+ }
+
+ override def scalafmtConfig: ScalafmtStyle = ScalafmtStyle.defaultWithAlign
+
+ def breakFormatting = {
+ import java.nio.file._
+ import java.nio.charset.Charset
+ import scala.collection.JavaConverters._
+ val utf8 = Charset.forName("UTF-8")
+ sourceFiles foreach { file =>
+ try {
+ val path = file.toPath
+ val fileLines = Files.readAllLines(path, utf8).asScala
+ val brokenLines = fileLines map (_.dropWhile(_ ==' '))
+ Files.write(path, brokenLines.asJava, utf8)
+ } catch {
+ case e: Exception => System.err.print(s"Error happend when breaking formatting: ${e}")
+ }
+ }
+ System.err.println("Done breaking formatting")
+ }
+}
diff --git a/examples/scalafmt-example/build/build/build.scala b/examples/scalafmt-example/build/build/build.scala
new file mode 100644
index 0000000..aa70f36
--- /dev/null
+++ b/examples/scalafmt-example/build/build/build.scala
@@ -0,0 +1,5 @@
+import cbt._
+
+class Build(val context: Context) extends BuildBuild {
+ override def dependencies = super.dependencies :+ plugins.scalafmt
+}
diff --git a/examples/scalafmt-example/resources/reference.conf b/examples/scalafmt-example/resources/reference.conf
new file mode 100644
index 0000000..f3e122d
--- /dev/null
+++ b/examples/scalafmt-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/scalafmt-example/src/Main.scala b/examples/scalafmt-example/src/Main.scala
new file mode 100644
index 0000000..595cede
--- /dev/null
+++ b/examples/scalafmt-example/src/Main.scala
@@ -0,0 +1,16 @@
+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")
+ }
+
+ List(1 -> 2, 2 -> 3, 3 -> 4) match {
+ case (1, 2) :: _ => 90 -> 1
+ case (22, 44) :: _ => 1 -> 150
+ }
+}
diff --git a/examples/scalariform-example/README.md b/examples/scalariform-example/README.md
index e599b5b..28ad226 100644
--- a/examples/scalariform-example/README.md
+++ b/examples/scalariform-example/README.md
@@ -1,5 +1,16 @@
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.
+
+To see formatting in action: execute `cbt breakFormatting` to break formatting and then execute `cbt scalariformFormat` to get formatting back.
+
+To check if your code is properly formatted(for example as part of CI validation), you can execute:
+
+```
+cbt scalariformFormat
+git diff --exit-code
+```
+
+Last command will return non-zero code, if your code isn't properly formatted.
diff --git a/examples/scalariform-example/build/build.scala b/examples/scalariform-example/build/build.scala
index b9caa59..5f7b7ff 100644
--- a/examples/scalariform-example/build/build.scala
+++ b/examples/scalariform-example/build/build.scala
@@ -1,7 +1,7 @@
import cbt._
import scalariform.formatter.preferences._
-class Build(val context: Context) extends BuildBuild with Scalariform {
+class Build(val context: Context) extends BaseBuild with Scalariform {
override def compile = {
scalariformFormat
super.compile
@@ -13,15 +13,17 @@ class Build(val context: Context) extends BuildBuild with Scalariform {
.setPreference(DoubleIndentClassDeclaration, true)
.setPreference(RewriteArrowSymbols, true)
- def breakFormatting = {
+ final def breakFormatting = {
import java.nio.file._
+ import java.nio.charset.Charset
import scala.collection.JavaConverters._
+ val utf8 = Charset.forName("UTF-8")
sourceFiles foreach { file =>
try {
val path = file.toPath
- val fileLines = Files.readAllLines(path).asScala
+ val fileLines = Files.readAllLines(path, utf8).asScala
val brokenLines = fileLines map (_.dropWhile(_ ==' '))
- Files.write(path, brokenLines.asJava)
+ Files.write(path, brokenLines.asJava, utf8)
} catch {
case e: Exception => System.err.print(s"Error happend when breaking formatting: ${e}")
}