|
| 1 | +package io.sentry.gradle |
| 2 | + |
| 3 | +import groovy.json.JsonSlurper |
| 4 | +import javax.inject.Inject |
| 5 | +import org.gradle.api.Plugin |
| 6 | +import org.gradle.api.Project |
| 7 | +import org.gradle.api.file.FileCollection |
| 8 | +import org.gradle.api.tasks.SourceSetContainer |
| 9 | +import org.gradle.api.tasks.testing.Test |
| 10 | +import org.gradle.language.base.plugins.LifecycleBasePlugin |
| 11 | + |
| 12 | +/** |
| 13 | + * Runs integration suites on each Spring Boot version of a line, without recompiling anything. |
| 14 | + * |
| 15 | + * The suites and the SDK compile once, against the version catalog's Spring Boot version. For each |
| 16 | + * version, a suite's test task reuses its compiled classes on a runtime classpath resolved for that |
| 17 | + * version, which is how users run our published jars on their Spring Boot version. |
| 18 | + */ |
| 19 | +class SpringBootMatrixPlugin : Plugin<Project> { |
| 20 | + override fun apply(project: Project) { |
| 21 | + project.extensions.create("springBootMatrix", SpringBootMatrixExtension::class.java) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +abstract class SpringBootMatrixExtension @Inject constructor(private val project: Project) { |
| 26 | + private var versions = emptyList<String>() |
| 27 | + |
| 28 | + /** |
| 29 | + * Registers `allIntegrationTestsBoot<version>` for each version of Spring Boot [line] in |
| 30 | + * `gradle/spring-boot-versions.json`, and `allIntegrationTestsBoot<line>` for all of them. The CI |
| 31 | + * workflows read the same file and run each version in its own job. A WAR module uses its Boot |
| 32 | + * module's line, because the Boot BOM decides its Spring version. |
| 33 | + */ |
| 34 | + fun versions(line: Int) { |
| 35 | + val file = project.layout.settingsDirectory.file("gradle/spring-boot-versions.json") |
| 36 | + val lines = |
| 37 | + JsonSlurper().parseText(project.providers.fileContents(file).asText.get()) as Map<*, *> |
| 38 | + val versions = (lines["boot$line"] as List<*>).map { it as String } |
| 39 | + this.versions = versions |
| 40 | + project.tasks.register("allIntegrationTestsBoot$line") { |
| 41 | + description = "Runs the integration suites on Spring Boot ${versions.joinToString()}." |
| 42 | + group = LifecycleBasePlugin.VERIFICATION_GROUP |
| 43 | + dependsOn(versions.map { aggregateName(it) }) |
| 44 | + } |
| 45 | + versions.forEach { version -> |
| 46 | + project.tasks.register(aggregateName(version)) { |
| 47 | + description = "Runs the integration suites on Spring Boot $version." |
| 48 | + group = LifecycleBasePlugin.VERIFICATION_GROUP |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Registers `<name>Boot<version>` for each version, running [suite]'s tests. [name] tells apart |
| 55 | + * several tasks of one suite, and [configure] adds what the suite's own task has beyond its |
| 56 | + * classes and classpath. |
| 57 | + */ |
| 58 | + fun test(suite: String, name: String = suite, configure: Test.(version: String) -> Unit = {}) { |
| 59 | + check(versions.isNotEmpty()) { "Call versions() before test()" } |
| 60 | + versions.forEach { version -> |
| 61 | + // Created now rather than with the task, so `dependencies --configuration` finds it. |
| 62 | + val runtimeClasspath = runtimeClasspath(suite, version) |
| 63 | + val task = |
| 64 | + project.tasks.register("$name${suffix(version)}", Test::class.java) { |
| 65 | + description = "Runs $name on Spring Boot $version." |
| 66 | + group = LifecycleBasePlugin.VERIFICATION_GROUP |
| 67 | + testClassesDirs = sourceSet(suite).output.classesDirs |
| 68 | + classpath = runtimeClasspath |
| 69 | + useJUnit() |
| 70 | + configure(version) |
| 71 | + } |
| 72 | + project.tasks.named(aggregateName(version)) { dependsOn(task) } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** [sourceSet]'s output and runtime dependencies, with Spring Boot at [version]. */ |
| 77 | + fun runtimeClasspath(sourceSet: String, version: String): FileCollection { |
| 78 | + val sources = sourceSet(sourceSet) |
| 79 | + val name = "$sourceSet${suffix(version)}RuntimeClasspath" |
| 80 | + val configurations = project.configurations |
| 81 | + val dependencies = |
| 82 | + configurations.findByName(name) |
| 83 | + ?: configurations |
| 84 | + .resolvable(name) { |
| 85 | + extendsFrom( |
| 86 | + configurations.getByName(sources.implementationConfigurationName), |
| 87 | + configurations.getByName(sources.runtimeOnlyConfigurationName), |
| 88 | + ) |
| 89 | + attributes.addAllLater( |
| 90 | + configurations.getByName(sources.runtimeClasspathConfigurationName).attributes |
| 91 | + ) |
| 92 | + // What setting the version in the catalog does: every Spring Boot artifact, the BOM |
| 93 | + // included, is in this group. Libraries pinned to their own version still win |
| 94 | + // conflicts. |
| 95 | + resolutionStrategy.eachDependency { |
| 96 | + if (requested.group == "org.springframework.boot") useVersion(version) |
| 97 | + } |
| 98 | + } |
| 99 | + .get() |
| 100 | + return sources.output + dependencies |
| 101 | + } |
| 102 | + |
| 103 | + private fun sourceSet(name: String) = |
| 104 | + project.extensions.getByType(SourceSetContainer::class.java).getByName(name) |
| 105 | + |
| 106 | + private fun aggregateName(version: String) = "allIntegrationTests${suffix(version)}" |
| 107 | + |
| 108 | + private fun suffix(version: String) = "Boot" + version.replace('.', '_') |
| 109 | +} |
0 commit comments