diff options
author | Nikolay Tatarinov <5min4eq.unity@gmail.com> | 2016-06-24 02:48:45 +0300 |
---|---|---|
committer | Jan Christopher Vogt <oss.nsp@cvogt.org> | 2016-06-23 19:48:45 -0400 |
commit | 2cbc42fd0809b60b1ee2116657d18b3f44f8aef1 (patch) | |
tree | e894a040137e591c4a7664b7353d709298a44f65 /plugins/scalafmt | |
parent | 75c32537cd8f29f9d12db37bf06ad942806f0239 (diff) | |
download | cbt-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 'plugins/scalafmt')
-rw-r--r-- | plugins/scalafmt/Scalafmt.scala | 69 | ||||
-rw-r--r-- | plugins/scalafmt/build/build.scala | 9 |
2 files changed, 78 insertions, 0 deletions
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") + ) +} |