Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some helpers to simplify cross-version/cross-platform modules #2406

Merged
merged 8 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 1 addition & 2 deletions example/web/5-webapp-scalajs-shared/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ object app extends RootModule with AppScalaModule{
}

object shared extends Module{
trait SharedModule extends AppScalaModule{
def millSourcePath = super.millSourcePath / os.up
trait SharedModule extends AppScalaModule with PlatformScalaModule {

def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags::0.12.0",
Expand Down
44 changes: 15 additions & 29 deletions example/web/6-cross-platform-publishing/build.sc
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import mill._, scalalib._, scalajslib._, publish._

object wrapper extends Cross[WrapperModule]("2.13.10", "3.2.2")
class WrapperModule(val crossScalaVersion: String) extends Module {

trait MyModule extends CrossScalaModule with PublishModule {
def artifactName = millModuleSegments.parts.dropRight(1).last

def crossScalaVersion = WrapperModule.this.crossScalaVersion
def millSourcePath = super.millSourcePath / os.up
class WrapperModule(val crossScalaVersion: String) extends CrossScalaModule.Wrapper {
trait MyScalaModule extends CrossScalaModule with PlatformScalaModule with PublishModule {
def publishVersion = "0.0.1"

def pomSettings = PomSettings(
Expand All @@ -25,23 +20,14 @@ class WrapperModule(val crossScalaVersion: String) extends Module {
def ivyDeps = Agg(ivy"com.lihaoyi::utest::0.7.11")
def testFramework = "utest.runner.Framework"
}

def sources = T.sources {
val platform = millModuleSegments.parts.last
super.sources().flatMap(source =>
Seq(
source,
PathRef(source.path / os.up / s"${source.path.last}-${platform}")
)
)
}
}
trait MyScalaJSModule extends MyModule with ScalaJSModule {

trait MyScalaJSModule extends MyScalaModule with ScalaJSModule {
def scalaJSVersion = "1.13.0"
}

object foo extends Module{
object jvm extends MyModule{
object jvm extends MyScalaModule{
def moduleDeps = Seq(bar.jvm)
def ivyDeps = super.ivyDeps() ++ Agg(ivy"com.lihaoyi::upickle::3.0.0")
}
Expand All @@ -51,7 +37,7 @@ class WrapperModule(val crossScalaVersion: String) extends Module {
}

object bar extends Module{
object jvm extends MyModule
object jvm extends MyScalaModule
object js extends MyScalaJSModule
}
}
Expand Down Expand Up @@ -93,12 +79,12 @@ Parsing JSON with js.JSON.parse
Foo.main: Set(<p>i</p>, <p>cow</p>, <p>me</p>)

> ./mill __.publishLocal
Publishing Artifact(com.lihaoyi,bar_sjs1_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,bar_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,foo_sjs1_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,foo_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,bar_sjs1_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,bar_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,foo_sjs1_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,foo_3,0.0.1) to ivy repo
*/
Publishing Artifact(com.lihaoyi,wrapper-bar_sjs1_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-bar_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-foo_sjs1_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-foo_2.13,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-bar_sjs1_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-bar_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-foo_sjs1_3,0.0.1) to ivy repo
Publishing Artifact(com.lihaoyi,wrapper-foo_3,0.0.1) to ivy repo
*/
3 changes: 2 additions & 1 deletion scalalib/src/mill/scalalib/CrossModuleBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ trait CrossModuleBase extends ScalaModule {
protected def scalaVersionDirectoryNames: Seq[String] =
ZincWorkerUtil.matchingVersions(crossScalaVersion)

override def artifactName: T[String] = millModuleSegments.parts.init.mkString("-")
protected def wrapperSegments = millModuleSegments.parts
override def artifactNameParts = super.artifactNameParts().patch(wrapperSegments.size - 1, Nil, 1)
implicit def crossSbtModuleResolver: Resolver[CrossModuleBase] =
new Resolver[CrossModuleBase] {
def resolve[V <: CrossModuleBase](c: Cross[V]): V = {
Expand Down
23 changes: 20 additions & 3 deletions scalalib/src/mill/scalalib/CrossScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import mill.api.PathRef
import mill.T

/**
* A [[ScalaModule]] with is suited to be used with [[mill.define.Cross]].
* It supports additional source directories with the scala version pattern as suffix (`src-{scalaversionprefix}`),
* e.g.
* A [[ScalaModule]] which is suited to be used with [[mill.define.Cross]].
* It supports additional source directories with the scala version pattern
* as suffix (`src-{scalaversionprefix}`), e.g.
*
* - src
* - src-2.11
* - src-2.12.3
Expand All @@ -25,3 +26,19 @@ trait CrossScalaModule extends ScalaModule with CrossModuleBase { outer =>
}
trait Tests extends CrossScalaModuleTests
}

object CrossScalaModule{
/**
* Used with a [[mill.define.Cross]] when you want [[CrossScalaModule]]'s
* nested within it
*/
trait Wrapper extends mill.Module {
def crossScalaVersion: String
private def wrapperSegments0 = millModuleSegments.parts
trait CrossScalaModule extends mill.scalalib.CrossScalaModule {
override def wrapperSegments = wrapperSegments0
def crossScalaVersion = Wrapper.this.crossScalaVersion

}
}
}
4 changes: 3 additions & 1 deletion scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,9 @@ trait JavaModule
* For example, by default a scala module foo.baz might be published as foo-baz_2.12 and a java module would be foo-baz.
* Setting this to baz would result in a scala artifact baz_2.12 or a java artifact baz.
*/
def artifactName: T[String] = millModuleSegments.parts.mkString("-")
def artifactName: T[String] = artifactNameParts().mkString("-")

def artifactNameParts: T[Seq[String]] = millModuleSegments.parts

/**
* The exact id of the artifact to be published. You probably don't want to override this.
Expand Down
29 changes: 29 additions & 0 deletions scalalib/src/mill/scalalib/PlatformScalaModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mill.scalalib
import mill._

/**
* A [[ScalaModule]] intended for defining `.jvm`/`.js`/`.native` submodules
* It supports additional source directories per platform, e.g. `src-jvm/` or
* `src-js/` and can be used inside a [[CrossScalaModule.Wrapper]], to get one
* source folder per platform per version e.g. `src-2.12-jvm/`.
*
* Adjusts the [[millSourcePath]] and [[artifactNameParts]] to ignore the last
* path segment, which is assumed to be the name of the platform the module is
* built against and not something that should affect the filesystem path or
* artifact name
*/
trait PlatformScalaModule extends ScalaModule{
override def millSourcePath = super.millSourcePath / os.up

override def sources = T.sources {
val platform = millModuleSegments.parts.last
super.sources().flatMap(source =>
Seq(
source,
PathRef(source.path / os.up / s"${source.path.last}-${platform}")
)
)
}

override def artifactNameParts = super.artifactNameParts().dropRight(1)
}