Skip to content

Commit

Permalink
Add option to enable/disable logging
Browse files Browse the repository at this point in the history
Logging is enabled by default.
  • Loading branch information
erichaagdev committed Jan 26, 2025
1 parent ed09b39 commit 83cf748
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 198 deletions.
42 changes: 28 additions & 14 deletions src/main/resources/develocity-injection.init.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import org.gradle.util.GradleVersion

import static org.gradle.api.logging.LogLevel.LIFECYCLE
import static org.gradle.api.logging.LogLevel.QUIET
import static org.gradle.api.logging.LogLevel.WARN

initscript {
// NOTE: there is no mechanism to share code between the initscript{} block and the main script, so some logic is duplicated
def isTopLevelBuild = !gradle.parent
Expand Down Expand Up @@ -39,15 +43,20 @@ initscript {
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')

def log = { Gradle gradle, Logger logger, LogLevel level, String message ->
String loggingEnabled = getInputParam(gradle, 'develocity.injection.logging-enabled')
if (loggingEnabled == null || Boolean.parseBoolean(loggingEnabled)) logger.log(level, message)
}

if (develocityPluginVersion || ccudPluginVersion && atLeastGradle4) {
pluginRepositoryUrl = pluginRepositoryUrl ?: 'https://plugins.gradle.org/m2'
logger.lifecycle("Develocity plugins resolution: $pluginRepositoryUrl")
log(gradle, logger, LIFECYCLE, "Develocity plugins resolution: $pluginRepositoryUrl")

repositories {
maven {
url pluginRepositoryUrl
if (pluginRepositoryUsername && pluginRepositoryPassword) {
logger.lifecycle("Using credentials for plugin repository")
log(gradle, logger, LIFECYCLE, "Using credentials for plugin repository")
credentials {
username(pluginRepositoryUsername)
password(pluginRepositoryPassword)
Expand Down Expand Up @@ -90,10 +99,15 @@ if (!isTopLevelBuild) {
return
}

static log(Gradle gradle, Logger logger, LogLevel level, String message) {
String loggingEnabled = getInputParam(gradle, 'develocity.injection.logging-enabled')
if (loggingEnabled == null || Boolean.parseBoolean(loggingEnabled)) logger.log(level, message)
}

def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
def initScriptName = buildscript.sourceFile.name
if (requestedInitScriptName != initScriptName) {
logger.quiet("Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match")
log(gradle, logger, QUIET, "Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match")
return
}

Expand Down Expand Up @@ -146,16 +160,16 @@ void enableDevelocityInjection() {
}

def printEnforcingDevelocityUrl = {
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
log(gradle, logger, LIFECYCLE, "Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
}

def printAcceptingGradleTermsOfUse = {
logger.lifecycle("Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
log(gradle, logger, LIFECYCLE, "Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
}

// finish early if configuration parameters passed in via system properties are not valid/supported
if (ccudPluginVersion && isNotAtLeast(ccudPluginVersion, '1.7')) {
logger.warn("Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
log(gradle, logger, WARN, "Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
return
}

Expand All @@ -182,7 +196,7 @@ void enableDevelocityInjection() {
{ rootExtension }
)
if (develocityUrl) {
logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
log(gradle, logger, LIFECYCLE, "Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
rootExtension.server = develocityUrl
rootExtension.allowUntrustedServer = develocityAllowUntrustedServer
}
Expand All @@ -194,7 +208,7 @@ void enableDevelocityInjection() {
if (buildScanExtension.metaClass.respondsTo(buildScanExtension, 'setUploadInBackground', Boolean)) buildScanExtension.uploadInBackground = buildScanUploadInBackground
buildScanExtension.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, ciAutoInjectionCustomValueValue
if (isAtLeast(develocityPluginVersion, '2.1') && atLeastGradle5) {
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
log(gradle, logger, LIFECYCLE, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
if (isAtLeast(develocityPluginVersion, '3.17')) {
buildScanExtension.capture.fileFingerprints.set(develocityCaptureFileFingerprints)
} else if (isAtLeast(develocityPluginVersion, '3.7')) {
Expand Down Expand Up @@ -249,7 +263,7 @@ void enableDevelocityInjection() {
it.moduleVersion.with { group == "com.gradle" && name == "common-custom-user-data-gradle-plugin" }
}
if (!ccudPluginComponent) {
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
log(gradle, logger, LIFECYCLE, "Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
}
}
Expand All @@ -262,7 +276,7 @@ void enableDevelocityInjection() {
def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS)
applyPluginExternally(settings.pluginManager, pluginClass, develocityPluginVersion)
if (develocityUrl) {
logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
log(gradle, logger, LIFECYCLE, "Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
eachDevelocitySettingsExtension(settings) { ext ->
// server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
if (ext.metaClass.respondsTo(ext, 'getServer')) {
Expand All @@ -282,13 +296,13 @@ void enableDevelocityInjection() {

eachDevelocitySettingsExtension(settings,
{ develocity ->
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
log(gradle, logger, LIFECYCLE, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
develocity.buildScan.capture.fileFingerprints = develocityCaptureFileFingerprints
},
{ gradleEnterprise ->
gradleEnterprise.buildScan.publishAlways()
if (isAtLeast(develocityPluginVersion, '2.1')) {
logger.lifecycle("Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
log(gradle, logger, LIFECYCLE, "Setting captureFileFingerprints: $develocityCaptureFileFingerprints")
if (isAtLeast(develocityPluginVersion, '3.7')) {
gradleEnterprise.buildScan.capture.taskInputFiles = develocityCaptureFileFingerprints
} else {
Expand Down Expand Up @@ -337,7 +351,7 @@ void enableDevelocityInjection() {

if (ccudPluginVersion) {
if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) {
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
log(gradle, logger, LIFECYCLE, "Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
}
}
Expand All @@ -346,7 +360,7 @@ void enableDevelocityInjection() {
}

void applyPluginExternally(def pluginManager, String pluginClassName, String pluginVersion) {
logger.lifecycle("Applying $pluginClassName with version $pluginVersion via init script")
log(gradle, logger, LIFECYCLE, "Applying $pluginClassName with version $pluginVersion via init script")

def externallyApplied = 'develocity.externally-applied'
def externallyAppliedDeprecated = 'gradle.enterprise.externally-applied'
Expand Down
Loading

0 comments on commit 83cf748

Please # to comment.