-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
build.gradle.kts
348 lines (287 loc) · 10.2 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2024. Tony Robalik.
// SPDX-License-Identifier: Apache-2.0
@file:Suppress("UnstableApiUsage", "HasPlatformType", "PropertyName")
plugins {
id("java-gradle-plugin")
id("com.gradle.plugin-publish")
`kotlin-dsl`
id("groovy")
id("convention")
alias(libs.plugins.dependencyAnalysis)
id("com.autonomousapps.testkit")
}
// This version string comes from gradle.properties
val VERSION: String by project
version = VERSION
val isSnapshot: Boolean = project.version.toString().endsWith("SNAPSHOT")
val isRelease: Boolean = !isSnapshot
dagp {
version(version)
pom {
name.set("Dependency Analysis Gradle Plugin")
description.set("Analyzes dependency usage in Android and JVM projects")
inceptionYear.set("2019")
}
publishTaskDescription(
"Publishes plugin marker and plugin artifacts to Maven Central " +
"(${if (version.toString().endsWith("SNAPSHOT")) "snapshots" else "staging"})"
)
}
// For publishing to the Gradle Plugin Portal
// https://plugins.gradle.org/docs/publish-plugin
gradlePlugin {
plugins {
create("dependencyAnalysisPlugin") {
id = "com.autonomousapps.dependency-analysis"
implementationClass = "com.autonomousapps.DependencyAnalysisPlugin"
displayName = "Dependency Analysis Gradle Plugin"
description = "A plugin to report mis-used dependencies in your JVM or Android project"
tags.set(listOf("java", "kotlin", "groovy", "scala", "android", "dependencies"))
}
create("buildHealthPlugin") {
id = "com.autonomousapps.build-health"
implementationClass = "com.autonomousapps.BuildHealthPlugin"
displayName = "Build Health Gradle Plugin"
description = "A plugin to report on the health of your JVM or Android build"
tags.set(listOf("java", "kotlin", "groovy", "scala", "android", "dependencies"))
}
}
website.set("https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin")
vcsUrl.set("https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin")
}
val main = sourceSets["main"]
val commonTest = sourceSets.create("commonTest") {
java {
srcDir("src/commonTest/kotlin")
}
}
sourceSets {
test {
compileClasspath += commonTest.output
runtimeClasspath += output + compileClasspath
}
}
// Add a source set for the functional test suite. This must come _above_ the `dependencies` block.
val functionalTestSourceSet = sourceSets.maybeCreate("functionalTest").apply {
compileClasspath += main.output + configurations["testRuntimeClasspath"] + commonTest.output
runtimeClasspath += output + compileClasspath
}
val functionalTestImplementation = configurations
.getByName("functionalTestImplementation")
.extendsFrom(configurations.getByName("testImplementation"))
val functionalTestApi = configurations.getByName("functionalTestApi")
val compileFunctionalTestKotlin = tasks.named("compileFunctionalTestKotlin")
tasks.named<AbstractCompile>("compileFunctionalTestGroovy") {
dependsOn(compileFunctionalTestKotlin)
classpath += files(compileFunctionalTestKotlin.get().outputs.files)
}
// Add a source set for the smoke test suite. This must come _above_ the `dependencies` block.
val smokeTestSourceSet = sourceSets.create("smokeTest") {
compileClasspath += main.output + configurations["testRuntimeClasspath"]
runtimeClasspath += output + compileClasspath
}
val smokeTestImplementation = configurations
.getByName("smokeTestImplementation")
.extendsFrom(functionalTestImplementation)
gradleTestKitSupport {
withSupportLibrary()
withTruthLibrary()
}
dependencies {
implementation(platform(libs.kotlin.bom))
implementation(platform(libs.okio.bom))
api(libs.javax.inject)
api(libs.moshi.core)
api(libs.moshix.sealed.runtime)
implementation(project(":graph-support"))
implementation(libs.guava)
implementation(libs.kotlin.stdlib.jdk8)
implementation(libs.kotlin.editor.relocated)
implementation(libs.moshi.kotlin)
implementation(libs.moshix.sealed.reflect)
implementation(libs.okio)
implementation(libs.kotlin.metadata.jvm) {
// Depends on Kotlin 2.x, which I don't want. This is fragile, though. Will eventually have to update to Kotlin 2.
isTransitive = false
}
implementation(libs.caffeine) {
because("High performance, concurrent cache")
}
implementation(libs.relocated.antlr)
implementation(libs.relocated.asm)
runtimeOnly(libs.kotlin.reflect) {
because("For Kotlin ABI analysis")
}
compileOnly(libs.agp) {
because("Auto-wiring into Android projects")
}
compileOnly(libs.android.tools.common) {
because("com.android.Version")
}
compileOnly(libs.kotlin.gradle) {
because("Auto-wiring into Kotlin projects")
}
testImplementation(platform(libs.junit.bom))
testImplementation(libs.spock) {
exclude(group = "org.codehaus.groovy")
because("For Spock tests")
}
testImplementation(libs.junit.api)
testImplementation(libs.junit.params)
testImplementation(libs.truth)
testRuntimeOnly(libs.junit.engine)
smokeTestImplementation(libs.commons.io) {
because("For FileUtils.deleteDirectory()")
}
}
fun shadowed(): Action<ExternalModuleDependency> = Action {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
}
}
// additive (vs testSourceSets() which _sets_)
gradlePlugin.testSourceSet(smokeTestSourceSet)
// CI cannot handle too much parallelization. Runs out of metaspace.
fun maxParallelForks() =
if (isCi) 1
else Runtime.getRuntime().availableProcessors() / 2
val isCi = providers.environmentVariable("CI")
.getOrElse("false")
.toBoolean()
// This will slow down tests on CI, but maybe it won't run out of metaspace.
fun forkEvery(): Long = if (isCi) 40 else 0
// Add a task to run the functional tests
// quickTest only runs against the latest gradle version. For iterating faster
fun quickTest(): Boolean = providers
.systemProperty("funcTest.quick")
.orNull != null
val functionalTest = tasks.named("functionalTest", Test::class) {
// forking JVMs is very expensive, and only necessary with full test runs
forkEvery = forkEvery()
maxParallelForks = maxParallelForks()
jvmArgs("-XX:+HeapDumpOnOutOfMemoryError", "-XX:MaxMetaspaceSize=1g")
systemProperty("com.autonomousapps.quick", "${quickTest()}")
systemProperty("com.autonomousapps.test.versions.kotlin", libs.versions.kotlin.get())
beforeTest(closureOf<TestDescriptor> {
logger.lifecycle("Running test: $this")
})
// ./gradlew :functionalTest -DfuncTest.package=<all|jvm|android>
val testKindLog = when (providers.systemProperty("funcTest.package").getOrElse("all").lowercase()) {
"jvm" -> {
include("com/autonomousapps/jvm/**")
"Run JVM tests"
}
"android" -> {
include("com/autonomousapps/android/**")
// Android requires JDK 17 from AGP 8.0.
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(17))
})
"Run Android tests"
}
else -> {
// Android requires JDK 17 from AGP 8.0.
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(17))
})
"Run all tests"
}
}
doFirst {
logger.quiet(">>> $testKindLog (use '-DfuncTest.package=<android|jvm|all>' to change filter)")
}
}
val quickFunctionalTest by tasks.registering {
dependsOn(functionalTest)
System.setProperty("funcTest.quick", "true")
}
val smokeTestVersionKey = "com.autonomousapps.version"
val smokeTestVersion: String = System.getProperty(smokeTestVersionKey, latestRelease())
val smokeTest by tasks.registering(Test::class) {
mustRunAfter(tasks.named("test"), functionalTest)
description = "Runs the smoke tests."
group = "verification"
testClassesDirs = smokeTestSourceSet.output.classesDirs
classpath = smokeTestSourceSet.runtimeClasspath
jvmArgs("-XX:+HeapDumpOnOutOfMemoryError", "-XX:MaxMetaspaceSize=1g")
systemProperty(smokeTestVersionKey, smokeTestVersion)
beforeTest(closureOf<TestDescriptor> {
logger.lifecycle("Running test: $this")
})
}
/**
* Algorithm:
* 1. If version is !SNAPSHOT, latestRelease is identical, but with -SNAPSHOT suffix.
* 2. If version is SNAPSHOT, latestRelease is non-SNAPSHOT, and patch should be -1 by comparison.
*/
fun latestRelease(): String {
val v = version.toString()
return if (isRelease) {
"$v-SNAPSHOT"
} else {
val regex = """(\d+).(\d+).(\d+)(?:-rc\d{2})?-SNAPSHOT""".toRegex()
val groups = regex.find(v)!!.groupValues
val major = groups[1].toInt()
val minor = groups[2].toInt()
val patch = groups[3].toInt()
"$major.$minor.${patch - 1}"
}
}
val check = tasks.named("check")
val publishToMavenCentral = tasks.named("publishToMavenCentral") {
// Note that publishing a release requires a successful smokeTest
if (isRelease) {
dependsOn(check, smokeTest)
}
}
val publishToPluginPortal = tasks.named("publishPlugins") {
// Can't publish snapshots to the portal
onlyIf { isRelease }
shouldRunAfter(publishToMavenCentral)
// Note that publishing a release requires a successful smokeTest
if (isRelease) {
dependsOn(check, smokeTest)
}
}
tasks.register("publishEverywhere") {
dependsOn(publishToMavenCentral, publishToPluginPortal)
group = "publishing"
description = "Publishes to Plugin Portal and Maven Central"
}
tasks.withType<GroovyCompile>().configureEach {
options.isIncremental = true
}
dependencyAnalysis {
structure {
bundle("agp") {
primary("com.android.tools.build:gradle")
includeGroup("com.android.tools")
includeGroup("com.android.tools.build")
}
bundle("kgp") {
includeDependency("org.jetbrains.kotlin:kotlin-gradle-plugin")
includeDependency("org.jetbrains.kotlin:kotlin-gradle-plugin-api")
}
bundle("truth") {
includeDependency(libs.truth)
// Truth's Subject class makes use of the @Nullable annotation from this library when creating `Factory`s. It ends
// up in the bytecode, but I don't really have any control over that.
includeDependency("org.checkerframework:checker-qual")
}
}
abi {
exclusions {
excludeSourceSets(
// These source sets have an "...Api" configuration, but have no ABI, semantically. Exclude them.
"functionalTest", "smokeTest"
)
}
}
issues {
all {
onAny {
severity("fail")
}
}
}
}