aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2017-02-26 21:43:16 +0800
committerChristopher Vogt <oss.nsp@cvogt.org>2017-02-27 12:27:44 +0800
commitf5cb9a92574bb4deea75090e8ccf0f259f622f04 (patch)
tree6872b7dc7be2c7c3907c3a01a713a551b1c03014 /plugins
parent623fc52d37dc8c171d1fa2a0bb346265054c4b40 (diff)
downloadcbt-f5cb9a92574bb4deea75090e8ccf0f259f622f04.tar.gz
cbt-f5cb9a92574bb4deea75090e8ccf0f259f622f04.tar.bz2
cbt-f5cb9a92574bb4deea75090e8ccf0f259f622f04.zip
upgrade and simplify scalafmt plugin
Diffstat (limited to 'plugins')
-rw-r--r--plugins/scalafmt/Scalafmt.scala64
-rw-r--r--plugins/scalafmt/build/build.scala3
2 files changed, 24 insertions, 43 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 }
}
-
}
diff --git a/plugins/scalafmt/build/build.scala b/plugins/scalafmt/build/build.scala
index 2631908..b37d769 100644
--- a/plugins/scalafmt/build/build.scala
+++ b/plugins/scalafmt/build/build.scala
@@ -1,12 +1,11 @@
import cbt._
class Build(val context: Context) extends Plugin {
- private val ScalafmtVersion = "0.4.2"
+ private val ScalafmtVersion = "0.5.7"
override def dependencies =
super.dependencies ++
Resolver( mavenCentral ).bind(
- ScalaDependency("com.geirsson", "scalafmt", ScalafmtVersion),
ScalaDependency("com.geirsson", "scalafmt-cli", ScalafmtVersion)
)
}