Skip to content

Commit 89c5eda

Browse files
committed
use Mill caching for stub folder
1 parent 9dd5936 commit 89c5eda

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

libs/groovylib/api/src/mill/groovylib/worker/api/GroovyWorker.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package mill.groovylib.worker.api
22

3-
import mill.api.{Result, TaskCtx}
3+
import mill.api.{PathRef, Result, TaskCtx}
44
import mill.javalib.api.CompilationResult
55

66
/**
@@ -25,7 +25,7 @@ trait GroovyWorker {
2525
)(implicit
2626
ctx: TaskCtx
2727
)
28-
: Result[CompilationResult]
28+
: Result[Unit]
2929

3030
/**
3131
* Compiles the Groovy sources. In a mixed setup this method assumes that the Java stubs

libs/groovylib/src/mill/groovylib/GroovyModule.scala

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
132132
groovyCompileTask()()
133133
}
134134

135+
def compileGroovyStubs: T[Result[Unit]] = Task(persistent = true){
136+
val groovySourceFiles = allGroovySourceFiles().map(_.path)
137+
val stubDir = compileGeneratedGroovyStubs()
138+
Task.ctx().log.info(s"Generating Java stubs for ${groovySourceFiles.size} Groovy sources to $stubDir ...")
139+
140+
val compileCp = compileClasspath().map(_.path).filter(os.exists)
141+
val config = GroovyCompilerConfiguration(
142+
enablePreview = groovyCompileEnablePreview(),
143+
targetBytecode = groovyCompileTargetBytecode(),
144+
disabledGlobalAstTransformations = disabledGlobalAstTransformations()
145+
)
146+
147+
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
148+
_.compileGroovyStubs(groovySourceFiles, compileCp, stubDir, config)
149+
}
150+
}
151+
152+
/**
153+
* Path to Java stub sources as part of the `compile` step. Stubs are generated
154+
* by the Groovy compiler and later used by the Java compiler.
155+
*/
156+
def compileGeneratedGroovyStubs: T[os.Path] = Task(persistent = true) { Task.dest }
157+
135158
/**
136159
* The actual Groovy compile task (used by [[compile]]).
137160
*/
@@ -147,11 +170,21 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
147170

148171
val isGroovy = groovySourceFiles.nonEmpty
149172
val isJava = javaSourceFiles.nonEmpty
150-
val isMixed = isGroovy && isJava
151-
152173
val compileCp = compileClasspath().map(_.path).filter(os.exists)
153174
val updateCompileOutput = upstreamCompileOutput()
154175

176+
sealed trait CompilationStrategy
177+
case object JavaOnly extends CompilationStrategy
178+
case object GroovyOnly extends CompilationStrategy
179+
case object Mixed extends CompilationStrategy
180+
181+
val strategy: CompilationStrategy = (isJava, isGroovy) match {
182+
case (true, false) => JavaOnly
183+
case (false, true) => GroovyOnly
184+
case (true, true) => Mixed
185+
case (false, false) => JavaOnly // fallback, though this shouldn't happen
186+
}
187+
155188
val config = GroovyCompilerConfiguration(
156189
enablePreview = groovyCompileEnablePreview(),
157190
targetBytecode = groovyCompileTargetBytecode(),
@@ -167,21 +200,14 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
167200
worker = jvmWorkerRef().internalWorker(),
168201
upstreamCompileOutput = updateCompileOutput,
169202
javaSourceFiles = javaSourceFiles,
170-
compileCp = compileCp,
203+
compileCp = compileCp :+ compileGeneratedGroovyStubs(),
171204
javaHome = javaHome().map(_.path),
172205
javacOptions = javacOptions(),
173206
compileProblemReporter = ctx.reporter(hashCode),
174207
reportOldProblems = zincReportCachedProblems()
175208
)
176209
}
177210

