Skip to content

Commit af1a196

Browse files
authored
Clean up main method discovery logic (#4762)
This PR removes the task definitions from `JavaModule` that have already been moved into `RunModule`, encapsulating the difference between `JavaModule` and other `RunModule`s that do not use Zinc (e.g. `KotlinModule`) in the `def allLocalMainClasses` task. This should be semantically identical as before, just with a bit less indirection
1 parent e5e3289 commit af1a196

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala

+1-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,5 @@ trait ZincWorkerApi {
5151
/**
5252
* Discover main classes by inspecting the classpath.
5353
*/
54-
def discoverMainClasses(classpath: Seq[os.Path]): Seq[String] = {
55-
// We need this default-impl to keep binary compatibility (0.11.x)
56-
Seq.empty
57-
}
54+
def discoverMainClasses(classpath: Seq[os.Path]): Seq[String]
5855
}

scalalib/src/mill/scalalib/JavaModule.scala

+1-34
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,7 @@ trait JavaModule
9999
Artifact.fromDepJava(_: Dep)
100100
}
101101

102-
/**
103-
* Allows you to specify an explicit main class to use for the `run` command.
104-
* If none is specified, the classpath is searched for an appropriate main
105-
* class to use if one exists
106-
*/
107-
def mainClass: T[Option[String]] = None
108-
109-
def finalMainClassOpt: T[Either[String, String]] = Task {
110-
mainClass() match {
111-
case Some(m) => Right(m)
112-
case None =>
113-
if (zincWorker().javaHome().isDefined) {
114-
super[RunModule].finalMainClassOpt()
115-
} else {
116-
zincWorker().worker().discoverMainClasses(compile()) match {
117-
case Seq() => Left("No main class specified or found")
118-
case Seq(main) => Right(main)
119-
case mains =>
120-
Left(
121-
s"Multiple main classes found (${mains.mkString(",")}) " +
122-
"please explicitly specify which one to use by overriding `mainClass` " +
123-
"or using `runMain <main-class> <...args>` instead of `run`"
124-
)
125-
}
126-
}
127-
}
128-
}
129-
130-
def finalMainClass: T[String] = Task {
131-
finalMainClassOpt() match {
132-
case Right(main) => Result.Success(main)
133-
case Left(msg) => Result.Failure(msg)
134-
}
135-
}
102+
def allLocalMainClasses0 = Task { zincWorker().worker().discoverMainClasses(compile()) }
136103

137104
/**
138105
* Mandatory ivy dependencies that are typically always required and shouldn't be removed by

scalalib/src/mill/scalalib/RunModule.scala

+16-6
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,29 @@ trait RunModule extends WithZincWorker {
4545
*/
4646
def mainClass: T[Option[String]] = None
4747

48+
/**
49+
* Same as [[allLocalMainClasses]], but only for modules with a custom
50+
* JVM version configured
51+
*/
52+
def allLocalMainClasses0: T[Seq[String]] = Task {
53+
zincWorker().worker().discoverMainClasses(localRunClasspath().map(_.path))
54+
}
55+
56+
/**
57+
* All main classes detected in this module that can serve as program entrypoints
58+
*/
4859
def allLocalMainClasses: T[Seq[String]] = Task {
49-
val classpath = localRunClasspath().map(_.path)
50-
if (zincWorker().javaHome().isDefined) {
60+
if (zincWorker().javaHome().isEmpty) allLocalMainClasses0()
61+
else {
5162
Jvm.callProcess(
5263
mainClass = "mill.scalalib.worker.DiscoverMainClassesMain",
5364
classPath = zincWorker().classpath().map(_.path).toVector,
54-
mainArgs = Seq(classpath.mkString(",")),
65+
mainArgs = Seq(localRunClasspath().map(_.path).mkString(",")),
5566
javaHome = zincWorker().javaHome().map(_.path),
5667
stdin = os.Inherit,
5768
stdout = os.Pipe,
5869
cwd = Task.dest
5970
).out.lines()
60-
} else {
61-
zincWorker().worker().discoverMainClasses(classpath)
6271
}
6372
}
6473

@@ -72,7 +81,8 @@ trait RunModule extends WithZincWorker {
7281
case mains =>
7382
Left(
7483
s"Multiple main classes found (${mains.mkString(",")}) " +
75-
"please explicitly specify which one to use by overriding mainClass"
84+
"please explicitly specify which one to use by overriding `mainClass` " +
85+
"or using `runMain <main-class> <...args>` instead of `run`"
7686
)
7787
}
7888
}

0 commit comments

Comments
 (0)