Skip to content

Commit 4ba743a

Browse files
authored
Refactor muzzle tasks to dedicated classes, make Muzzletask cacheable (#9544)
* refactor: Proper task for muzzle printReferences * refactor: Proper tasks for muzzle reports * refactor: Proper tasks for muzzle checks * refactor: Makes MuzzleTask Cacheable Note, this only caches the muzzle assertion, tasks are being still registered depending on what is brought back from maven. I.e. if a new dependency is discovered a new task will be registered and as such will be executed. * refactor: Makes muzzle printReferences Cacheable * style: make google-java-format happy * chore: PR reviews * chore: Declare reports as inout / output * chore: Use BuildEventsListenerRegistry to print task output even when cached * chore: Extract end task output file * chore: Use kotlin dsl extension function for newInstance * chore: Use same single instance for the provider * chore: Revert the BuildEventsListenerRegistry approach * chore: Revert the declaration of versionReports as input files * chore: touch
1 parent 362e742 commit 4ba743a

File tree

16 files changed

+455
-284
lines changed

16 files changed

+455
-284
lines changed

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleAction.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.gradle.plugin.muzzle
22

3+
import org.gradle.api.GradleException
34
import org.gradle.api.file.FileCollection
45
import org.gradle.workers.WorkAction
56
import java.lang.reflect.Method
@@ -42,6 +43,12 @@ abstract class MuzzleAction : WorkAction<MuzzleWorkParameters> {
4243
Boolean::class.java,
4344
String::class.java
4445
)
46+
try {
4547
assertionMethod.invoke(null, instCL, testCL, assertPass, muzzleDirective)
48+
parameters.resultFile.get().asFile.writeText("PASSING")
49+
} catch (e: Exception) {
50+
parameters.resultFile.get().asFile.writeText(e.stackTraceToString())
51+
throw GradleException("Muzzle validation failed", e)
52+
}
4653
}
4754
}

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleDirective.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package datadog.gradle.plugin.muzzle
22

33
import org.eclipse.aether.repository.RemoteRepository
4+
import java.io.Serializable
45

