diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/google-java-format/GoogleJavaFormat.scala | 34 | ||||
-rw-r--r-- | plugins/google-java-format/Immutable.java | 5 | ||||
-rw-r--r-- | plugins/google-java-format/build/build.scala | 9 | ||||
-rw-r--r-- | plugins/scalafmt/Scalafmt.scala | 21 |
4 files changed, 56 insertions, 13 deletions
diff --git a/plugins/google-java-format/GoogleJavaFormat.scala b/plugins/google-java-format/GoogleJavaFormat.scala new file mode 100644 index 0000000..cbccb94 --- /dev/null +++ b/plugins/google-java-format/GoogleJavaFormat.scala @@ -0,0 +1,34 @@ +package cbt + +import java.io.File +import java.nio.file.Files._ +import java.nio.file._ + +import com.google.googlejavaformat.java._ + +trait GoogleJavaFormat extends BaseBuild { + def googleJavaFormat() = GoogleJavaFormat.apply( lib, sourceFiles.filter(_.string endsWith ".java") ).format +} + +object GoogleJavaFormat{ + case class apply( lib: Lib, files: Seq[File] ){ + /** @param whiteSpaceInParenthesis more of a hack to make up for missing support in Scalafmt. Does not respect alignment and maxColumn. */ + def format = { + val (successes, errors) = lib.transformFilesOrError( files, in => + try{ + Right( new Formatter().formatSource(in) ) + } catch { + case e: FormatterException => Left( e ) + } + ) + if(errors.nonEmpty) + throw new RuntimeException( + "Google Java Format failed to parse some files:\n" ++ errors.map{ + case (file, error) => file.string ++ ":" ++ error.toString + }.mkString("\n"), + errors.head._2 + ) + successes + } + } +} diff --git a/plugins/google-java-format/Immutable.java b/plugins/google-java-format/Immutable.java new file mode 100644 index 0000000..5b3ff44 --- /dev/null +++ b/plugins/google-java-format/Immutable.java @@ -0,0 +1,5 @@ +package com.google.errorprone.annotations; +// to suppress warning +// "Class com.google.errorprone.annotations.Immutable not found - continuing with a stub." +// there probably is a better solution +public class Immutable{} diff --git a/plugins/google-java-format/build/build.scala b/plugins/google-java-format/build/build.scala new file mode 100644 index 0000000..50bc423 --- /dev/null +++ b/plugins/google-java-format/build/build.scala @@ -0,0 +1,9 @@ +import cbt._ + +class Build(val context: Context) extends Plugin { + override def dependencies = + super.dependencies ++ + Resolver( mavenCentral ).bind( + MavenDependency( "com.google.googlejavaformat", "google-java-format", "1.3" ) + ) +} diff --git a/plugins/scalafmt/Scalafmt.scala b/plugins/scalafmt/Scalafmt.scala index 9d42cbd..5535964 100644 --- a/plugins/scalafmt/Scalafmt.scala +++ b/plugins/scalafmt/Scalafmt.scala @@ -12,16 +12,15 @@ import java.nio.file._ trait Scalafmt extends BaseBuild { /** Reformat scala source code according to `scalafmtConfig` rules */ def scalafmt = { - val scalafmtLib = new ScalafmtLib(lib) - scalafmtLib.format( sourceFiles ).config( - scalafmtLib.loadConfig( + Scalafmt.apply( lib, sourceFiles.filter(_.string endsWith ".scala") ).config( + Scalafmt.loadConfig( projectDirectory.toPath ) getOrElse ScalafmtConfig.default ) } } -class ScalafmtLib(lib: Lib){ scalafmtLib => +object Scalafmt{ def userHome = Option( System.getProperty("user.home") ).map(Paths.get(_)) /** Tries to load config from .scalafmt.conf in given directory or fallback directory */ @@ -34,22 +33,18 @@ class ScalafmtLib(lib: Lib){ scalafmtLib => .flatMap ( file => StyleCache.getStyleForFile(file.toString) ) } - case class format(files: Seq[File]){ - /** - * @param whiteSpaceInParenthesis more of a hack to make up for missing support in Scalafmt. Does not respect alignment and maxColumn. - */ + case class apply( lib: Lib, files: Seq[File] ){ + /** @param whiteSpaceInParenthesis more of a hack to make up for missing support in Scalafmt. Does not respect alignment and maxColumn. */ case class config( - config: ScalafmtConfig, - whiteSpaceInParenthesis: Boolean = false + config: ScalafmtConfig, whiteSpaceInParenthesis: Boolean = false ) extends (() => Seq[File]){ - def lib = scalafmtLib def apply = { - val (successes, errors) = scalafmtLib.lib.transformFilesOrError( + val (successes, errors) = lib.transformFilesOrError( files, org.scalafmt.Scalafmt.format(_, config) match { case Formatted.Success(formatted) => Right( if( whiteSpaceInParenthesis ){ - scalafmtLib.whiteSpaceInParenthesis(formatted) + Scalafmt.whiteSpaceInParenthesis(formatted) } else formatted ) case Formatted.Failure( e ) => Left( e ) |