From 07d3b85e98b4cc86c9f685c1096469cf6eca9cbb Mon Sep 17 00:00:00 2001 From: rockjam <5min4eq.unity@gmail.com> Date: Mon, 20 Jun 2016 13:38:38 +0300 Subject: scalariform plugin naive implementation --- plugins/scalariform/Scalariform.scala | 62 +++++++++++++++++++++++++++++++++++ plugins/scalariform/build/build.scala | 9 +++++ 2 files changed, 71 insertions(+) create mode 100644 plugins/scalariform/Scalariform.scala create mode 100644 plugins/scalariform/build/build.scala (limited to 'plugins/scalariform') 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") + ) +} -- cgit v1.2.3