From 2c792df95d36d61c55b07ddfa13387cf79371bdb Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Mon, 27 Mar 2017 19:33:59 -0400 Subject: add scala meta and scalafix plugin --- plugins/scalafix/Scalafix.scala | 66 ++++++++++++++++++++++++++++++++++++++ plugins/scalafix/build/build.scala | 27 ++++++++++++++++ stage2/plugins.scala | 1 + stage2/plugins/Scalameta.scala | 26 +++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 plugins/scalafix/Scalafix.scala create mode 100644 plugins/scalafix/build/build.scala create mode 100644 stage2/plugins/Scalameta.scala diff --git a/plugins/scalafix/Scalafix.scala b/plugins/scalafix/Scalafix.scala new file mode 100644 index 0000000..e95840c --- /dev/null +++ b/plugins/scalafix/Scalafix.scala @@ -0,0 +1,66 @@ +package cbt + +import java.io.File + +import scala.meta._ +import scala.meta.semantic.v1._ +import scala.meta.{ Symbol => _, _ } +import scalafix._, rewrite._, config._, util._ +import org.scalameta.{logger => scalametaLogger} + +import cbt._ + +trait Scalafix extends Scalameta{ + def scalafix = Scalafix.apply( lib ).config( + classpath, + sourceFiles zip sourceFiles + ) +} + +object Scalafix{ + case class apply( lib: Lib ){ + case class config( + classpath: ClassPath, + files: Seq[(File,File)], + patches: Seq[Patch] = Seq(), + rewrites: Seq[ Rewrite[ScalafixMirror] ] = Seq(), + allowEmpty: Boolean = false + ){ + def mirror = + Mirror( + classpath.string, + files.map(_._1).mkString(File.pathSeparator) + ) + + def context(file: File): ( RewriteCtx[Mirror], RewriteCtx[ScalafixMirror] ) = ( + scalafix.rewrite.RewriteCtx( + mirror.dialect(file).parse[Source].get, ScalafixConfig(), mirror + ), + scalafix.rewrite.RewriteCtx( + mirror.dialect(file).parse[Source].get, ScalafixConfig(), ScalafixMirror.fromMirror( mirror ) + ) + ) + + def apply: Unit = { + require( + allowEmpty || rewrites.nonEmpty || patches.nonEmpty, + "You need to provide some rewrites via: `override def scalafix = super.scalafix.copy( rewrites = Seq(...) )`" + ) + files.foreach{ case (from, to) => + implicit val ( ctx, ctx2 ) = context(from) + lib.writeIfChanged( + to, + Patch( + ( + patches + ++ rewrites.flatMap( + _.rewrite( ctx2 ).to + ) + ).to + ) + ) + } + } + } + } +} diff --git a/plugins/scalafix/build/build.scala b/plugins/scalafix/build/build.scala new file mode 100644 index 0000000..02dcc0c --- /dev/null +++ b/plugins/scalafix/build/build.scala @@ -0,0 +1,27 @@ +package scalafix_build + +import cbt._ + +class Build(val context: Context) extends Plugin { + override def dependencies = super.dependencies ++ Seq( + //plugins.scalameta + ) :+ Resolver( mavenCentral ).bindOne( + ScalaDependency( + "ch.epfl.scala", "scalafix-core", "0.3.2" + ) + ).copy( + // required until https://github.com/scalacenter/scalafix/issues/100 is fixed + replace = _ => _.flatMap{ + case m@MavenDependency("org.scalameta", artifactId,_,_,_) + if (artifactId startsWith "scalahost_") + || (artifactId startsWith "contrib_") + => Seq( m ) + case MavenDependency("org.scalameta", _,_,_,_) => Seq( + MavenDependency( + "org.scalameta", "scalahost_" ++ scalaVersion, "1.6.0" + ) + ) + case other => Seq( other ) + } + ) +} diff --git a/stage2/plugins.scala b/stage2/plugins.scala index b1638da..6c5ee29 100644 --- a/stage2/plugins.scala +++ b/stage2/plugins.scala @@ -5,6 +5,7 @@ class plugins( implicit context: Context ) { final lazy val googleJavaFormat = plugin( "google-java-format" ) final lazy val proguard = plugin( "proguard" ) final lazy val sbtLayout = plugin( "sbt_layout" ) + final lazy val scalafix = plugin( "scalafix" ) final lazy val scalafixCompilerPlugin = plugin( "scalafix-compiler-plugin" ) final lazy val scalafmt = plugin( "scalafmt" ) final lazy val scalaJs = plugin( "scalajs" ) diff --git a/stage2/plugins/Scalameta.scala b/stage2/plugins/Scalameta.scala new file mode 100644 index 0000000..663399d --- /dev/null +++ b/stage2/plugins/Scalameta.scala @@ -0,0 +1,26 @@ +package cbt +import java.io.File +trait Scalameta extends BaseBuild{ + override def scalacOptions = super.scalacOptions ++ Scalameta.scalacOptions( + Scalameta.scalaHost( scalaVersion, context.cbtLastModified, context.paths.mavenCache ).jar + ) +} +object Scalameta{ + def scalaHost( + scalaVersion: String, cbtLastModified: Long, mavenCache: java.io.File + )( + implicit logger: Logger, transientCache: java.util.Map[AnyRef,AnyRef], classLoaderCache: ClassLoaderCache + ) = + MavenResolver( cbtLastModified, mavenCache, mavenCentral ).bindOne( + MavenDependency( + "org.scalameta", "scalahost_"+scalaVersion, "1.6.0" + ) + ) + + def scalacOptions( scalaHost: File ) = + Seq( + "-Xplugin:" ++ scalaHost.string, + "-Yrangepos", + "-Xplugin-require:scalahost" + ) +} -- cgit v1.2.3