diff options
Diffstat (limited to 'plugins/scalafix')
-rw-r--r-- | plugins/scalafix/Scalafix.scala | 66 | ||||
-rw-r--r-- | plugins/scalafix/build/build.scala | 27 |
2 files changed, 93 insertions, 0 deletions
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 ) + } + ) +} |