Skip to content

Instantly share code, notes, and snippets.

@ghale
Last active July 28, 2021 20:29
Show Gist options
  • Save ghale/37a489fed84b4187403b139f40305367 to your computer and use it in GitHub Desktop.
Save ghale/37a489fed84b4187403b139f40305367 to your computer and use it in GitHub Desktop.
Checking for an untracked output
task checkForUntrackedOutput
gradle.taskGraph.whenReady { graph ->
def kaptTasks = []
graph.allTasks.findAll { it instanceof org.jetbrains.kotlin.gradle.internal.KaptTask }.each { task ->
kaptTasks << task
}
if (graph.hasTask(':checkForUntrackedOutput')) {
kaptTasks.each { kaptTask ->
if (kaptTask.destinationDir.exists()) {
captureOutputDirContents(kaptTask)
throw new RuntimeException("${kaptTask.destinationDir.absolutePath} appears to have been created at configuration time or the outputs have not been cleaned. Did you run the clean task first?")
}
}
if (gradle.startParameter.maxWorkerCount > 1) {
throw new IllegalStateException("The checkForUntrackedOutput requires max workers to be set to 1 (i.e. --max-workers=1) in order to accurately track the task that creates the untracked output.")
}
graph.afterTask { task ->
kaptTasks.each { kaptTask ->
if (task != kaptTask && kaptTask.destinationDir.exists()) {
captureOutputDirContents(kaptTask)
throw new RuntimeException("${kaptTask.destinationDir.absolutePath} appears to have been created by ${task.path}")
}
}
}
graph.beforeTask { task ->
kaptTasks.each { kaptTask ->
if (kaptTask.destinationDir.exists()) {
captureOutputDirContents(kaptTask)
throw new IllegalStateException("${kaptTask.destinationDir.absolutePath} appears to have been created outside of task execution but not during configuration time. Next task to execute is ${task.path}")
}
}
}
}
}
def captureOutputDirContents(Task task) {
def contents = []
rootProject.fileTree(task.destinationDir).visit { contents << it.relativePath }
rootProject.buildScan.value("${task.path}.destinationDir", contents.join("\n"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment