diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/scalafix-compiler-plugin/Scalafix.scala | 28 | ||||
-rw-r--r-- | plugins/scalafix-compiler-plugin/build/build.scala | 12 | ||||
-rw-r--r-- | plugins/scalafix/Scalafix.scala | 76 | ||||
-rw-r--r-- | plugins/scalafix/build/build.scala | 23 |
4 files changed, 116 insertions, 23 deletions
diff --git a/plugins/scalafix-compiler-plugin/Scalafix.scala b/plugins/scalafix-compiler-plugin/Scalafix.scala new file mode 100644 index 0000000..2a18c5d --- /dev/null +++ b/plugins/scalafix-compiler-plugin/Scalafix.scala @@ -0,0 +1,28 @@ +package cbt + +import cbt._ +import java.nio.file.Files._ +import java.nio.file._ +import java.io.File + +trait ScalafixCompilerPlugin extends BaseBuild { + def scalafixVersion = "0.3.1" + + override def scalacOptions = super.scalacOptions ++ + Scalafix.scalacOptions(projectDirectory.toPath, + Resolver( mavenCentral, sonatypeReleases ).bindOne( + ScalaDependency( "ch.epfl.scala", "scalafix-nsc", scalafixVersion ) + ).jar) +} + +object Scalafix { + def scalacOptions( rootPath: Path, nscJar: File ) = + Seq( + "-Xplugin:" ++ nscJar.string, + "-Yrangepos" + ) ++ configOption(rootPath) + + def configOption( rootPath: Path ) = + Some( rootPath.resolve(".scalafix.conf").toAbsolutePath ) + .filter(isRegularFile(_)).map("-P:scalafix:" ++ _.toString).toSeq +} diff --git a/plugins/scalafix-compiler-plugin/build/build.scala b/plugins/scalafix-compiler-plugin/build/build.scala new file mode 100644 index 0000000..b25b54d --- /dev/null +++ b/plugins/scalafix-compiler-plugin/build/build.scala @@ -0,0 +1,12 @@ +package scalafix_build + +import cbt._ + +class Build(val context: Context) extends Plugin { + override def dependencies = ( + super.dependencies ++ + Resolver( mavenCentral, sonatypeReleases ).bind( + ScalaDependency( "ch.epfl.scala", "scalafix-nsc", "0.3.1" ) + ) + ) +} diff --git a/plugins/scalafix/Scalafix.scala b/plugins/scalafix/Scalafix.scala index cf808d7..e95840c 100644 --- a/plugins/scalafix/Scalafix.scala +++ b/plugins/scalafix/Scalafix.scala @@ -1,28 +1,66 @@ package cbt -import cbt._ -import java.nio.file.Files._ -import java.nio.file._ import java.io.File -trait Scalafix extends BaseBuild { - def scalafixVersion = "0.3.1" +import scala.meta._ +import scala.meta.semantic.v1._ +import scala.meta.{ Symbol => _, _ } +import scalafix._, rewrite._, config._, util._ +import org.scalameta.{logger => scalametaLogger} + +import cbt._ - override def scalacOptions = super.scalacOptions ++ - Scalafix.scalacOptions(projectDirectory.toPath, - Resolver( mavenCentral, sonatypeReleases ).bindOne( - ScalaDependency( "ch.epfl.scala", "scalafix-nsc", scalafixVersion ) - ).jar) +trait Scalafix extends Scalameta{ + def scalafix = Scalafix.apply( lib ).config( + classpath, + sourceFiles zip sourceFiles + ) } -object Scalafix { - def scalacOptions( rootPath: Path, nscJar: File ) = - Seq( - "-Xplugin:" ++ nscJar.string, - "-Yrangepos" - ) ++ configOption(rootPath) +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 configOption( rootPath: Path ) = - Some( rootPath.resolve(".scalafix.conf").toAbsolutePath ) - .filter(isRegularFile(_)).map("-P:scalafix:" ++ _.toString).toSeq + 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 index b25b54d..02dcc0c 100644 --- a/plugins/scalafix/build/build.scala +++ b/plugins/scalafix/build/build.scala @@ -3,10 +3,25 @@ package scalafix_build import cbt._ class Build(val context: Context) extends Plugin { - override def dependencies = ( - super.dependencies ++ - Resolver( mavenCentral, sonatypeReleases ).bind( - ScalaDependency( "ch.epfl.scala", "scalafix-nsc", "0.3.1" ) + 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 ) + } ) } |