Skip to content

Commit 46cb8f9

Browse files
feat: gradle plugin markers are the preferred way to depend on a plugin.
1 parent b6dd315 commit 46cb8f9

3 files changed

Lines changed: 62 additions & 14 deletions

File tree

src/functionalTest/groovy/com/autonomousapps/jvm/BuildLogicSpec.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import static com.google.common.truth.Truth.assertThat
99

1010
final class BuildLogicSpec extends AbstractJvmSpec {
1111

12-
def "gradle plugin markers are used when the plugin is used (#gradleVersion)"() {
12+
// This ensures that plugin dependencies preferable reference the marker artifact.
13+
def "gradle plugin markers are used when the plugin is used (#gradleVersion isDirect=#isDirect)"() {
1314
given:
14-
def project = new BuildLogicProject()
15+
def project = new BuildLogicProject(isDirect)
1516
gradleProject = project.gradleProject
1617
1718
when:
1819
build(gradleVersion, gradleProject.rootDir, 'buildHealth')
1920
2021
then:
21-
assertThat(project.actualBuildHealth()).containsExactlyElementsIn(project.expectedBuildHealth)
22+
assertThat(project.actualBuildHealth()).containsExactlyElementsIn(project.expectedBuildHealth())
2223
2324
where:
24-
gradleVersion << gradleVersions()
25+
[gradleVersion, isDirect] << multivariableDataPipe(gradleVersions(), [true, false])
2526
}
2627
}

src/functionalTest/groovy/com/autonomousapps/jvm/projects/BuildLogicProject.groovy

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@ package com.autonomousapps.jvm.projects
55
import com.autonomousapps.AbstractProject
66
import com.autonomousapps.kit.GradleProject
77
import com.autonomousapps.kit.Source
8-
import com.autonomousapps.kit.SourceType
98
import com.autonomousapps.kit.gradle.Dependency
109
import com.autonomousapps.kit.gradle.Plugin
1110
import com.autonomousapps.kit.gradle.dependencies.Plugins
11+
import com.autonomousapps.model.Advice
1212
import com.autonomousapps.model.ProjectAdvice
1313

14-
import static com.autonomousapps.AdviceHelper.actualProjectAdvice
15-
import static com.autonomousapps.AdviceHelper.emptyProjectAdviceFor
14+
import static com.autonomousapps.AdviceHelper.*
15+
import static com.autonomousapps.kit.gradle.Dependency.api
1616
import static com.autonomousapps.kit.gradle.Dependency.implementation
1717

1818
final class BuildLogicProject extends AbstractProject {
1919

20-
private static final Dependency DAGP = implementation('com.autonomousapps.dependency-analysis:com.autonomousapps.dependency-analysis.gradle.plugin:3.9.0')
20+
private static final Dependency DAGP = api('com.autonomousapps.dependency-analysis:com.autonomousapps.dependency-analysis.gradle.plugin:3.9.0')
21+
private static final String PROVIDES_DAGP = ':provides-dagp'
2122

23+
private final boolean isDirect
2224
final GradleProject gradleProject
2325

24-
BuildLogicProject() {
26+
BuildLogicProject(boolean isDirect) {
27+
this.isDirect = isDirect
2528
this.gradleProject = build()
2629
}
2730

@@ -31,20 +34,34 @@ final class BuildLogicProject extends AbstractProject {
3134
s.sources = sources
3235
s.withBuildScript { bs ->
3336
bs.plugins(Plugin.javaGradle, Plugins.kotlinJvmNoVersion, Plugins.dependencyAnalysisNoVersion)
37+
bs.dependencies(dependencies())
38+
}
39+
}
40+
.withSubproject(PROVIDES_DAGP) { s ->
41+
s.withBuildScript { bs ->
42+
bs.plugins(Plugin.javaLibrary)
3443
bs.dependencies(DAGP)
3544
}
3645
}
3746
.write()
3847
}
3948

49+
private Dependency dependencies() {
50+
if (isDirect) {
51+
DAGP
52+
} else {
53+
implementation(PROVIDES_DAGP)
54+
}
55+
}
56+
4057
private List<Source> sources = [
4158
Source.kotlin(
4259
'''\
4360
package mutual.aid
4461
4562
import com.autonomousapps.DependencyAnalysisSubExtension
4663
47-
class DagpConfigurer(private val dependencyAnalysis: DependencyAnalysisSubExtension)
64+
class DagpConfigurer(val dependencyAnalysis: DependencyAnalysisSubExtension)
4865
'''.stripIndent()
4966
).build()
5067
]
@@ -53,7 +70,20 @@ final class BuildLogicProject extends AbstractProject {
5370
return actualProjectAdvice(gradleProject)
5471
}
5572

56-
final Set<ProjectAdvice> expectedBuildHealth = [
57-
emptyProjectAdviceFor(':conventions'),
58-
]
73+
private Set<Advice> conventionsAdvice() {
74+
if (isDirect) {
75+
[]
76+
} else {
77+
[
78+
Advice.ofAdd(moduleCoordinates(DAGP), 'api'),
79+
Advice.ofRemove(projectCoordinates(PROVIDES_DAGP), 'implementation')
80+
]
81+
}
82+
}
83+
84+
final Set<ProjectAdvice> expectedBuildHealth() {
85+
[
86+
projectAdviceForDependencies(':conventions', conventionsAdvice()),
87+
]
88+
}
5989
}

src/main/kotlin/com/autonomousapps/internal/Bundles.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ internal class Bundles private constructor(
191191
}
192192

193193
companion object {
194+
private const val GRADLE_PLUGIN_MARKER_SUFFIX = ".gradle.plugin"
195+
194196
fun of(
195197
projectPath: String,
196198
dependencyGraph: Map<String, DependencyGraphView>,
@@ -249,7 +251,7 @@ internal class Bundles private constructor(
249251
}
250252

251253
// handle Gradle plugin marker artifacts
252-
if (parentNode.identifier.endsWith(".gradle.plugin")) {
254+
if (parentNode.identifier.endsWith(GRADLE_PLUGIN_MARKER_SUFFIX)) {
253255
view.graph.children(parentNode)
254256
.singleOrNull()
255257
?.let { child -> bundles[parentNode] = child }
@@ -267,6 +269,21 @@ internal class Bundles private constructor(
267269
implicitKmpBundleFor("android")
268270
}
269271

272+
// handle Gradle plugin marker artifacts buried in the graph
273+
view.graph.nodes()
274+
.filter { node -> node.identifier.endsWith(GRADLE_PLUGIN_MARKER_SUFFIX) }
275+
.mapNotNull { markerArtifact ->
276+
val plugin = view.graph.children(markerArtifact).singleOrNull()
277+
if (plugin != null) {
278+
markerArtifact to plugin
279+
} else {
280+
null
281+
}
282+
}
283+
.forEach { (markerArtifact, plugin) ->
284+
bundles.setPrimary(markerArtifact, plugin)
285+
}
286+
270287
fun implicitKmpPrimaryFor(target: String) {
271288
val suffix = "-$target"
272289
view.graph.nodes()

0 commit comments

Comments
 (0)