56
/**
67
* A pass or fail directive for a single dependency.
78
*/
8-
open class MuzzleDirective {
9+
open class MuzzleDirective : Serializable {
910
/**
1011
* Name is optional and is used to further define the scope of a directive. The motivation for this is that this
1112
* plugin creates a config for each of the dependencies under test with name '...-<group_id>-<artifact_id>-<version>'.
@@ -20,7 +21,7 @@ open class MuzzleDirective {
2021
var versions: String? = null
2122
var skipVersions: MutableSet<String> = HashSet()
2223
var additionalDependencies: MutableList<String> = ArrayList()
23-
internal var additionalRepositories: MutableList<RemoteRepository> = ArrayList()
24+
internal var additionalRepositories: MutableList<Triple<String, String, String>> = ArrayList()
2425
internal var excludedDependencies: MutableList<String> = ArrayList()
2526
var assertPass: Boolean = false
2627
var assertInverse: Boolean = false
@@ -51,7 +52,7 @@ open class MuzzleDirective {
5152
* @param type the type of repository, defaults to "default"
5253
*/
5354
fun extraRepository(id: String, url: String, type: String = "default") {
54-
additionalRepositories.add(RemoteRepository.Builder(id, type, url).build())
55+
additionalRepositories.add(Triple(id, type, url))
5556
}
5657

5758
/**
@@ -69,13 +70,15 @@ open class MuzzleDirective {
6970
* @param defaults the default repositories
7071
* @return a list of the default repositories followed by any additional repositories
7172
*/
72-
fun getRepositories(defaults: List<RemoteRepository>): List<RemoteRepository> {
73+
internal fun getRepositories(defaults: List<RemoteRepository>): List<RemoteRepository> {
7374
return if (additionalRepositories.isEmpty()) {
7475
defaults
7576
} else {
7677
ArrayList<RemoteRepository>(defaults.size + additionalRepositories.size).apply {
7778
addAll(defaults)
78-
addAll(additionalRepositories)
79+
addAll(additionalRepositories.map { (id, type, url) ->
80+
RemoteRepository.Builder(id, type, url).build()
81+
})
7982
}
8083
}
8184
}

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleExtension.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package datadog.gradle.plugin.muzzle
22

3-
import org.eclipse.aether.repository.RemoteRepository
43
import org.gradle.api.Action
54
import org.gradle.api.model.ObjectFactory
5+
import org.gradle.kotlin.dsl.newInstance
66
import javax.inject.Inject
77
import java.util.Locale
88

99
/**
1010
* Muzzle extension containing all pass and fail directives.
1111
*/
12-
abstract class MuzzleExtension @Inject constructor(protected val objectFactory: ObjectFactory) {
12+
abstract class MuzzleExtension @Inject constructor(private val objectFactory: ObjectFactory) {
1313
val directives: MutableList<MuzzleDirective> = ArrayList()
14-
private val additionalRepositories: MutableList<RemoteRepository> = ArrayList()
14+
private val additionalRepositories: MutableList<Triple<String, String, String>> = ArrayList()
1515

1616
fun pass(action: Action<in MuzzleDirective>) {
17-
val pass = objectFactory.newInstance(MuzzleDirective::class.java)
17+
val pass = objectFactory.newInstance<MuzzleDirective>()
1818
action.execute(pass)
1919
postConstruct(pass)
2020
pass.assertPass = true
2121
directives.add(pass)
2222
}
2323

2424
fun fail(action: Action<in MuzzleDirective>) {
25-
val fail = objectFactory.newInstance(MuzzleDirective::class.java)
25+
val fail = objectFactory.newInstance<MuzzleDirective>()
2626
action.execute(fail)
2727
postConstruct(fail)
2828
fail.assertPass = false
@@ -39,7 +39,7 @@ abstract class MuzzleExtension @Inject constructor(protected val objectFactory:
3939
*/
4040
@JvmOverloads
4141
fun extraRepository(id: String, url: String, type: String = "default") {
42-
additionalRepositories.add(RemoteRepository.Builder(id, type, url).build())
42+
additionalRepositories.add(Triple(id, type, url))
4343
}
4444

4545
private fun postConstruct(directive: MuzzleDirective) {

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePlugin.kt

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package datadog.gradle.plugin.muzzle
33
import datadog.gradle.plugin.muzzle.MuzzleMavenRepoUtils.inverseOf
44
import datadog.gradle.plugin.muzzle.MuzzleMavenRepoUtils.muzzleDirectiveToArtifacts
55
import datadog.gradle.plugin.muzzle.MuzzleMavenRepoUtils.resolveVersionRange
6-
import datadog.gradle.plugin.muzzle.MuzzleReportUtils.dumpVersionRanges
7-
import datadog.gradle.plugin.muzzle.MuzzleReportUtils.mergeReports
6+
import datadog.gradle.plugin.muzzle.tasks.MuzzleEndTask
7+
import datadog.gradle.plugin.muzzle.tasks.MuzzleGenerateReportTask
8+
import datadog.gradle.plugin.muzzle.tasks.MuzzleMergeReportsTask
9+
import datadog.gradle.plugin.muzzle.tasks.MuzzleGetReferencesTask
10+
import datadog.gradle.plugin.muzzle.tasks.MuzzleTask
811
import org.eclipse.aether.artifact.Artifact
912
import org.gradle.api.NamedDomainObjectProvider
1013
import org.gradle.api.Plugin
1114
import org.gradle.api.Project
1215
import org.gradle.api.Task
1316
import org.gradle.api.artifacts.Configuration
14-
import org.gradle.api.plugins.JavaBasePlugin
15-
import org.gradle.api.plugins.JavaPlugin
1617
import org.gradle.api.tasks.TaskProvider
1718
import org.gradle.kotlin.dsl.create
1819
import org.gradle.kotlin.dsl.exclude
@@ -79,38 +80,27 @@ class MuzzlePlugin : Plugin<Project> {
7980
}
8081

8182
val muzzleTask = project.tasks.register<MuzzleTask>("muzzle") {
82-
description = "Run instrumentation muzzle on compile time dependencies"
83-
doLast {
84-
if (!project.extensions.getByType<MuzzleExtension>().directives.any { it.assertPass }) {
85-
project.logger.info("No muzzle pass directives configured. Asserting pass against instrumentation compile-time dependencies")
86-
assertMuzzle(muzzleBootstrap, muzzleTooling, project)
87-
}
88-
}
83+
this.muzzleBootstrap.set(muzzleBootstrap)
84+
this.muzzleTooling.set(muzzleTooling)
8985
dependsOn(compileMuzzle)
9086
}
9187

92-
project.tasks.register<MuzzleTask>("printReferences") {
93-
description = "Print references created by instrumentation muzzle"
94-
doLast {
95-
printMuzzle(project)
96-
}
88+
project.tasks.register<MuzzleGetReferencesTask>("printReferences") {
9789
dependsOn(compileMuzzle)
90+
}.also {
91+
val printReferencesTask = project.tasks.register("actuallyPrintReferences") {
92+
doLast {
93+
println(it.get().outputFile.get().asFile.readText())
94+
}
95+
}
96+
it.configure { finalizedBy(printReferencesTask) }
9897
}
9998

100-
project.tasks.register<MuzzleTask>("generateMuzzleReport") {
101-
description = "Print instrumentation version report"
102-
doLast {
103-
dumpVersionRanges(project)
104-
}
99+
project.tasks.register<MuzzleGenerateReportTask>("generateMuzzleReport") {
105100
dependsOn(compileMuzzle)
106101
}
107102

108-
project.tasks.register<MuzzleTask>("mergeMuzzleReports") {
109-
description = "Merge generated version reports in one unique csv"
110-
doLast {
111-
mergeReports(project)
112-
}
113-
}
103+
project.tasks.register<MuzzleMergeReportsTask>("mergeMuzzleReports")
114104

115105
val hasRelevantTask = project.gradle.startParameter.taskNames.any { taskName ->
116106
// removing leading ':' if present
@@ -133,7 +123,7 @@ class MuzzlePlugin : Plugin<Project> {
133123
var runAfter: TaskProvider<MuzzleTask> = muzzleTask
134124

135125
project.extensions.getByType<MuzzleExtension>().directives.forEach { directive ->
136-
project.logger.debug("configuring $directive")
126+
project.logger.debug("configuring {}", directive)
137127

138128
if (directive.isCoreJdk) {
139129
runAfter = addMuzzleTask(directive, null, project, runAfter, muzzleBootstrap, muzzleTooling)
@@ -157,11 +147,8 @@ class MuzzlePlugin : Plugin<Project> {
157147
project.logger.info("configured $directive")
158148
}
159149

160-
val timingTask = project.tasks.register("muzzle-end") {
161-
doLast {
162-
val endTime = System.currentTimeMillis()
163-
MuzzleReportUtils.generateResultsXML(project, endTime - startTime)
164-
}
150+
val timingTask = project.tasks.register<MuzzleEndTask>("muzzle-end") {
151+
startTimeMs.set(startTime)
165152
}
166153
// last muzzle task to run
167154
runAfter.configure {
@@ -252,9 +239,9 @@ class MuzzlePlugin : Plugin<Project> {
252239
}
253240

254241
val muzzleTask = instrumentationProject.tasks.register<MuzzleTask>(muzzleTaskName) {
255-
doLast {
256-
assertMuzzle(muzzleBootstrap, muzzleTooling, instrumentationProject, muzzleDirective)
257-
}
242+
this.muzzleDirective.set(muzzleDirective)
243+
this.muzzleBootstrap.set(muzzleBootstrap)
244+
this.muzzleTooling.set(muzzleTooling)
258245
}
259246

260247
runAfterTask.configure {

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePluginUtils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import org.gradle.api.file.FileCollection
55
import org.gradle.api.tasks.SourceSet
66
import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
77
import org.gradle.api.tasks.SourceSetContainer
8+
import org.gradle.build.event.BuildEventsListenerRegistry
89
import org.gradle.kotlin.dsl.findByType
910
import org.gradle.kotlin.dsl.getByType
11+
import org.gradle.tooling.events.FinishEvent
12+
import org.gradle.tooling.events.OperationCompletionListener
13+
import org.gradle.tooling.events.task.TaskFinishEvent
14+
import org.gradle.tooling.events.task.TaskSuccessResult
1015

1116
internal val Project.mainSourceSet: SourceSet
1217
get() = extensions.findByType<SourceSetContainer>()
@@ -17,3 +22,6 @@ internal val Project.allMainSourceSet: List<SourceSet>
1722
get() = extensions.findByType<SourceSetContainer>()
1823
?.filter { it.name.startsWith(MAIN_SOURCE_SET_NAME) }
1924
.orEmpty()
25+
26+
internal val Project.pathSlug: String
27+
get() = path.removePrefix(":").replace(':', '_')

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleReportUtils.kt

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)