|
| 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 | + } |
0 commit comments