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 mill.define.DynamicModule to allow custom overriding of millModuleDirectChildren #2574

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions main/define/src/mill/define/DynamicModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mill.define

/**
* A module which you can override [[millModuleDirectChildren]] to dynamically
* enable or disable child modules at runtime
*/
trait DynamicModule extends Module
83 changes: 55 additions & 28 deletions main/resolve/src/mill/resolve/ResolveCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ private object ResolveCore {
segments.value.foldLeft[Either[String, Module]](Right(rootModule)) {
case (Right(current), Segment.Label(s)) =>
assert(s != "_", s)
resolveDirectChildren0(current.getClass, Some(s)) match {
resolveDirectChildren0(
rootModule,
current.millModuleSegments,
current.getClass,
Some(s)
).flatMap {
case Seq((_, Some(f))) => f(current)
case unknown =>
sys.error(
Expand Down Expand Up @@ -204,42 +209,64 @@ private object ResolveCore {
}
} else Right(Nil)

crossesOrErr.map { crosses =>
resolveDirectChildren0(cls, nameOpt)
.map {
case (Resolved.Module(s, cls), _) => Resolved.Module(segments ++ s, cls)
case (Resolved.Target(s), _) => Resolved.Target(segments ++ s)
case (Resolved.Command(s), _) => Resolved.Command(segments ++ s)
}
.toSet
.++(crosses)
crossesOrErr.flatMap { crosses =>
resolveDirectChildren0(rootModule, segments, cls, nameOpt)
.map(
_.map {
case (Resolved.Module(s, cls), _) => Resolved.Module(segments ++ s, cls)
case (Resolved.Target(s), _) => Resolved.Target(segments ++ s)
case (Resolved.Command(s), _) => Resolved.Command(segments ++ s)
}
.toSet
.++(crosses)
)
}
}

def resolveDirectChildren0(
rootModule: Module,
segments: Segments,
cls: Class[_],
nameOpt: Option[String]
): Seq[(Resolved, Option[Module => Either[String, Module]])] = {
): Either[String, Seq[(Resolved, Option[Module => Either[String, Module]])]] = {
def namePred(n: String) = nameOpt.isEmpty || nameOpt.contains(n)

val modules = Reflect
.reflectNestedObjects0[Module](cls, namePred)
.map { case (name, member) =>
Resolved.Module(
Segments.labels(decode(name)),
member match {
case f: java.lang.reflect.Field => f.getType
case f: java.lang.reflect.Method => f.getReturnType
}
) -> (
member match {
case f: java.lang.reflect.Field =>
Some((x: Module) => catchWrapException(f.get(x).asInstanceOf[Module]))
val modulesOrErr: Either[String, Seq[(Resolved, Option[Module => Either[String, Module]])]] =
if (classOf[DynamicModule].isAssignableFrom(cls)) {
instantiateModule(rootModule, segments).map {
case m: DynamicModule =>
m.millModuleDirectChildren
.filter(c => namePred(c.millModuleSegments.parts.last))
.map(c =>
(
Resolved.Module(
Segments.labels(c.millModuleSegments.parts.last),
c.getClass
),
Some((x: Module) => Right(c))
)
)
}
} else Right {
Reflect
.reflectNestedObjects0[Module](cls, namePred)
.map { case (name, member) =>
Resolved.Module(
Segments.labels(decode(name)),
member match {
case f: java.lang.reflect.Field => f.getType
case f: java.lang.reflect.Method => f.getReturnType
}
) -> (
member match {
case f: java.lang.reflect.Field =>
Some((x: Module) => catchWrapException(f.get(x).asInstanceOf[Module]))

case f: java.lang.reflect.Method =>
Some((x: Module) => catchWrapException(f.invoke(x).asInstanceOf[Module]))
case f: java.lang.reflect.Method =>
Some((x: Module) => catchWrapException(f.invoke(x).asInstanceOf[Module]))
}
)
}
)
}

val targets = Reflect
Expand All @@ -254,7 +281,7 @@ private object ResolveCore {
.map(m => decode(m.getName))
.map { name => Resolved.Command(Segments.labels(name)) -> None }

modules ++ targets ++ commands
modulesOrErr.map(_ ++ targets ++ commands)
}

def notFoundResult(rootModule: Module, querySoFar: Segments, current: Resolved, next: Segment) = {
Expand Down
30 changes: 30 additions & 0 deletions main/resolve/test/src/mill/main/ResolveTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -759,5 +759,35 @@ object ResolveTests extends TestSuite {
Set("sub.inner.baseTarget")
)
}
test("dynamicModule") {
val check = new Checker(dynamicModule)
test - check(
"normal.inner.target",
Right(Set(_.normal.inner.target)),
Set("normal.inner.target")
)
test - check(
"normal._.target",
Right(Set(_.normal.inner.target)),
Set("normal.inner.target")
)
test - check(
"niled.inner.target",
Left(
"Cannot resolve niled.inner.target. Try `mill resolve niled._` to see what's available."
),
Set()
)
test - check(
"niled._.target",
Left("Cannot resolve niled._.target. Try `mill resolve niled._` to see what's available."),
Set()
)
test - check(
"__.target",
Right(Set(_.normal.inner.target)),
Set("normal.inner.target")
)
}
}
}
18 changes: 17 additions & 1 deletion main/test/src/mill/util/TestGraphs.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.util
import TestUtil.test
import mill.define.{ModuleRef, Command, Cross, Discover, TaskModule}
import mill.define.{Command, Cross, Discover, DynamicModule, ModuleRef, TaskModule}
import mill.{Module, T}

/**
Expand Down Expand Up @@ -233,6 +233,22 @@ class TestGraphs() {

override lazy val millDiscover = Discover[this.type]
}

object dynamicModule extends TestUtil.BaseModule {
object normal extends DynamicModule {
object inner extends Module {
def target = T { 1 }
}
}
object niled extends DynamicModule {
override def millModuleDirectChildren: Seq[Module] = Nil
object inner extends Module {
def target = T { 1 }
}
}

override lazy val millDiscover = Discover[this.type]
}
}

object TestGraphs {
Expand Down