-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some helpers to simplify cross-version/cross-platform modules (#2406
) This PR attempts to minimize the boilerplate of defining cross-version cross-platform Scala modules. Currently, cross-version cross-platform modules involves a lot annoying fiddling with overriding `millSourcePath` and `sources` and `artifactName` and passing around `crossScalaVersion` and all that. Simple `Cross`/`CrossScalaModule`s get some of the plumbing handled behind the scenes, but anything more complicated and the plumbing spills out again into plain view. And although `Cross`/`CrossScalaModule`s have less boilerplate, it's still enough boilerplate you don't want to go around defining them over and over This PR tries to hide all of them behind helper traits `CrossScalaModule.Wrapper` and `PlatformScalaModule`, so your build file only has the logic you care about and none of the plumbing. ## Major changes 1. We replace the `artifactName` definition, so that instead of reading from `millModuleSegments` directly, it reads from `artifactNameParts: Seq[String]`. This makes it easier for us to splice the artifact name parts to remove unwanted segments without needing to do fragile string manipulation 2. Introduce `CrossScalaModule.Wrapper`. This can be used when the `ScalaModule`s you are defining are not the direct children of the `Cross` module, but are nested somewhere inside of it. `CrossScalaModule.Wrapper` shades the `CrossScalaModule` trait, so that any `CrossScalaModule` defined within its body would have the `crossScalaVersion` automatically wired up and the `artifactNameParts` properly adjusted to remove the cross segment in the middle of the artifact name 3. Introduce `PlatformScalaModule`. This is analogous to `CrossScalaModule`, in that it adjusts the `millSourcePath`/`artifactNameParts` to drop the last segment, and adjusts `sources` to include the `-{jvm,js,native}` suffix ## Tests The changes are largely covered by existing tests, including the `example/` tests which were adjusted to make use of the new traits ## Notes 1. We cannot make the platform-specific sub-modules into `Cross` modules like we do with the version-specific sub-modules, since the platform-specific submodules have pretty different types/targets defined on them and `Cross` modules must all be of the same type 2. `6-cross-platform-publishing`, is a lot better than before, but there is still a significant amount of boilerplate involved, especially the lines around `object wrapper`. Not sure if there are other things we can do to make it even easier to define these cross-version/platform modules
- Loading branch information
Showing
19 changed files
with
153 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package baz | ||
import scalatags.Text.all._ | ||
import mainargs.{main, ParserForMethods, arg} | ||
|
||
object Baz { | ||
@main | ||
def main(@arg(name = "bar-text") barText: String, | ||
@arg(name = "qux-text") quxText: String, | ||
@arg(name = "baz-text") bazText: String): Unit = { | ||
foo.qux.Qux.main(barText, quxText) | ||
|
||
val value = p(bazText) | ||
println("Baz.value: " + value) | ||
} | ||
|
||
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...-nested-modules/wrapper/bar/src/Bar.scala → ...ic/5-nested-modules/foo/bar/src/Bar.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 5 additions & 8 deletions
13
.../basic/5-nested-modules/qux/src/Qux.scala → ...ic/5-nested-modules/foo/qux/src/Qux.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...pper/foo/src-js/FooPlatformSpecific.scala → .../foo/qux/src-js/QuxPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...per/foo/src-jvm/FooPlatformSpecific.scala → ...foo/qux/src-jvm/QuxPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...form-publishing/wrapper/foo/src/Foo.scala → ...platform-publishing/foo/qux/src/Qux.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package foo | ||
package qux | ||
import scalatags.Text.all._ | ||
object Foo { | ||
object Qux { | ||
def main(args: Array[String]): Unit = { | ||
println("Bar.value: " + bar.Bar.value) | ||
val string = """{"i": "am", "cow": "hear", "me": "moo"}""" | ||
println("Foo.main: " + FooPlatformSpecific.parseJsonGetKeys(string).map(p(_))) | ||
println("Qux.main: " + QuxPlatformSpecific.parseJsonGetKeys(string).map(p(_))) | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...shing/wrapper/foo/test/src/FooTests.scala → ...ublishing/foo/qux/test/src/QuxTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Base]], 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) | ||
} |