Skip to content

Commit

Permalink
Create a separate integration repo per project
Browse files Browse the repository at this point in the history
Since the integration repo is technically a build output of the plugin project, it should live under the plugin's project's build directory and not the build directory of the root project
  • Loading branch information
ogolberg authored Jan 16, 2025
1 parent 3827c34 commit 7520c6f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
8 changes: 6 additions & 2 deletions integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ jacoco {
tasks {
test {
systemProperty("version", "$version")
systemProperty("testkit-integration-repo", rootProject.layout.buildDirectory.dir("integration-repo").get().asFile.path)
systemProperty("testkit-integration-repo", layout.buildDirectory.dir("testkit-integration-repo").get().asFile.path)

reports {
junitXml.required = true
}
}
}

configureIntegrationPublishing("testRuntimeClasspath")
publishOnlyIf { _, repo -> repo == RepositoryDescriptor.INTEGRATION }
publishOnlyIf { _, repo -> repo.isIntegration() }

tasks.withType<PublishTask> {
enabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,31 @@ import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
import java.net.URI

sealed interface RepositoryDescriptor {
object MavenLocal : RepositoryDescriptor
data class MavenRemote(val name: String) : RepositoryDescriptor
fun isIntegration(): Boolean

object MavenLocal : RepositoryDescriptor {
override fun isIntegration() = false
}

class MavenRemote(val name: String, val url: URI) : RepositoryDescriptor {
override fun isIntegration() = name.startsWith(INTEGRATION_REPO_NAME_PREFIX)

val capitalizedName by lazy { name.simpleCapitalize() }
}

companion object {
const val INTEGRATION_REPO_NAME = "integration"
val INTEGRATION = MavenRemote(INTEGRATION_REPO_NAME)
const val INTEGRATION_REPO_NAME_PREFIX = "testkitIntegrationFor"
}
}

data class PublicationDescriptor(val name: String) {
fun isPlugin() = name.endsWith("PluginMarkerMaven")

companion object {
const val INTEGRATION_PUBLICATION_NAME = "integration"
const val INTEGRATION_PUBLICATION_NAME = "testkitIntegration"
val INTEGRATION = PublicationDescriptor(INTEGRATION_PUBLICATION_NAME)
}
}
Expand All @@ -59,17 +68,26 @@ fun Project.publishOnlyIf(predicate: (PublicationDescriptor, RepositoryDescripto

project.tasks.withType<PublishToMavenRepository> {
onlyIf {
predicate(PublicationDescriptor(publication.name), RepositoryDescriptor.MavenRemote(repository.name))
predicate(PublicationDescriptor(publication.name), RepositoryDescriptor.MavenRemote(repository.name, repository.url))
}
}
}

val Project.integrationRepo get() = rootProject.layout.buildDirectory.dir("integration-repo").get().asFile.path
fun Project.integrationDirectory() = layout.buildDirectory.dir("testkit-integration-repo").get().asFile

fun Project.configureIntegrationPublishing(
configuration: String = "runtimeClasspath"
) {
val repo = integrationRepo
val repo = RepositoryDescriptor.MavenRemote(
name = RepositoryDescriptor.INTEGRATION_REPO_NAME_PREFIX + path.split(Regex("\\W+")).mapIndexed { i, s ->
if (i == 0) {
s
} else {
s.simpleCapitalize()
}
}.joinToString(separator = ""),
url = integrationDirectory().toURI()
)

afterEvaluate {
val coverage = project.coverage()
Expand All @@ -87,11 +105,11 @@ fun Project.configureIntegrationPublishing(
}

tasks.named("test") {
dependsOn("publishIntegrationPublicationToIntegrationRepository")
dependsOn("publishTestkitIntegrationPublicationTo${repo.capitalizedName}Repository")
}
}

private fun Project.configureIntegrationPublishingForDependency(project: Project, repo: Any, coverage: CoverageConfiguration) {
private fun Project.configureIntegrationPublishingForDependency(project: Project, repo: RepositoryDescriptor.MavenRemote, coverage: CoverageConfiguration) {
project.pluginManager.apply("maven-publish")

if (coverage is CoverageConfiguration.Jacoco) {
Expand All @@ -109,8 +127,8 @@ private fun Project.configureIntegrationPublishingForDependency(project: Project
project.extensions.configure<PublishingExtension>("publishing") {
repositories {
maven {
name = RepositoryDescriptor.INTEGRATION_REPO_NAME
url = project.uri("file://$repo")
name = repo.name
url = repo.url
}
}

Expand Down Expand Up @@ -139,32 +157,34 @@ private fun Project.configureIntegrationPublishingForDependency(project: Project
}

tasks.named("test") {
dependsOn("${project.path}:publishIntegrationPublicationToIntegrationRepository")
dependsOn("${project.path}:publishTestkitIntegrationPublicationTo${repo.capitalizedName}Repository")
}

project.extensions.findByType(
GradlePluginDevelopmentExtension::class.java
)?.plugins?.forEach { plugin ->
val name = "publish" + plugin.name.capitalize() + "PluginMarkerMavenPublicationToIntegrationRepository"
val name = "publish" + plugin.name.simpleCapitalize() + "PluginMarkerMavenPublicationTo${repo.capitalizedName}Repository"

tasks.named("test") {
dependsOn("${project.path}:$name")
}
}

if (coverage is CoverageConfiguration.Jacoco) {
project.tasks.named<GenerateModuleMetadata>("generateMetadataFileForIntegrationPublication") {
project.tasks.named<GenerateModuleMetadata>("generateMetadataFileForTestkitIntegrationPublication") {
enabled = false
}
}

project.publishOnlyIf { publication, repository ->
if (publication == PublicationDescriptor.INTEGRATION) {
repository == RepositoryDescriptor.INTEGRATION
} else if (repository == RepositoryDescriptor.INTEGRATION) {
repository.isIntegration()
} else if (repository.isIntegration()) {
publication.isPlugin()
} else {
true
}
}
}

fun String.simpleCapitalize() = replaceFirstChar(Char::titlecaseChar)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.toasttab.gradle.testkit

import com.toasttab.gradle.testkit.shared.configureIntegrationPublishing
import com.toasttab.gradle.testkit.shared.integrationRepo
import com.toasttab.gradle.testkit.shared.integrationDirectory
import org.apache.tools.ant.filters.ReplaceTokens
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -48,7 +48,7 @@ class TestkitPlugin @Inject constructor(
mapOf(
"tokens" to mapOf(
"TESTKIT_PLUGIN_VERSION" to BuildConfig.VERSION,
"TESTKIT_INTEGRATION_REPO" to project.integrationRepo,
"TESTKIT_INTEGRATION_REPO" to project.integrationDirectory().path,
"VERSION" to "${project.version}"
) + extension.replaceTokens
)
Expand All @@ -69,7 +69,7 @@ class TestkitPlugin @Inject constructor(

systemProperty("testkit-coverage-output", "${destfile.get()}")
systemProperty("testkit-projects", "${testProjectDir.get()}")
systemProperty("testkit-integration-repo", project.integrationRepo)
systemProperty("testkit-integration-repo", project.integrationDirectory().path)
}

project.configureIntegrationPublishing()
Expand Down

0 comments on commit 7520c6f

Please sign in to comment.