Merge pull request #23 from sbt/wip/percent

Add % syntax for configuration scoping
This commit is contained in:
eugene yokota 2020-04-13 00:25:23 -04:00 committed by GitHub
commit 3e38d9c916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 11 deletions

View File

@ -13,7 +13,7 @@ setup
In `project/plugins.sbt`:
```scala
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.3.0")
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.4.0")
// add also the following for Scala.js support
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.27")

View File

@ -1,5 +1,5 @@
ThisBuild / organization := "com.eed3si9n"
ThisBuild / version := "0.3.1-SNAPSHOT"
ThisBuild / version := "0.4.1-SNAPSHOT"
ThisBuild / description := "sbt plugin to define project matrix for cross building"
ThisBuild / licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-projectmatrix/blob/master/LICENSE"))

View File

@ -56,6 +56,12 @@ sealed trait ProjectMatrix extends CompositeProject {
/** Disable the given plugins on this project. */
def disablePlugins(ps: AutoPlugin*): ProjectMatrix
/**
* Applies the given functions to this Project.
* The second function is applied to the result of applying the first to this Project and so on.
* The intended use is a convenience for applying default configuration provided by a plugin.
*/
def configure(transforms: (Project => Project)*): ProjectMatrix
/**
* If autoScalaLibrary is false, add non-Scala row.
@ -160,6 +166,14 @@ object ProjectMatrix {
override def toString: String = s"ProjectRow($autoScalaLibrary, $axisValues)"
}
final class ProjectMatrixReferenceSyntax(m: ProjectMatrixReference) {
def %(conf: String): ProjectMatrix.MatrixClasspathDependency =
ProjectMatrix.MatrixClasspathDependency(m, Some(conf))
def %(conf: Configuration): ProjectMatrix.MatrixClasspathDependency =
ProjectMatrix.MatrixClasspathDependency(m, Some(conf.name))
}
final case class MatrixClasspathDependency(
matrix: ProjectMatrixReference,
configuration: Option[String]
@ -174,7 +188,8 @@ object ProjectMatrix {
val dependencies: Seq[MatrixClasspathDep[ProjectMatrixReference]],
val settings: Seq[Def.Setting[_]],
val configurations: Seq[Configuration],
val plugins: Plugins
val plugins: Plugins,
val transforms: Seq[Project => Project],
) extends ProjectMatrix { self =>
lazy val resolvedMappings: ListMap[ProjectRow, Project] = resolveMappings
private def resolveProjectIds: Map[ProjectRow, String] = {
@ -232,6 +247,7 @@ object ProjectMatrix {
inConfig(Test)(makeSources(nonScalaDirSuffix, svDirSuffix))
)
.settings(self.settings)
.configure(transforms: _*)
r -> r.process(p)
}): _*)
@ -292,6 +308,9 @@ object ProjectMatrix {
override def disablePlugins(ps: AutoPlugin*): ProjectMatrix =
setPlugins(Plugins.and(plugins, Plugins.And(ps.map(p => Plugins.Exclude(p)).toList)))
override def configure(ts: (Project => Project)*): ProjectMatrix =
copy(transforms = transforms ++ ts)
def setPlugins(ns: Plugins): ProjectMatrix = copy(plugins = ns)
override def jvmPlatform(scalaVersions: Seq[String]): ProjectMatrix =
@ -426,7 +445,8 @@ object ProjectMatrix {
dependencies: Seq[MatrixClasspathDep[ProjectMatrixReference]] = dependencies,
settings: Seq[Setting[_]] = settings,
configurations: Seq[Configuration] = configurations,
plugins: Plugins = plugins
plugins: Plugins = plugins,
transforms: Seq[Project => Project] = transforms,
): ProjectMatrix = {
val matrix = unresolved(
id,
@ -437,7 +457,8 @@ object ProjectMatrix {
dependencies,
settings,
configurations,
plugins
plugins,
transforms
)
allMatrices(id) = matrix
matrix
@ -446,7 +467,7 @@ object ProjectMatrix {
// called by macro
def apply(id: String, base: sbt.File): ProjectMatrix = {
val matrix = unresolved(id, base, Nil, Nil, Nil, Nil, Nil, Nil, Plugins.Empty)
val matrix = unresolved(id, base, Nil, Nil, Nil, Nil, Nil, Nil, Plugins.Empty, Nil)
allMatrices(id) = matrix
matrix
}
@ -460,7 +481,8 @@ object ProjectMatrix {
dependencies: Seq[MatrixClasspathDep[ProjectMatrixReference]],
settings: Seq[Def.Setting[_]],
configurations: Seq[Configuration],
plugins: Plugins
plugins: Plugins,
transforms: Seq[Project => Project]
): ProjectMatrix =
new ProjectMatrixDef(
id,
@ -471,7 +493,8 @@ object ProjectMatrix {
dependencies,
settings,
configurations,
plugins
plugins,
transforms
)
def lookupMatrix(local: LocalProjectMatrix): ProjectMatrix = {

View File

@ -11,9 +11,14 @@ object ProjectMatrixPlugin extends AutoPlugin {
object autoImport {
def projectMatrix: ProjectMatrix = macro ProjectMatrix.projectMatrixMacroImpl
implicit def matrixClasspathDependency[T](
implicit def matrixClasspathDependency[T](
m: T
)(implicit ev: T => ProjectMatrixReference): ProjectMatrix.MatrixClasspathDependency =
ProjectMatrix.MatrixClasspathDependency(m, None)
)(implicit ev: T => ProjectMatrixReference): ProjectMatrix.MatrixClasspathDependency =
ProjectMatrix.MatrixClasspathDependency(m, None)
implicit def matrixReferenceSyntax[T](
m: T
)(implicit ev: T => ProjectMatrixReference): ProjectMatrix.ProjectMatrixReferenceSyntax =
new ProjectMatrix.ProjectMatrixReferenceSyntax(m)
}
}

View File

@ -0,0 +1,5 @@
package a
object Main extends App {
val core = Core
}

View File

@ -0,0 +1,42 @@
lazy val check = taskKey[Unit]("")
lazy val root = (project in file("."))
.aggregate(core.projectRefs ++ app.projectRefs: _*)
.settings(
)
lazy val app = (projectMatrix in file("app"))
.aggregate(core, intf)
.dependsOn(core % Compile, intf % "compile->compile;test->test")
.settings(
name := "app"
)
.jvmPlatform(scalaVersions = Seq("2.12.8"))
lazy val core = (projectMatrix in file("core"))
.settings(
check := {
assert(moduleName.value == "core", s"moduleName is ${moduleName.value}")
val directs = libraryDependencies.value
assert(directs.size == 2, s"$directs")
},
)
.jvmPlatform(scalaVersions = Seq("2.12.8", "2.11.12"))
.configure(addStuff)
lazy val intf = (projectMatrix in file("intf"))
.settings(
check := {
assert(moduleName.value == "intf", s"moduleName is ${moduleName.value}")
},
)
.jvmPlatform(autoScalaLibrary = false)
lazy val core212 = core.jvm("2.12.8")
def addStuff(p: Project): Project = {
p.settings(
libraryDependencies += "junit" % "junit" % "4.12" % Test
)
}

View File

@ -0,0 +1,6 @@
package a
class Core {
}
object Core extends Core

View File

@ -0,0 +1,5 @@
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}

View File

@ -0,0 +1,6 @@
> compile
$ exists core/target/jvm-2.12/classes/a/Core.class
$ exists core/target/jvm-2.11/classes/a/Core.class
> coreJVM2_12/check