diff options
author | rockjam <5min4eq.unity@gmail.com> | 2016-06-20 13:38:38 +0300 |
---|---|---|
committer | rockjam <5min4eq.unity@gmail.com> | 2016-06-20 14:35:48 +0300 |
commit | 07d3b85e98b4cc86c9f685c1096469cf6eca9cbb (patch) | |
tree | 17b022ef5e8154869b0b33ae881a25b87be7faf7 /plugins | |
parent | f834052e2825cb283399c14eb626f05d73b73759 (diff) | |
download | cbt-07d3b85e98b4cc86c9f685c1096469cf6eca9cbb.tar.gz cbt-07d3b85e98b4cc86c9f685c1096469cf6eca9cbb.tar.bz2 cbt-07d3b85e98b4cc86c9f685c1096469cf6eca9cbb.zip |
scalariform plugin naive implementation
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/scalariform/Scalariform.scala | 62 | ||||
-rw-r--r-- | plugins/scalariform/build/build.scala | 9 |
2 files changed, 71 insertions, 0 deletions
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") + ) +} |