diff options
author | Jan Christopher Vogt <oss.nsp@cvogt.org> | 2017-02-28 09:22:29 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 09:22:29 -0500 |
commit | 9f3c5f3a36ea70a8dec03b048cc41b6579eb6c4b (patch) | |
tree | 13ce0083a15f7289682dc2fd6bf5066d25ee52cd /plugins/scalafmt/Scalafmt.scala | |
parent | fdb5d7cdb692ded3ccc1d865282169598d190c03 (diff) | |
parent | 03b066827b3d397fb30a689904fa8d55db43fc8e (diff) | |
download | cbt-9f3c5f3a36ea70a8dec03b048cc41b6579eb6c4b.tar.gz cbt-9f3c5f3a36ea70a8dec03b048cc41b6579eb6c4b.tar.bz2 cbt-9f3c5f3a36ea70a8dec03b048cc41b6579eb6c4b.zip |
Merge pull request #358 from cvogt/chris
various changes
Diffstat (limited to 'plugins/scalafmt/Scalafmt.scala')
-rw-r--r-- | plugins/scalafmt/Scalafmt.scala | 64 |
1 files changed, 23 insertions, 41 deletions
diff --git a/plugins/scalafmt/Scalafmt.scala b/plugins/scalafmt/Scalafmt.scala index 1f8bf2d..e3c3c3e 100644 --- a/plugins/scalafmt/Scalafmt.scala +++ b/plugins/scalafmt/Scalafmt.scala @@ -6,7 +6,7 @@ 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, Paths } +import java.nio.file._ /** * This plugin provides scalafmt support for cbt. @@ -18,10 +18,7 @@ trait Scalafmt extends BaseBuild { * * @return always returns `ExitCode.Success` */ - final def scalafmt: ExitCode = { - Scalafmt.format(sourceFiles, scalafmtConfig) - ExitCode.Success - } + final def scalafmt: ExitCode = Scalafmt.format(sourceFiles, scalafmtConfig) /** * Scalafmt formatting config. @@ -54,46 +51,31 @@ object Scalafmt { customStyle.getOrElse(ScalafmtConfig.default) } - 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 Formatted.Success(formatted) => - if (original != formatted) { - write(path, formatted.getBytes) - reformattedCount += 1 - } - case Formatted.Failure(e: Incomplete) => - System.err.println(s"Couldn't complete file reformat: $path") - case Formatted.Failure(e) => - System.err.println(s"Failed to format file: $path, cause: ${e}") - } + def format(files: Seq[File], style: ScalafmtConfig): ExitCode = { + val results = files.filter(_.string endsWith ".scala").map(_.toPath).map{ path => + val original = new String(readAllBytes(path)) + org.scalafmt.Scalafmt.format(original, style) match { + case Formatted.Success(formatted) => + if (original != formatted) { + val tmpPath = Paths.get(path.toString ++ ".scalafmt-tmp") + write(tmpPath, formatted.getBytes) + move(tmpPath, path, StandardCopyOption.REPLACE_EXISTING) + Some(1) + } + Some(0) + case Formatted.Failure(e) => + System.err.println(s"Scalafmt failed for $path\nCause: $e\n") + None } } - 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: ScalafmtConfig)(handler: (String, Formatted) => T): T = { - val original = new String(readAllBytes(path)) - val result = org.scalafmt.Scalafmt.format(original, style) - handler(original, result) + if(results.forall(_.nonEmpty)){ + System.err.println(s"Formatted ${results.flatten.sum} Scala sources") + ExitCode.Success + } else ExitCode.Failure } 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 - } + Some( base.resolve(".scalafmt.conf").toFile ) + .collect{ case f if f.exists && f.isFile => f.toPath.toAbsolutePath } } - } |