From 2cbc42fd0809b60b1ee2116657d18b3f44f8aef1 Mon Sep 17 00:00:00 2001 From: Nikolay Tatarinov <5min4eq.unity@gmail.com> Date: Fri, 24 Jun 2016 02:48:45 +0300 Subject: 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 --- plugins/scalafmt/Scalafmt.scala | 69 ++++++++++++++++++++++++++++++++++++++ plugins/scalafmt/build/build.scala | 9 +++++ 2 files changed, 78 insertions(+) create mode 100644 plugins/scalafmt/Scalafmt.scala create mode 100644 plugins/scalafmt/build/build.scala (limited to 'plugins/scalafmt') diff --git a/plugins/scalafmt/Scalafmt.scala b/plugins/scalafmt/Scalafmt.scala new file mode 100644 index 0000000..94670cb --- /dev/null +++ b/plugins/scalafmt/Scalafmt.scala @@ -0,0 +1,69 @@ +package cbt + +import org.scalafmt.{FormatResult, ScalafmtStyle} + +import java.io.File +import java.nio.file.Files._ +import java.nio.file.{FileSystems, Path} + +/** + * This plugin provides scalafmt support for cbt. + * + */ +trait Scalafmt extends BaseBuild { + /** + * Reformat scala source code according to `scalafmtConfig` rules + * + * @return always returns `ExitCode.Success` + */ + final def scalafmt: ExitCode = { + Scalafmt.format(sourceFiles, scalafmtConfig) + ExitCode.Success + } + + /** + * Scalafmt formatting config + */ + def scalafmtConfig: ScalafmtStyle = Scalafmt.defaultConfig +} + +object Scalafmt { + + val defaultConfig = ScalafmtStyle.default + + def format(files: Seq[File], style: ScalafmtStyle): Unit = { + var reformattedCount: Int = 0 + scalaSourceFiles(files) foreach { path => + handleFormatted(path, style) { case (original, result) => + result match { + case FormatResult.Success(formatted) => + if (original != formatted) { + write(path, formatted.getBytes) + reformattedCount += 1 + } + case FormatResult.Failure(e) => + System.err.println(s"Failed to format file: $path, cause: ${e}") + case FormatResult.Incomplete(e) => + System.err.println(s"Couldn't complete file reformat: $path") + } + } + } + if (reformattedCount > 0) System.err.println(s"Formatted $reformattedCount Scala sources") + } + + private val scalaFileMatcher = FileSystems.getDefault.getPathMatcher("glob:**.scala") + + private def scalaSourceFiles(files: Seq[File]): Seq[Path] = { + files collect { + case f if f.exists + && scalaFileMatcher.matches(f.toPath) => f.toPath + } + } + + private def handleFormatted[T](path: Path, style: ScalafmtStyle)(handler: (String, FormatResult) => T): T = { + val original = new String(readAllBytes(path)) + val result = org.scalafmt.Scalafmt.format(original, style) + handler(original, result) + } + +} diff --git a/plugins/scalafmt/build/build.scala b/plugins/scalafmt/build/build.scala new file mode 100644 index 0000000..e8ce270 --- /dev/null +++ b/plugins/scalafmt/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("com.geirsson", "scalafmt", "0.2.5") + ) +} -- cgit v1.2.3