178-
def compileGroovyStubs(): Result[CompilationResult] = {
179-
ctx.log.info("Compiling Groovy stubs for mixed compilation")
180-
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
181-
_.compileGroovyStubs(groovySourceFiles, compileCp, classes, config)
182-
}
183-
}
184-
185211
def compileGroovy(): Result[CompilationResult] = {
186212
ctx.log.info(
187213
s"Compiling ${groovySourceFiles.size} Groovy sources to $classes ..."
@@ -192,7 +218,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
192218
}
193219

194220
// TODO figure out if there is a better way to do this
195-
val analysisFile = dest / "groovy.analysis.dummy" // needed for mills CompilationResult
221+
val analysisFile = dest / "groovy.analysis.dummy" // needed for Mills CompilationResult
196222
os.write(target = analysisFile, data = "", createFolders = true)
197223

198224
workerGroovyResult match {
@@ -202,18 +228,19 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
202228
}
203229
}
204230

205-
val firstAndSecondStage = if (isMixed) {
206-
// only compile Java if Stubs are successfully generated
207-
compileGroovyStubs().flatMap(_ => compileJava)
208-
} else {
209-
Result.Success
210-
}
231+
strategy match {
232+
case JavaOnly =>
233+
compileJava
211234

212-
if (isMixed || isGroovy) {
213-
firstAndSecondStage.flatMap(_ => compileGroovy())
214-
} else {
215-
compileJava
235+
case GroovyOnly =>
236+
compileGroovy()
237+
238+
case Mixed =>
239+
compileGroovyStubs()
240+
.flatMap(_ => compileJava)
241+
.flatMap(_ => compileGroovy())
216242
}
243+
217244
}
218245

219246
private[groovylib] def internalCompileJavaFiles(

libs/groovylib/worker/src/mill/groovylib/worker/impl/GroovyWorkerImpl.scala

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package mill.groovylib.worker.impl
22

33
import groovy.lang.GroovyClassLoader
4-
import mill.api.Result
5-
import mill.api.TaskCtx
4+
import mill.api.{PathRef, Result, TaskCtx}
65
import mill.javalib.api.CompilationResult
76
import mill.groovylib.worker.api.{GroovyCompilerConfiguration, GroovyWorker}
87
import org.codehaus.groovy.control.{CompilationUnit, CompilerConfiguration, Phases}
@@ -19,13 +18,13 @@ class GroovyWorkerImpl extends GroovyWorker {
1918
classpath: Seq[Path],
2019
outputDir: Path,
2120
config: GroovyCompilerConfiguration
22-
)(implicit ctx: TaskCtx): Result[CompilationResult] = {
21+
)(implicit ctx: TaskCtx): Result[Unit] = {
2322
val compilerConfig = new CompilerConfiguration()
2423
compilerConfig.setTargetDirectory(outputDir.toIO)
2524
compilerConfig.setClasspathList(classpath.map(_.toIO.getAbsolutePath).asJava)
2625
compilerConfig.setJointCompilationOptions(Map(
2726
"stubDir" -> outputDir.toIO,
28-
"keepStubs" -> false
27+
"keepStubs" -> true
2928
).asJava)
3029
compilerConfig.setDisabledGlobalASTTransformations(
3130
config.disabledGlobalAstTransformations.asJava
@@ -46,10 +45,9 @@ class GroovyWorkerImpl extends GroovyWorker {
4645

4746
Try {
4847
stubUnit.compile(Phases.CONVERSION)
49-
CompilationResult(outputDir, mill.api.PathRef(outputDir))
5048
}.fold(
5149
exception => Result.Failure(s"Groovy stub generation failed: ${exception.getMessage}"),
52-
result => Result.Success(result)
50+
result => Result.Success(())
5351
)
5452

5553
}
@@ -88,19 +86,10 @@ class GroovyWorkerImpl extends GroovyWorker {
8886

8987
Try {
9088
unit.compile(Phases.OUTPUT)
91-
removeAllJavaFiles(outputDir)
9289
CompilationResult(outputDir, mill.api.PathRef(outputDir))
9390
}.fold(
9491
exception => Result.Failure(s"Groovy compilation failed: ${exception.getMessage}"),
9592
result => Result.Success(result)
9693
)
9794
}
98-
99-
private def removeAllJavaFiles(outputDir: os.Path): Unit = {
100-
if (os.exists(outputDir)) {
101-
os.walk(outputDir)
102-
.filter(_.ext == "java")
103-
.foreach(os.remove)
104-
}
105-
}
10695
}

0 commit comments

Comments
 (0)