From aea470195de6cb8f0111e5908917d64889f3c753 Mon Sep 17 00:00:00 2001 From: rockjam Date: Sun, 2 Oct 2016 19:34:10 +0300 Subject: upgrade scalafmt 0.3.1 -> 0.4.2; load scalafmt config from config file --- plugins/scalafmt/Scalafmt.scala | 51 ++++++++++++++++++++++++++++++-------- plugins/scalafmt/build/build.scala | 5 +++- 2 files changed, 44 insertions(+), 12 deletions(-) (limited to 'plugins') diff --git a/plugins/scalafmt/Scalafmt.scala b/plugins/scalafmt/Scalafmt.scala index a1c7a0d..1f8bf2d 100644 --- a/plugins/scalafmt/Scalafmt.scala +++ b/plugins/scalafmt/Scalafmt.scala @@ -1,11 +1,12 @@ package cbt import org.scalafmt.Error.Incomplete -import org.scalafmt.{FormatResult, ScalafmtStyle} - +import org.scalafmt.Formatted +import org.scalafmt.cli.StyleCache +import org.scalafmt.config.ScalafmtConfig import java.io.File import java.nio.file.Files._ -import java.nio.file.{FileSystems, Path} +import java.nio.file.{ FileSystems, Path, Paths } /** * This plugin provides scalafmt support for cbt. @@ -23,28 +24,49 @@ trait Scalafmt extends BaseBuild { } /** - * Scalafmt formatting config + * Scalafmt formatting config. + * + * Tries to get style in following order: + * • project local .scalafmt.conf + * • global ~/.scalafmt.conf + * • default scalafmt config + * + * Override this task if you want to provide + * scalafmt config programmatically on your own. */ - def scalafmtConfig: ScalafmtStyle = Scalafmt.defaultConfig + def scalafmtConfig: ScalafmtConfig = + Scalafmt.getStyle( + project = projectDirectory.toPath, + home = Option(System.getProperty("user.home")) map (p => Paths.get(p)) + ) } object Scalafmt { - val defaultConfig = ScalafmtStyle.default + def getStyle(project: Path, home: Option[Path]): ScalafmtConfig = { + val local = getConfigPath(project) + val global = home flatMap getConfigPath + val customStyle = for { + configPath <- local.orElse(global) + style <- StyleCache.getStyleForFile(configPath.toString) + } yield style + + customStyle.getOrElse(ScalafmtConfig.default) + } - def format(files: Seq[File], style: ScalafmtStyle): Unit = { + def format(files: Seq[File], style: ScalafmtConfig): Unit = { var reformattedCount: Int = 0 scalaSourceFiles(files) foreach { path => handleFormatted(path, style) { case (original, result) => result match { - case FormatResult.Success(formatted) => + case Formatted.Success(formatted) => if (original != formatted) { write(path, formatted.getBytes) reformattedCount += 1 } - case FormatResult.Failure(e: Incomplete) => + case Formatted.Failure(e: Incomplete) => System.err.println(s"Couldn't complete file reformat: $path") - case FormatResult.Failure(e) => + case Formatted.Failure(e) => System.err.println(s"Failed to format file: $path, cause: ${e}") } } @@ -61,10 +83,17 @@ object Scalafmt { } } - private def handleFormatted[T](path: Path, style: ScalafmtStyle)(handler: (String, FormatResult) => T): T = { + private def handleFormatted[T](path: Path, style: ScalafmtConfig)(handler: (String, Formatted) => T): T = { val original = new String(readAllBytes(path)) val result = org.scalafmt.Scalafmt.format(original, style) handler(original, result) } + private def getConfigPath(base: Path): Option[Path] = { + val location = base.resolve(".scalafmt.conf").toFile + Option(location.exists && location.isFile) collect { + case true => location.toPath.toAbsolutePath + } + } + } diff --git a/plugins/scalafmt/build/build.scala b/plugins/scalafmt/build/build.scala index 0d4900c..2631908 100644 --- a/plugins/scalafmt/build/build.scala +++ b/plugins/scalafmt/build/build.scala @@ -1,9 +1,12 @@ import cbt._ class Build(val context: Context) extends Plugin { + private val ScalafmtVersion = "0.4.2" + override def dependencies = super.dependencies ++ Resolver( mavenCentral ).bind( - ScalaDependency("com.geirsson", "scalafmt", "0.3.1") + ScalaDependency("com.geirsson", "scalafmt", ScalafmtVersion), + ScalaDependency("com.geirsson", "scalafmt-cli", ScalafmtVersion) ) } -- cgit v1.2.3