Skip to content

Commit 80c20d8

Browse files
committed
refactor: Proper tasks for muzzle checks
1 parent 8966252 commit 80c20d8

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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.tasks.MuzzleEndTask
67
import datadog.gradle.plugin.muzzle.tasks.MuzzleGenerateReportTask
78
import datadog.gradle.plugin.muzzle.tasks.MuzzleMergeReportsTask
89
import datadog.gradle.plugin.muzzle.tasks.MuzzlePrintReferencesTask
@@ -79,13 +80,15 @@ 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)
85+
// description = "Run instrumentation muzzle on compile time dependencies"
86+
// doLast {
87+
// if (!project.extensions.getByType<MuzzleExtension>().directives.any { it.assertPass }) {
88+
// project.logger.info("No muzzle pass directives configured. Asserting pass against instrumentation compile-time dependencies")
89+
// assertMuzzle(muzzleBootstrap, muzzleTooling, project)
90+
// }
91+
// }
8992
dependsOn(compileMuzzle)
9093
}
9194

@@ -144,7 +147,8 @@ class MuzzlePlugin : Plugin<Project> {
144147
project.logger.info("configured $directive")
145148
}
146149

147-
val timingTask = project.tasks.register("muzzle-end") {
150+
val timingTask = project.tasks.register<MuzzleEndTask>("muzzle-end") {
151+
startTimeMs.set(startTime)
148152
doLast {
149153
val endTime = System.currentTimeMillis()
150154
MuzzleReportUtils.generateResultsXML(project, endTime - startTime)
@@ -239,9 +243,12 @@ class MuzzlePlugin : Plugin<Project> {
239243
}
240244

241245
val muzzleTask = instrumentationProject.tasks.register<MuzzleTask>(muzzleTaskName) {
242-
doLast {
243-
assertMuzzle(muzzleBootstrap, muzzleTooling, instrumentationProject, muzzleDirective)
244-
}
246+
this.muzzleDirective.set(muzzleDirective)
247+
this.muzzleBootstrap.set(muzzleBootstrap)
248+
this.muzzleTooling.set(muzzleTooling)
249+
// doLast {
250+
// assertMuzzle(muzzleBootstrap, muzzleTooling, instrumentationProject, muzzleDirective)
251+
// }
245252
}
246253

247254
runAfterTask.configure {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package datadog.gradle.plugin.muzzle.tasks
2+
3+
import datadog.gradle.plugin.muzzle.MuzzleReportUtils
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.tasks.Input
6+
import org.gradle.api.tasks.TaskAction
7+
8+
abstract class MuzzleEndTask : AbstractMuzzleTask() {
9+
@get:Input
10+
abstract val startTimeMs: Property<Long>
11+
12+
@TaskAction
13+
fun generatesResultFile() {
14+
val endTimeMs = System.currentTimeMillis()
15+
MuzzleReportUtils.generateResultsXML(project, endTimeMs - startTimeMs.get())
16+
}
17+
}

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/tasks/MuzzleTask.kt

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ package datadog.gradle.plugin.muzzle.tasks
22

33
import datadog.gradle.plugin.muzzle.MuzzleAction
44
import datadog.gradle.plugin.muzzle.MuzzleDirective
5+
import datadog.gradle.plugin.muzzle.MuzzleExtension
56
import datadog.gradle.plugin.muzzle.allMainSourceSet
6-
import org.gradle.api.NamedDomainObjectProvider
77
import org.gradle.api.Project
88
import org.gradle.api.artifacts.Configuration
99
import org.gradle.api.file.FileCollection
1010
import org.gradle.api.invocation.BuildInvocationDetails
11+
import org.gradle.api.provider.Property
12+
import org.gradle.api.tasks.Input
13+
import org.gradle.api.tasks.InputFiles
14+
import org.gradle.api.tasks.Optional
15+
import org.gradle.api.tasks.TaskAction
1116
import org.gradle.jvm.toolchain.JavaLanguageVersion
1217
import org.gradle.jvm.toolchain.JavaToolchainService
18+
import org.gradle.kotlin.dsl.getByType
1319
import org.gradle.workers.WorkerExecutor
1420
import javax.inject.Inject
1521

1622
abstract class MuzzleTask : AbstractMuzzleTask() {
23+
override fun getDescription(): String {
24+
return if (muzzleDirective.isPresent) {
25+
"Run instrumentation muzzle on ${muzzleDirective.get().name} dependency"
26+
} else {
27+
"Run instrumentation muzzle on compile time dependencies"
28+
}
29+
}
30+
1731
@get:Inject
1832
abstract val javaToolchainService: JavaToolchainService
1933

@@ -23,12 +37,30 @@ abstract class MuzzleTask : AbstractMuzzleTask() {
2337
@get:Inject
2438
abstract val workerExecutor: WorkerExecutor
2539

26-
fun assertMuzzle(
27-
muzzleBootstrap: NamedDomainObjectProvider<Configuration>,
28-
muzzleTooling: NamedDomainObjectProvider<Configuration>,
29-
instrumentationProject: Project,
30-
muzzleDirective: MuzzleDirective? = null
31-
) {
40+
@get:InputFiles
41+
abstract val muzzleBootstrap: Property<Configuration>
42+
43+
@get:InputFiles
44+
abstract val muzzleTooling: Property<Configuration>
45+
46+
@get:Input
47+
@get:Optional
48+
abstract val muzzleDirective : Property<MuzzleDirective>
49+
50+
@TaskAction
51+
fun muzzle() {
52+
when {
53+
!project.extensions.getByType<MuzzleExtension>().directives.any { it.assertPass } -> {
54+
project.logger.info("No muzzle pass directives configured. Asserting pass against instrumentation compile-time dependencies")
55+
assertMuzzle()
56+
}
57+
muzzleDirective.isPresent -> {
58+
assertMuzzle(muzzleDirective.get())
59+
}
60+
}
61+
}
62+
63+
private fun assertMuzzle(muzzleDirective: MuzzleDirective? = null) {
3264
val workQueue = if (muzzleDirective?.javaVersion != null) {
3365
val javaLauncher = javaToolchainService.launcherFor {
3466
languageVersion.set(JavaLanguageVersion.of(muzzleDirective.javaVersion!!))
@@ -43,10 +75,10 @@ abstract class MuzzleTask : AbstractMuzzleTask() {
4375
}
4476
workQueue.submit(MuzzleAction::class.java) {
4577
buildStartedTime.set(invocationDetails.buildStartedTime)
46-
bootstrapClassPath.setFrom(muzzleBootstrap.get())
47-
toolingClassPath.setFrom(muzzleTooling.get())
48-
instrumentationClassPath.setFrom(createAgentClassPath(instrumentationProject))
49-
testApplicationClassPath.setFrom(createMuzzleClassPath(instrumentationProject, name))
78+
bootstrapClassPath.setFrom(muzzleBootstrap)
79+
toolingClassPath.setFrom(muzzleTooling)
80+
instrumentationClassPath.setFrom(createAgentClassPath(project))
81+
testApplicationClassPath.setFrom(createMuzzleClassPath(project, name))
5082
if (muzzleDirective != null) {
5183
assertPass.set(muzzleDirective.assertPass)
5284
this.muzzleDirective.set(muzzleDirective.name ?: muzzleDirective.module)

0 commit comments

Comments
 (0)