Skip to content

Commit

Permalink
Allow configuring Gradle Enterprise plugin to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jul 15, 2024
1 parent cb2cf8a commit 167bfbc
Showing 1 changed file with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ package org.metaborg.convention
import com.gradle.develocity.agent.gradle.DevelocityConfiguration
import com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension
import org.gradle.api.Plugin
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.initialization.Settings
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.maven
import org.slf4j.Logger
import org.slf4j.LoggerFactory

private val LOG: Logger = LoggerFactory.getLogger("FooPlugin")

/**
* Configures a Gradle build.
Expand Down Expand Up @@ -47,31 +51,43 @@ class SettingsConventionPlugin: Plugin<Settings> {
// Apply the Foojay plugin
plugins.apply("org.gradle.toolchains.foojay-resolver-convention")

val isCI = System.getenv("CI").isNullOrEmpty()
gradle.settingsEvaluated {
if (plugins.hasPlugin("com.gradle.build-scan") || plugins.hasPlugin("com.gradle.enterprise")) {
// Configure the Gradle Enterprise plugin if it's configured
settings.extensions.getByType(GradleEnterpriseExtension::class.java).apply {
buildScan {
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
publishAlwaysIf(isCI)
if (isCI) tag("CI")
}
}
tryConfigureGradleEnterprisePlugin()
} else {
// Otherwise, apply and configure the Gradle Develocity plugin and configure it
plugins.apply("com.gradle.develocity")
settings.extensions.getByType(DevelocityConfiguration::class.java).apply {
buildScan {
termsOfUseUrl.set("https://gradle.com/help/legal-terms-of-use")
termsOfUseAgree.set("yes")
publishing.onlyIf { isCI }
if (isCI) tag("CI")
capture {
fileFingerprints.set(true)
}
}
configureDevelocityPlugin()
}
}
}

private fun Settings.tryConfigureGradleEnterprisePlugin() {
val isCI = System.getenv("CI").isNullOrEmpty()
try {
settings.extensions.getByType(GradleEnterpriseExtension::class.java).apply {
buildScan {
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
publishAlwaysIf(isCI)
if (isCI) tag("CI")
}
}
} catch (e: UnknownDomainObjectException) {
LOG.info("Could not configure Gradle Enterprise plugin")
// Ignored
}
}

private fun Settings.configureDevelocityPlugin() {
val isCI = System.getenv("CI").isNullOrEmpty()
settings.extensions.getByType(DevelocityConfiguration::class.java).apply {
buildScan {
termsOfUseUrl.set("https://gradle.com/help/legal-terms-of-use")
termsOfUseAgree.set("yes")
publishing.onlyIf { isCI }
if (isCI) tag("CI")
capture {
fileFingerprints.set(true)
}
}
}
Expand Down

0 comments on commit 167bfbc

Please sign in to comment.