Commit 04984215 authored by bigdaz's avatar bigdaz Committed by Daz DeBoer
Browse files

Update develocity-injection init script to v0.5.0

parent edb13383
Loading
Loading
Loading
Loading
+73 −27
Changes for sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle: 73 added lines, 27 removed lines.
Original line number Diff line number Diff line
/*
 * Initscript for injection of Develocity into Gradle builds.
 * Version: v0.4.0
 * Version: v0.5.0
 */

import org.gradle.util.GradleVersion
@@ -14,8 +14,8 @@ initscript {
        return
    }

    def ENV_VAR_PREFIX = ''
    def getInputParam = { String name ->
        def ENV_VAR_PREFIX = ''
        def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
        return System.getProperty(name) ?: System.getenv(envVarName)
    }
@@ -26,9 +26,9 @@ initscript {
        return
    }

    // finish early if injection is disabled
    def gradleInjectionEnabled = getInputParam("develocity.injection-enabled")
    if (gradleInjectionEnabled != "true") {
    // plugin loading is only required for Develocity injection
    def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled"))
    if (!develocityInjectionEnabled) {
        return
    }

@@ -81,30 +81,17 @@ initscript {
    }
}

def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'

def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin'

def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
def DEVELOCITY_PLUGIN_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityPlugin'

def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection'
def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin'
static getInputParam(String name) {
    def ENV_VAR_PREFIX = ''
    def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
    return System.getProperty(name) ?: System.getenv(envVarName)
}

def isTopLevelBuild = !gradle.parent
if (!isTopLevelBuild) {
    return
}

def ENV_VAR_PREFIX = ''
def getInputParam = { String name ->
    def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
    return System.getProperty(name) ?: System.getenv(envVarName)
}

def requestedInitScriptName = getInputParam('develocity.injection.init-script-name')
def initScriptName = buildscript.sourceFile.name
if (requestedInitScriptName != initScriptName) {
@@ -112,12 +99,32 @@ if (requestedInitScriptName != initScriptName) {
    return
}

// finish early if injection is disabled
def gradleInjectionEnabled = getInputParam("develocity.injection-enabled")
if (gradleInjectionEnabled != "true") {
    return
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled"))
if (develocityInjectionEnabled) {
    enableDevelocityInjection()
}

// To enable build-scan capture, a `captureBuildScanLink(String)` method must be added to `BuildScanCollector`.
def buildScanCollector = new BuildScanCollector()
def buildScanCaptureEnabled = buildScanCollector.metaClass.respondsTo(buildScanCollector, 'captureBuildScanLink', String)
if (buildScanCaptureEnabled) {
    enableBuildScanLinkCapture(buildScanCollector)
}

void enableDevelocityInjection() {
    def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
    def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'

    def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
    def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin'

    def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
    def DEVELOCITY_PLUGIN_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityPlugin'

    def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection'
    def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
    def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin'

    def develocityUrl = getInputParam('develocity.url')
    def develocityAllowUntrustedServer = Boolean.parseBoolean(getInputParam('develocity.allow-untrusted-server'))
    def develocityEnforceUrl = Boolean.parseBoolean(getInputParam('develocity.enforce-url'))
@@ -315,6 +322,7 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
            }
        }
    }
}

void applyPluginExternally(def pluginManager, String pluginClassName) {
    def externallyApplied = 'develocity.externally-applied'
@@ -368,3 +376,41 @@ static boolean isAtLeast(String versionUnderTest, String referenceVersion) {
static boolean isNotAtLeast(String versionUnderTest, String referenceVersion) {
    !isAtLeast(versionUnderTest, referenceVersion)
}

void enableBuildScanLinkCapture(BuildScanCollector collector) {
    def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
    def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'

    // Conditionally apply and configure the Develocity plugin
    if (GradleVersion.current() < GradleVersion.version('6.0')) {
        rootProject {
            pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
                // Only execute if develocity plugin isn't applied.
                if (gradle.rootProject.extensions.findByName("develocity")) return
                buildScanPublishedAction(buildScan, collector)
            }

            pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
                buildScanPublishedAction(develocity.buildScan, collector)
            }
        }
    } else {
        gradle.settingsEvaluated { settings ->
            eachDevelocitySettingsExtension(settings) { ext ->
                buildScanPublishedAction(ext.buildScan, collector)
            }
        }
    }
}

// Action will only be called if a `BuildScanCollector.captureBuildScanLink` method is present.
// Add `void captureBuildScanLink(String) {}` to the `BuildScanCollector` class to respond to buildScanPublished events
static buildScanPublishedAction(def buildScanExtension, BuildScanCollector collector) {
    if (buildScanExtension.metaClass.respondsTo(buildScanExtension, 'buildScanPublished', Action)) {
        buildScanExtension.buildScanPublished { scan ->
            collector.captureBuildScanLink(scan.buildScanUri.toString())
        }
    }
}

class BuildScanCollector {}