Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 75 additions & 68 deletions examples/cross-rewrite-example/build/build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,10 @@ class Build(val context: Context) extends BaseBuild with Scalameta { defaultMain
}
}

def versions = Seq[(String, Seq[Patch])](
"2.12.1" -> Seq(),
"2.11.8" -> Seq(
RemoveGlobalImport(
importer"scala.concurrent.Future"
),
AddGlobalImport(
importer"scala.util.Try"
)
)
)
def libs = Seq[(String, MavenDependency, Seq[Patch])](
(
"scalaz",
MavenDependency( "org.scalaz", "scalaz-core", "7.2.10" ),
Seq(
AddGlobalImport(importer"scalaz._"),
Replace(Symbol("_root_.scala.package.Either."), q"\/"),
Replace(Symbol("_root_.scala.util.Right."), q"\/-"),
RemoveGlobalImport(importer"cats.implicits._")
)
),
(
"cats",
MavenDependency( "org.typelevel", "cats", "0.9.0" ),
Seq(
AddGlobalImport(importer"cats.implicits._")
)
)
)
import CrossRewrite._

def cross = versions.flatMap{ case ( v, version_rewrites ) =>
libs.map{
def cross = CrossRewrite.versions.flatMap{ case ( v, version_rewrites ) =>
CrossRewrite.libs.map{
case ( label, dep, lib_rewrites ) =>
val d = defaultMainBuild.target / "rewrites" / label ++ "-" ++ v
d.mkdirs
Expand All @@ -73,24 +44,13 @@ class Build(val context: Context) extends BaseBuild with Scalameta { defaultMain
)
override def projectDirectory = d
override def scaladoc = None
override def sources = {
val fromTo = lib.autoRelative( defaultMainBuild.sources ).collect{
case (location, relative) if location.isFile
=> location -> projectDirectory / "src" / relative
}

val to = fromTo.map(_._2)
assert( ( to diff to.distinct ).isEmpty )

Scalafix.apply(lib).config(
defaultMainBuild.classpath,
files = fromTo,
patches = lib_rewrites ++ version_rewrites,
allowEmpty = true
).apply

to
}
override def sources = CrossRewrite.patchesSources(
defaultMainBuild.sources,
projectDirectory / "src",
defaultMainBuild.classpath,
lib_rewrites ++ version_rewrites,
lib
)

override def test = {
new BasicBuild( context ) with ScalaTest { patchedTestBuild =>
Expand All @@ -103,27 +63,74 @@ class Build(val context: Context) extends BaseBuild with Scalameta { defaultMain
override def dependencies = patchedMainBuild +: super.dependencies
override def target = super.target / v ~ "-" ~ label

override def sources = {
val fromTo = lib.autoRelative( defaultMainBuild.test.sources ).collect{
case (location, relative) if location.isFile
=> location -> target / "src" / relative
}

val to = fromTo.map(_._2)
assert( ( to diff to.distinct ).isEmpty )

Scalafix.apply(lib).config(
defaultMainBuild.test.classpath,
files = fromTo,
patches = lib_rewrites ++ version_rewrites,
allowEmpty = true
).apply

to
}
override def sources = CrossRewrite.patchesSources(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this extraction 👍

defaultMainBuild.test.sources,
target / "src",
defaultMainBuild.test.classpath,
lib_rewrites ++ version_rewrites,
lib
)
}
}
}
}
}
}

object CrossRewrite{
def versions = Seq[(String, Seq[Patch])](
"2.12.1" -> Seq(),
"2.11.8" -> Seq(
RemoveGlobalImport(
importer"scala.concurrent.Future"
),
AddGlobalImport(
importer"scala.util.Try"
)
)
)
def libs = Seq[(String, MavenDependency, Seq[Patch])](
(
"scalaz",
MavenDependency( "org.scalaz", "scalaz-core", "7.2.10" ),
Seq(
AddGlobalImport(importer"scalaz._"),
Replace(Symbol("_root_.scala.package.Either."), q"\/"),
Replace(Symbol("_root_.scala.util.Right."), q"\/-"),
RemoveGlobalImport(importer"cats.implicits._")
)
),
(
"cats",
MavenDependency( "org.typelevel", "cats", "0.9.0" ),
Seq(
AddGlobalImport(importer"cats.implicits._")
)
)
)

def patchesSources(
sources: Seq[File],
destination: File,
semanticDbClassPath: ClassPath,
patches: Seq[Patch],
lib: Lib
) = {
val fromTo = lib.autoRelative( sources ).collect{
case (location, relative) if location.isFile
=> location -> destination / relative
}

val to = fromTo.map(_._2)
assert( ( to diff to.distinct ).isEmpty )

Scalafix.apply(lib).config(
semanticDbClassPath,
files = fromTo,
patches = patches,
allowEmpty = true
).apply

to
}
}