Skip to content

Commit ae741a5

Browse files
committed
Configure publishing via settings DSL instead of gradle properties
1 parent c71ac37 commit ae741a5

File tree

9 files changed

+236
-99
lines changed

9 files changed

+236
-99
lines changed

gradle-plugin/src/main/kotlin/xyz/block/artifactswap/ArtifactSwapBomService.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package xyz.block.artifactswap
22

3-
import xyz.block.gradle.services.SharedServiceKey
4-
import xyz.block.gradle.services.SharedServices
5-
import xyz.block.artifactswap.ArtifactSwapBomService.Parameters
63
import groovy.xml.XmlSlurper
74
import groovy.xml.slurpersupport.GPathResult
5+
import java.nio.file.Path
6+
import kotlin.io.path.Path
7+
import kotlin.io.path.exists
8+
import kotlin.io.path.inputStream
89
import org.gradle.api.logging.Logger
910
import org.gradle.api.logging.Logging
1011
import org.gradle.api.provider.Property
1112
import org.gradle.api.services.BuildService
1213
import org.gradle.api.services.BuildServiceParameters
13-
import java.nio.file.Path
14-
import kotlin.io.path.Path
15-
import kotlin.io.path.exists
16-
import kotlin.io.path.inputStream
17-
14+
import xyz.block.artifactswap.ArtifactSwapBomService.Parameters
15+
import xyz.block.gradle.services.SharedServiceKey
16+
import xyz.block.gradle.services.SharedServices
1817

1918
// Service to parse local BOM file once per sync
2019
abstract class ArtifactSwapBomService : BuildService<Parameters> {
@@ -44,13 +43,15 @@ abstract class ArtifactSwapBomService : BuildService<Parameters> {
4443
val dependencyManagement = pom.getProperty("dependencyManagement") as GPathResult
4544
val dependencies = dependencyManagement.getProperty("dependencies") as GPathResult
4645
val dependencySequence = dependencies.children().asSequence().filterIsInstance<GPathResult>()
47-
dependencySequence
48-
.associate { it.getProperty("artifactId").toString() to it.getProperty("version").toString() }
46+
dependencySequence.associate {
47+
it.getProperty("artifactId").toString() to it.getProperty("version").toString()
48+
}
4949
} else {
5050
logger.error("Artifact sync bom does not exist: {}", bomFile)
5151
emptyMap()
5252
}
5353
}
5454
}
5555

56-
internal val SharedServices.artifactSyncBomService get() = get(ArtifactSwapBomService.KEY)
56+
internal val SharedServices.artifactSyncBomService
57+
get() = get(ArtifactSwapBomService.KEY)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package xyz.block.artifactswap
2+
3+
import org.gradle.api.file.RegularFileProperty
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.services.BuildService
6+
import org.gradle.api.services.BuildServiceParameters
7+
import xyz.block.gradle.services.SharedServiceKey
8+
import xyz.block.gradle.services.SharedServices
9+
import java.io.File
10+
11+
/**
12+
* Build service that holds artifact swap publishing configuration. This allows the configuration to
13+
* be set at settings time and accessed in projects in a configuration-cache compatible way.
14+
*/
15+
abstract class ArtifactSwapConfigService : BuildService<ArtifactSwapConfigService.Params> {
16+
17+
interface Params : BuildServiceParameters {
18+
val artifactHashFile: RegularFileProperty
19+
val repoUrl: Property<String>
20+
val repoUsername: Property<String>
21+
val repoPassword: Property<String>
22+
}
23+
24+
internal object KEY : SharedServiceKey<ArtifactSwapConfigService, Params>("artifactSwapConfig")
25+
}
26+
27+
internal val SharedServices.artifactSwapConfigService
28+
get() = get(ArtifactSwapConfigService.KEY)

gradle-plugin/src/main/kotlin/xyz/block/artifactswap/ArtifactSwapProjectPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
package xyz.block.artifactswap
44

5-
import xyz.block.gradle.generatedProtosVersion
6-
import xyz.block.gradle.protosSchemaVersion
7-
import xyz.block.gradle.services.services
85
import org.gradle.api.Plugin
96
import org.gradle.api.Project
107
import org.gradle.api.artifacts.DependencySubstitution
118
import org.gradle.api.artifacts.component.ModuleComponentSelector
129
import org.jetbrains.kotlin.util.prefixIfNot
10+
import xyz.block.gradle.generatedProtosVersion
11+
import xyz.block.gradle.protosSchemaVersion
12+
import xyz.block.gradle.services.services
1313

