Skip to content

Commit

Permalink
Fold BomModule.Consumer into JavaModule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Dec 17, 2024
1 parent 47f1a71 commit 1911783
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 118 deletions.
102 changes: 0 additions & 102 deletions scalalib/src/mill/scalalib/BomModule.scala

This file was deleted.

81 changes: 73 additions & 8 deletions scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ trait JavaModule
*/
def runModuleDeps: Seq[JavaModule] = Seq.empty

/**
* BOM dependencies of this module.
* This is meant to be overridden to add BOM dependencies.
* To read the value, you should use [[bomModuleDepsChecked]] instead,
* which uses a cached result which is also checked to be free of cycle.
* @see [[bomModuleDepsChecked]]
*/
def bomModuleDeps: Seq[BomModule] = Seq.empty

/**
* Same as [[moduleDeps]] but checked to not contain cycles.
* Prefer this over using [[moduleDeps]] directly.
Expand Down Expand Up @@ -348,6 +357,16 @@ trait JavaModule
runModuleDeps
}

/**
* Same as [[bomModuleDeps]] but checked to not contain cycles.
* Prefer this over using [[bomModuleDeps]] directly.
*/
final def bomModuleDepsChecked: Seq[BomModule] = {
// trigger initialization to check for cycles
recBomModuleDeps
bomModuleDeps
}

/** Should only be called from [[moduleDepsChecked]] */
private lazy val recModuleDeps: Seq[JavaModule] =
ModuleUtils.recursive[JavaModule](
Expand All @@ -372,6 +391,14 @@ trait JavaModule
m => m.runModuleDeps ++ m.moduleDeps
)

/** Should only be called from [[bomModuleDepsChecked]] */
private lazy val recBomModuleDeps: Seq[BomModule] =
ModuleUtils.recursive[BomModule](
(millModuleSegments ++ Seq(Segment.Label("bomModuleDeps"))).render,
null,
mod => if (mod == null) bomModuleDeps else mod.bomModuleDeps
)

/** The direct and indirect dependencies of this module */
def recursiveModuleDeps: Seq[JavaModule] = { recModuleDeps }

Expand Down Expand Up @@ -482,15 +509,26 @@ trait JavaModule
)

val internalDependencies =
moduleDepsChecked.flatMap { modDep =>
Seq(
(cs.Configuration.compile, modDep.coursierDependency),
(
cs.Configuration.runtime,
modDep.coursierDependency.withConfiguration(cs.Configuration.runtime)
)
bomModuleDepsChecked.map { modDep =>
val dep = coursier.core.Dependency(
coursier.core.Module(
coursier.core.Organization("mill-internal"),
coursier.core.ModuleName(modDep.millModuleSegments.parts.mkString("-")),
Map.empty
),
"0+mill-internal"
)
(coursier.core.Configuration.`import`, dep)
} ++
moduleDepsChecked.flatMap { modDep =>
Seq(
(cs.Configuration.compile, modDep.coursierDependency),
(
cs.Configuration.runtime,
modDep.coursierDependency.withConfiguration(cs.Configuration.runtime)
)
)
} ++
compileModuleDepsChecked.map { modDep =>
(cs.Configuration.provided, modDep.coursierDependency)
} ++
Expand Down Expand Up @@ -561,7 +599,8 @@ trait JavaModule
val projects = Seq(coursierProject()) ++
T.traverse(compileModuleDepsChecked)(_.transitiveCoursierProjects)().flatten ++
T.traverse(moduleDepsChecked)(_.transitiveCoursierProjects)().flatten ++
T.traverse(runModuleDepsChecked)(_.transitiveCoursierProjects)().flatten
T.traverse(runModuleDepsChecked)(_.transitiveCoursierProjects)().flatten ++
T.traverse(bomModuleDepsChecked)(_.transitiveCoursierProjects)().flatten
projects.distinct
}

Expand Down Expand Up @@ -1475,3 +1514,29 @@ object JavaModule {
Nil
}
}

