Skip to content

Commit f06117a

Browse files
authored
SONARGO-83 Create a settings plugin to set project version (#8)
1 parent a2bbe6c commit f06117a

5 files changed

Lines changed: 176 additions & 2 deletions

File tree

gradle-modules/build.gradle.kts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@
1616
*/
1717
plugins {
1818
`kotlin-dsl`
19+
`java-gradle-plugin`
1920
alias(libs.plugins.spotless)
2021
}
2122

2223
dependencies {
24+
implementation(libs.develocity)
2325
implementation(libs.diffplug.spotless)
24-
implementation(libs.diffplug.blowdryer)
26+
implementation(libs.diffplug.blowdryer.setup)
2527
}
2628

29+
gradlePlugin {
30+
plugins {
31+
create("org.sonarsource.cloud-native.common-settings") {
32+
id = "org.sonarsource.cloud-native.common-settings"
33+
implementationClass = "org.sonarsource.cloudnative.gradle.CommonSettingsPlugin"
34+
}
35+
}
36+
}
37+
38+
val kotlinGradleDelimiter = "(package|import|plugins|pluginManagement|dependencyResolutionManagement|repositories) "
2739
spotless {
2840
kotlin {
2941
ktlint().setEditorConfigPath("$rootDir/.editorconfig")
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonarsource.cloudnative.gradle
18+
19+
import com.gradle.develocity.agent.gradle.DevelocityConfiguration
20+
import javax.inject.Inject
21+
import org.gradle.api.Plugin
22+
import org.gradle.api.artifacts.dsl.RepositoryHandler
23+
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
24+
import org.gradle.api.initialization.Settings
25+
import org.gradle.api.internal.file.FileOperations
26+
import org.gradle.api.provider.ProviderFactory
27+
import org.gradle.internal.extensions.core.extra
28+
import org.gradle.kotlin.dsl.assign
29+
import org.gradle.kotlin.dsl.configure
30+
import org.gradle.kotlin.dsl.getByType
31+
import org.gradle.kotlin.dsl.repositories
32+
33+
open class CommonSettingsPlugin
34+
@Inject
35+
constructor(
36+
private val fileOperations: FileOperations,
37+
) : Plugin<Settings> {
38+
override fun apply(settings: Settings) {
39+
settings.configureRepositories()
40+
41+
settings.applyPlugins()
42+
43+
settings.gradle.allprojects {
44+
// this value is present on CI
45+
val buildNumber: String? = System.getProperty("buildNumber")
46+
val version = properties["version"] as String
47+
if (version.endsWith("-SNAPSHOT") && buildNumber != null) {
48+
val versionSuffix = if (version.count { it == '.' } == 1) ".0.$buildNumber" else ".$buildNumber"
49+
project.version = version.replace("-SNAPSHOT", versionSuffix).also {
50+
logger.lifecycle("Project ${project.name} version set to $it")
51+
}
52+
}
53+
}
54+
55+
settings.configureDevelocity()
56+
}
57+
58+
private fun Settings.configureRepositories() {
59+
pluginManagement {
60+
repositories {
61+
mavenCentral()
62+
gradlePluginPortal()
63+
repox(settings.providers, fileOperations)
64+
}
65+
}
66+
67+
dependencyResolutionManagement {
68+
repositories {
69+
mavenCentral()
70+
repox(settings.providers, fileOperations)
71+
}
72+
}
73+
74+
buildscript.repositories {
75+
gradlePluginPortal()
76+
}
77+
}
78+
79+
private fun Settings.applyPlugins() {
80+
// Blowdryer is needed to provide immutable URLs for Spotless (e.g. the Eclipse formatter config)
81+
settings.pluginManager.apply("com.diffplug.blowdryerSetup")
82+
83+
settings.pluginManager.apply("com.gradle.develocity")
84+
}
85+
86+
private fun RepositoryHandler.repox(
87+
providers: ProviderFactory,
88+
fileOperations: FileOperations,
89+
): MavenArtifactRepository =
90+
maven {
91+
name = "artifactory"
92+
url = fileOperations.uri("https://repox.jfrog.io/repox/sonarsource")
93+
94+
// This authentication relies on env vars configured on Cirrus CI or on Gradle properties (`-P<prop>` flags or `gradle.properties` file)
95+
val artifactoryUsername = providers.environmentVariable("ARTIFACTORY_PRIVATE_USERNAME")
96+
.orElse(providers.gradleProperty("artifactoryUsername"))
97+
val artifactoryPassword = providers.environmentVariable("ARTIFACTORY_PRIVATE_PASSWORD")
98+
.orElse(providers.gradleProperty("artifactoryPassword"))
99+
100+
if (artifactoryUsername.isPresent && artifactoryPassword.isPresent) {
101+
authentication {
102+
credentials {
103+
username = artifactoryUsername.get()
104+
password = artifactoryPassword.get()
105+
}
106+
}
107+
}
108+
}
109+
110+
private fun Settings.configureDevelocity() {
111+
val develocity = extensions.getByType<DevelocityConfiguration>()
112+
113+
extensions.configure<DevelocityConfiguration> {
114+
server = "https://develocity.sonar.build"
115+
buildScan {
116+
tag(if (System.getenv("CI").isNullOrEmpty()) "local" else "CI")
117+
tag(System.getProperty("os.name"))
118+
if (System.getenv("CIRRUS_BRANCH") == "master") {
119+
tag("master")
120+
}
121+
if (System.getenv("CIRRUS_PR")?.isBlank() == false) {
122+
tag("PR")
123+
}
124+
value("Build Number", System.getenv("BUILD_NUMBER"))
125+
value("Branch", System.getenv("CIRRUS_BRANCH"))
126+
value("PR", System.getenv("CIRRUS_PR"))
127+
value("PR Title", System.getenv("CIRRUS_PR_TITLE"))
128+
}
129+
}
130+
131+
val isCI = System.getenv("CI") != null
132+
buildCache {
133+
local {
134+
isEnabled = !isCI
135+
}
136+
remote(develocity.buildCache) {
137+
isEnabled = true
138+
isPush = isCI
139+
}
140+
}
141+
}
142+
}

gradle-modules/src/main/kotlin/org/sonarsource/cloudnative/gradle/IntegrationTestExtension.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
117
package org.sonarsource.cloudnative.gradle
218

319
import groovy.lang.Closure

gradle/libs.versions.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[versions]
22
spotless-gradle = "6.25.0"
33
blowdryer-gradle = "1.7.1"
4+
develocity = "3.18.2"
45

56
[libraries]
67
diffplug-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless-gradle" }
7-
diffplug-blowdryer = { module = "com.diffplug:blowdryer", version.ref = "blowdryer-gradle" }
8+
diffplug-blowdryer-setup = { module = "com.diffplug.blowdryerSetup:com.diffplug.blowdryerSetup.gradle.plugin", version.ref = "blowdryer-gradle" }
9+
develocity = { module = "com.gradle.develocity:com.gradle.develocity.gradle.plugin", version.ref = "develocity" }
810

911
[plugins]
1012
spotless = { id = "com.diffplug.spotless", version.ref = "spotless-gradle" }
13+
develocity = { id = "com.gradle.develocity", version.ref = "develocity" }

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ include("gradle-modules")
3131
dependencyResolutionManagement {
3232
repositories {
3333
mavenCentral()
34+
gradlePluginPortal()
3435
}
3536
}
3637

0 commit comments

Comments
 (0)