1414
/**
1515
* Artifact Sync project sub-plugin. This plugin is responsible for performing dependency substitution

gradle-plugin/src/main/kotlin/xyz/block/artifactswap/ArtifactSwapProjectPublishPlugin.kt

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,37 @@ import org.gradle.api.publish.maven.MavenPom
1313
import org.gradle.api.publish.maven.MavenPublication
1414
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
1515
import org.gradle.api.tasks.bundling.Jar
16+
import xyz.block.gradle.artifactVersion
1617
import xyz.block.gradle.isAndroid
1718
import xyz.block.gradle.isKotlin
18-
import xyz.block.gradle.sandbagVersion
19-
import xyz.block.gradle.toSandbagArtifact
19+
import xyz.block.gradle.services.services
20+
import xyz.block.gradle.toProjectArtifactName
2021

2122
/**
22-
* Artifact Swap project publish plugin for sandbags. This plugin is responsible for configuring
23-
* Maven publishing with sandbag-specific settings when sandbag publishing is enabled.
24-
*
25-
* This plugin extracts the sandbag publishing logic from PublishPlugin and AndroidLibJavaPlugin
26-
* to centralize artifact swap publishing concerns.
23+
* Artifact Swap project publish plugin for project artifacts. This plugin is responsible for
24+
* configuring Maven publishing with artifact-swap-specific settings when artifact publishing is
25+
* enabled.
2726
*
2827
* For reference and searchability, the ID of this plugin is `xyz.block.artifactswap.publish`.
2928
*/
3029
@Suppress("unused")
3130
class ArtifactSwapProjectPublishPlugin : Plugin<Project> {
3231

32+
private lateinit var configService: ArtifactSwapConfigService
33+
3334
override fun apply(target: Project): Unit = target.run {
34-
val version = sandbagVersion ?: return@run
35+
val version = artifactVersion ?: return@run
36+
37+
configService = gradle.services.artifactSwapConfigService
3538

3639
pluginManager.apply("maven-publish")
3740
extensions.getByType(PublishingExtension::class.java).also { mavenPublishing ->
38-
val repo = configureSandbagRepository(mavenPublishing)
41+
val repo = configureArtifactRepository(mavenPublishing)
3942

40-
// Other plugins configure the components to be published, so we have to configure them after
41-
// those plugins run
43+
// Other plugins configure the components to be published, so we have to configure them
44+
// after those plugins run
4245
afterEvaluate {
43-
val publication = configureSandbagPublication(mavenPublishing, version)
46+
val publication = configureArtifactPublication(mavenPublishing, version)
4447
createPublishAliasTask(repo, publication)
4548
}
4649
}
@@ -50,33 +53,35 @@ class ArtifactSwapProjectPublishPlugin : Plugin<Project> {
5053
}
5154
}
5255

53-
private fun Project.configureSandbagRepository(
56+
private fun Project.configureArtifactRepository(
5457
mavenPublishing: PublishingExtension,
55-
): MavenArtifactRepository = with(mavenPublishing) {
56-
val sandbagsUrl = providers.gradleProperty("square.sandbagsUrl").get()
57-
return repositories.maven { repo ->
58-
repo.name = "artifactSwap"
59-
repo.url = uri(sandbagsUrl)
60-
61-
getSandbagCredentials()?.apply {
62-
repo.credentials(PasswordCredentials::class.java) { creds ->
63-
creds.username = username
64-
creds.password = password
58+
): MavenArtifactRepository =
59+
with(mavenPublishing) {
60+
val repoUrl = configService.parameters.repoUrl.get()
61+
return repositories.maven { repo ->
62+
repo.name = "artifactSwap"
63+
repo.url = uri(repoUrl)
64+
65+
val username = configService.parameters.repoUsername.orNull
66+
val password = configService.parameters.repoPassword.orNull
67+
if (username != null && password != null) {
68+
repo.credentials(PasswordCredentials::class.java) { creds ->
69+
creds.username = username
70+
creds.password = password
71+
}
6572
}
6673
}
6774
}
68-
}
6975

70-
private fun Project.configureSandbagPublication(
76+
private fun Project.configureArtifactPublication(
7177
mavenPublishing: PublishingExtension,
7278
version: String
7379
): MavenPublication {
74-
val publication = mavenPublishing.publications
75-
.maybeCreate("projectArtifact", MavenPublication::class.java)
80+
val publication = mavenPublishing.publications.maybeCreate("projectArtifact", MavenPublication::class.java)
7681

77-
// Automatically configure maven coordinates for sandbag
82+
// Automatically configure maven coordinates for artifact
7883
publication.groupId = ARTIFACT_SWAP_MAVEN_GROUP
79-
publication.artifactId = path.toSandbagArtifact
84+
publication.artifactId = path.toProjectArtifactName
8085
publication.version = version
8186

8287
// For non-Android projects, automatically configure the java component and sources
@@ -85,7 +90,7 @@ class ArtifactSwapProjectPublishPlugin : Plugin<Project> {
8590
addSourcesArtifact(publication)
8691
}
8792

88-
configureSandbagPom(publication.pom)
93+
configureArtifactPom(publication.pom)
8994

9095
return publication
9196
}
@@ -113,26 +118,11 @@ class ArtifactSwapProjectPublishPlugin : Plugin<Project> {
113118
}
114119
}
115120

116-
private fun Project.configureSandbagPom(pom: MavenPom) {
121+
private fun Project.configureArtifactPom(pom: MavenPom) {
117122
with(pom) {
118123
name.set(project.name)
119-
description.set("Sandbag for ${project.name} in build ${project.isolated.rootProject.name}")
120-
url.set(providers.gradleProperty("square.repoUrl"))
121-
scm { scm ->
122-
scm.connection.set(providers.gradleProperty("square.scmConnectionUrl"))
123-
scm.developerConnection.set(providers.gradleProperty("square.scmDeveloperConnectionUrl"))
124-
scm.url.set(providers.gradleProperty("square.repoUrl"))
125-
}
126-
}
127-
}
128-
129-
private fun Project.getSandbagCredentials(): SandbagCredentials? {
130-
val username = providers.gradleProperty("square.artifactory.username").orNull
131-
val password = providers.gradleProperty("square.artifactory.password").orNull
132-
return if (username != null && password != null) {
133-
SandbagCredentials(username, password)
134-
} else {
135-
null
124+
description.set("Artifact for ${project.name} in build ${project.isolated.rootProject.name}")
125+
url.set(configService.parameters.repoUrl)
136126
}
137127
}
138128

@@ -145,9 +135,4 @@ class ArtifactSwapProjectPublishPlugin : Plugin<Project> {
145135
it.dependsOn(tasks.named(publishTaskName))
146136
}
147137
}
148-
149-
private data class SandbagCredentials(
150-
val username: String,
151-
val password: String
152-
)
153138
}

gradle-plugin/src/main/kotlin/xyz/block/artifactswap/ArtifactSwapSettingsPlugin.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
package xyz.block.artifactswap
44

5+
import java.io.File
56
import org.gradle.api.Plugin
67
import org.gradle.api.initialization.Settings
78
import org.gradle.api.initialization.resolve.DependencyResolutionManagement
89
import org.gradle.api.logging.Logger
910
import org.gradle.api.logging.Logging
11+
import xyz.block.artifactswap.dsl.ArtifactSwapExtension
1012
import xyz.block.gradle.LOCAL_PROTOS_ARTIFACTS
1113
import xyz.block.gradle.bomVersion
12-
import xyz.block.gradle.isArtifactPublishingEnabled
1314
import xyz.block.gradle.services.services
1415
import xyz.block.gradle.useArtifactSync
1516
import xyz.block.gradle.useLocalProtos
1617
import xyz.block.ide.forceSettingsModulesOverride
1718
import xyz.block.ide.isIdeSync
18-
import java.io.File
1919

2020
/**
2121
* Main Artifact Sync settings plugin. This plugin is responsible for:
@@ -31,7 +31,11 @@ import java.io.File
3131
*/
3232
@Suppress("unused")
3333
class ArtifactSwapSettingsPlugin : Plugin<Settings> {
34+
private lateinit var extension: ArtifactSwapExtension
35+
3436
override fun apply(target: Settings) = target.run {
37+
extension = ArtifactSwapExtension.of(target)
38+
3539
applyProjectIncludes()
3640
maybeApplyArtifactSync()
3741
maybeApplyPublishPlugin()
@@ -130,13 +134,20 @@ class ArtifactSwapSettingsPlugin : Plugin<Settings> {
130134
}
131135
}
132136

133-
/**
134-
* Applies the publish plugin to all projects when sandbag publishing is enabled.
135-
*/
137+
/** Applies the publish plugin to all projects when artifact publishing is enabled. */
136138
private fun Settings.maybeApplyPublishPlugin() {
137-
if (isArtifactPublishingEnabled) {
138-
gradle.lifecycle.beforeProject { project ->
139-
project.plugins.apply(ArtifactSwapProjectPublishPlugin::class.java)
139+
gradle.settingsEvaluated {
140+
if (extension.publishing.enabled.get()) {
141+
gradle.services.register(ArtifactSwapConfigService.KEY) { spec ->
142+
spec.parameters.artifactHashFile.set(extension.publishing.artifactHashFile)
143+
spec.parameters.repoUrl.set(extension.publishing.repo.url)
144+
spec.parameters.repoUsername.set(extension.publishing.repo.username)
145+
spec.parameters.repoPassword.set(extension.publishing.repo.password)
146+
}
147+
148+
gradle.lifecycle.beforeProject { project ->
149+
project.plugins.apply(ArtifactSwapProjectPublishPlugin::class.java)
150+
}
140151
}
141152
}
142153
}

0 commit comments

Comments
 (0)