Skip to content

Commit

Permalink
Merge pull request #23 from sbt/wip/percent
Browse files Browse the repository at this point in the history
Add % syntax for configuration scoping
  • Loading branch information
eed3si9n authored Apr 13, 2020
2 parents 609917c + 20821a1 commit 3e38d9c
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -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"))

Expand Down
35 changes: 29 additions & 6 deletions src/main/scala/sbt/internal/ProjectMatrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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] = {
Expand Down Expand Up @@ -232,6 +247,7 @@ object ProjectMatrix {
inConfig(Test)(makeSources(nonScalaDirSuffix, svDirSuffix))
)
.settings(self.settings)
.configure(transforms: _*)

r -> r.process(p)
}): _*)
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand All @@ -437,7 +457,8 @@ object ProjectMatrix {
dependencies,
settings,
configurations,
plugins
plugins,
transforms
)
allMatrices(id) = matrix
matrix
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -471,7 +493,8 @@ object ProjectMatrix {
dependencies,
settings,
configurations,
plugins
plugins,
transforms
)

def lookupMatrix(local: LocalProjectMatrix): ProjectMatrix = {
Expand Down
11 changes: 8 additions & 3 deletions src/main/scala/sbtprojectmatrix/ProjectMatrixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package a

object Main extends App {
val core = Core
}
42 changes: 42 additions & 0 deletions src/sbt-test/projectMatrix/jvm-with-scoping/build.sbt
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package a

class Core {
}

object Core extends Core
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions src/sbt-test/projectMatrix/jvm-with-scoping/test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3e38d9c

Please sign in to comment.