trait BomModule extends JavaModule {
def compile: T[CompilationResult] = Task {
val sources = allSourceFiles()
if (sources.nonEmpty)
throw new Exception(s"A BomModule cannot have sources")
CompilationResult(T.dest / "zinc", PathRef(T.dest / "classes"))
}

def resources: T[Seq[PathRef]] = Task {
val value = super.resources()
if (value.nonEmpty)
throw new Exception(s"A BomModule cannot have ressources")
Seq.empty[PathRef]
}

def jar: T[PathRef] = Task {
(throw new Exception(s"A BomModule doesn't have a JAR")): PathRef
}
def docJar: T[PathRef] = Task {
(throw new Exception(s"A BomModule doesn't have a doc JAR")): PathRef
}
def sourceJar: T[PathRef] = Task {
(throw new Exception(s"A BomModule doesn't have a source JAR")): PathRef
}
}
14 changes: 12 additions & 2 deletions scalalib/src/mill/scalalib/PublishModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ trait PublishModule extends JavaModule { outer =>
/**
* The packaging type. See [[PackagingType]] for specially handled values.
*/
def pomPackagingType: String = PackagingType.Jar
def pomPackagingType: String =
this match {
case _: BomModule => PackagingType.Pom
case _ => PackagingType.Jar
}

/**
* POM parent project.
Expand Down Expand Up @@ -171,7 +175,13 @@ trait PublishModule extends JavaModule { outer =>
* BOM dependency to specify in the POM
*/
def publishXmlBomDeps: Task[Agg[Dependency]] = Task.Anon {
bomIvyDeps().map(resolvePublishDependency.apply().apply(_))
val fromBomMods = T.traverse(
bomModuleDepsChecked.collect { case p: PublishModule => p }
)(_.artifactMetadata)().map { a =>
Dependency(a, Scope.Import)
}
Agg(fromBomMods: _*) ++
bomIvyDeps().map(resolvePublishDependency.apply().apply(_))
}

/**
Expand Down
12 changes: 6 additions & 6 deletions scalalib/test/src/mill/scalalib/BomTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ object BomTests extends TestSuite {
ivy"com.lihaoyi:os-lib_2.13:0.11.3"
)

object bomUser extends BomModule.Consumer with TestPublishModule {
object bomUser extends JavaModule with TestPublishModule {
def bomModuleDeps = Seq(depMgmtBomMod)
def ivyDeps = Agg(
ivy"com.lihaoyi:os-lib_2.13"
Expand All @@ -419,7 +419,7 @@ object BomTests extends TestSuite {
ivy"com.lihaoyi::os-lib:0.11.3"
)

object bomUser extends ScalaModule with BomModule.Consumer with TestPublishModule {
object bomUser extends ScalaModule with TestPublishModule {
def scalaVersion = "2.13.15"
def bomModuleDeps = Seq(scalaDepMgmtBomMod)
def ivyDeps = Agg(
Expand All @@ -436,7 +436,7 @@ object BomTests extends TestSuite {
ivy"com.lihaoyi:os-lib_2.13:0.11.3"
)

object bomUser extends BomModule.Consumer with TestPublishModule {
object bomUser extends JavaModule with TestPublishModule {
def bomModuleDeps = Seq(bomWithBom)
def ivyDeps = Agg(
ivy"com.lihaoyi:os-lib_2.13",
Expand All @@ -454,7 +454,7 @@ object BomTests extends TestSuite {
ivy"com.google.protobuf:protobuf-java:4.28.0"
)

object bomUser extends BomModule.Consumer with TestPublishModule {
object bomUser extends JavaModule with TestPublishModule {
def bomModuleDeps = Seq(bomWithBomOverride)
def ivyDeps = Agg(
ivy"com.lihaoyi:os-lib_2.13",
Expand All @@ -480,7 +480,7 @@ object BomTests extends TestSuite {
ivy"com.fasterxml.jackson.core:jackson-core:2.18.2"
)

object bomUser extends BomModule.Consumer with TestPublishModule {
object bomUser extends JavaModule with TestPublishModule {
def bomModuleDeps = Seq(simpleOverrides)
def ivyDeps = Agg(
ivy"com.fasterxml.jackson.core:jackson-core",
Expand All @@ -498,7 +498,7 @@ object BomTests extends TestSuite {
ivy"com.google.protobuf:protobuf-java:4.28.2"
)

object bomUser extends BomModule.Consumer with TestPublishModule {
object bomUser extends JavaModule with TestPublishModule {
def bomModuleDeps = Seq(crossedOverrides)
def ivyDeps = Agg(
ivy"com.fasterxml.jackson.core:jackson-core",
Expand Down

0 comments on commit 1911783

Please sign in to comment.