Skip to content

Commit d8d5a14

Browse files
[bot] Promote init-script as v1.1
1 parent c116940 commit d8d5a14

File tree

1 file changed

+130
-87
lines changed

1 file changed

+130
-87
lines changed

reference/develocity-injection.init.gradle

Lines changed: 130 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Initscript for injection of Develocity into Gradle builds.
3-
* Version: v1.0
3+
* Version: v1.1
44
*/
55

66
import org.gradle.util.GradleVersion
@@ -12,29 +12,29 @@ initscript {
1212
return
1313
}
1414

15-
def getInputParam = { String name ->
15+
def getInputParam = { Gradle gradle, String name ->
1616
def ENV_VAR_PREFIX = ''
1717
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
18-
return System.getProperty(name) ?: System.getenv(envVarName)
18+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getProperty(name) ?: System.getenv(envVarName)
1919
}
2020

21-
def requestedInitScriptName = getInputParam('develocity.injection.init-script-name')
21+
def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
2222
def initScriptName = buildscript.sourceFile.name
2323
if (requestedInitScriptName != initScriptName) {
2424
return
2525
}
2626

2727
// Plugin loading is only required for Develocity injection. Abort early if not enabled.
28-
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled"))
28+
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam(gradle, "develocity.injection-enabled"))
2929
if (!develocityInjectionEnabled) {
3030
return
3131
}
3232

33-
def pluginRepositoryUrl = getInputParam('gradle.plugin-repository.url')
34-
def pluginRepositoryUsername = getInputParam('gradle.plugin-repository.username')
35-
def pluginRepositoryPassword = getInputParam('gradle.plugin-repository.password')
36-
def develocityPluginVersion = getInputParam('develocity.plugin.version')
37-
def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version')
33+
def pluginRepositoryUrl = getInputParam(gradle, 'gradle.plugin-repository.url')
34+
def pluginRepositoryUsername = getInputParam(gradle, 'gradle.plugin-repository.username')
35+
def pluginRepositoryPassword = getInputParam(gradle, 'gradle.plugin-repository.password')
36+
def develocityPluginVersion = getInputParam(gradle, 'develocity.plugin.version')
37+
def ccudPluginVersion = getInputParam(gradle, 'develocity.ccud-plugin.version')
3838

3939
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
4040
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
@@ -79,25 +79,25 @@ initscript {
7979
}
8080
}
8181

82-
static getInputParam(String name) {
82+
static getInputParam(Gradle gradle, String name) {
8383
def ENV_VAR_PREFIX = ''
8484
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
85-
return System.getProperty(name) ?: System.getenv(envVarName)
85+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getProperty(name) ?: System.getenv(envVarName)
8686
}
8787

8888
def isTopLevelBuild = !gradle.parent
8989
if (!isTopLevelBuild) {
9090
return
9191
}
9292

93-
def requestedInitScriptName = getInputParam('develocity.injection.init-script-name')
93+
def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
9494
def initScriptName = buildscript.sourceFile.name
9595
if (requestedInitScriptName != initScriptName) {
9696
logger.quiet("Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match")
9797
return
9898
}
9999

100-
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled"))
100+
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam(gradle, "develocity.injection-enabled"))
101101
if (develocityInjectionEnabled) {
102102
enableDevelocityInjection()
103103
}
@@ -123,16 +123,16 @@ void enableDevelocityInjection() {
123123
def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
124124
def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin'
125125

126-
def develocityUrl = getInputParam('develocity.url')
127-
def develocityAllowUntrustedServer = Boolean.parseBoolean(getInputParam('develocity.allow-untrusted-server'))
128-
def develocityEnforceUrl = Boolean.parseBoolean(getInputParam('develocity.enforce-url'))
129-
def buildScanUploadInBackground = Boolean.parseBoolean(getInputParam('develocity.build-scan.upload-in-background'))
130-
def develocityCaptureFileFingerprints = getInputParam('develocity.capture-file-fingerprints') ? Boolean.parseBoolean(getInputParam('develocity.capture-file-fingerprints')) : true
131-
def develocityPluginVersion = getInputParam('develocity.plugin.version')
132-
def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version')
133-
def buildScanTermsOfUseUrl = getInputParam('develocity.terms-of-use.url')
134-
def buildScanTermsOfUseAgree = getInputParam('develocity.terms-of-use.agree')
135-
def ciAutoInjectionCustomValueValue = getInputParam('develocity.auto-injection.custom-value')
126+
def develocityUrl = getInputParam(gradle, 'develocity.url')
127+
def develocityAllowUntrustedServer = Boolean.parseBoolean(getInputParam(gradle, 'develocity.allow-untrusted-server'))
128+
def develocityEnforceUrl = Boolean.parseBoolean(getInputParam(gradle, 'develocity.enforce-url'))
129+
def buildScanUploadInBackground = Boolean.parseBoolean(getInputParam(gradle, 'develocity.build-scan.upload-in-background'))
130+
def develocityCaptureFileFingerprints = getInputParam(gradle, 'develocity.capture-file-fingerprints') ? Boolean.parseBoolean(getInputParam(gradle, 'develocity.capture-file-fingerprints')) : true
131+
def develocityPluginVersion = getInputParam(gradle, 'develocity.plugin.version')
132+
def ccudPluginVersion = getInputParam(gradle, 'develocity.ccud-plugin.version')
133+
def buildScanTermsOfUseUrl = getInputParam(gradle, 'develocity.terms-of-use.url')
134+
def buildScanTermsOfUseAgree = getInputParam(gradle, 'develocity.terms-of-use.agree')
135+
def ciAutoInjectionCustomValueValue = getInputParam(gradle, 'develocity.auto-injection.custom-value')
136136

137137
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
138138
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
@@ -145,6 +145,14 @@ void enableDevelocityInjection() {
145145
return geValue instanceof Closure<?> ? geValue() : geValue
146146
}
147147

148+
def printEnforcingDevelocityUrl = {
149+
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
150+
}
151+
152+
def printAcceptingGradleTermsOfUse = {
153+
logger.lifecycle("Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
154+
}
155+
148156
// finish early if configuration parameters passed in via system properties are not valid/supported
149157
if (ccudPluginVersion && isNotAtLeast(ccudPluginVersion, '1.7')) {
150158
logger.warn("Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
@@ -163,8 +171,8 @@ void enableDevelocityInjection() {
163171
}
164172
if (!scanPluginComponent) {
165173
def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, BUILD_SCAN_PLUGIN_CLASS)
166-
logger.lifecycle("Applying $pluginClass via init script")
167-
applyPluginExternally(pluginManager, pluginClass)
174+
def pluginVersion = atLeastGradle5 ? develocityPluginVersion : "1.16"
175+
applyPluginExternally(pluginManager, pluginClass, pluginVersion)
168176
def rootExtension = dvOrGe(
169177
{ develocity },
170178
{ buildScan }
@@ -196,48 +204,52 @@ void enableDevelocityInjection() {
196204
}
197205
}
198206
}
207+
}
199208

200-
if (develocityUrl && develocityEnforceUrl) {
201-
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
202-
}
203-
204-
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
205-
// Only execute if develocity plugin isn't applied.
206-
if (gradle.rootProject.extensions.findByName("develocity")) return
209+
eachDevelocityProjectExtension(project,
210+
{ develocity ->
207211
afterEvaluate {
208212
if (develocityUrl && develocityEnforceUrl) {
209-
buildScan.server = develocityUrl
210-
buildScan.allowUntrustedServer = develocityAllowUntrustedServer
213+
printEnforcingDevelocityUrl()
214+
develocity.server = develocityUrl
215+
develocity.allowUntrustedServer = develocityAllowUntrustedServer
211216
}
212217
}
213218

214219
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
215-
buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
216-
buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
220+
printAcceptingGradleTermsOfUse()
221+
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
222+
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
217223
}
218-
}
219-
220-
pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
224+
},
225+
{ buildScan ->
221226
afterEvaluate {
222227
if (develocityUrl && develocityEnforceUrl) {
223-
develocity.server = develocityUrl
224-
develocity.allowUntrustedServer = develocityAllowUntrustedServer
228+
printEnforcingDevelocityUrl()
229+
buildScan.server = develocityUrl
230+
buildScan.allowUntrustedServer = develocityAllowUntrustedServer
225231
}
226232
}
227233

228234
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
229-
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
230-
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
235+
printAcceptingGradleTermsOfUse()
236+
if (buildScan.metaClass.respondsTo(buildScan, 'setTermsOfServiceUrl', String)) {
237+
buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
238+
buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
239+
} else {
240+
buildScan.licenseAgreementUrl = buildScanTermsOfUseUrl
241+
buildScan.licenseAgree = buildScanTermsOfUseAgree
242+
}
231243
}
232244
}
233-
}
245+
)
234246

235247
if (ccudPluginVersion && atLeastGradle4) {
236248
def ccudPluginComponent = resolutionResult.allComponents.find {
237249
it.moduleVersion.with { group == "com.gradle" && name == "common-custom-user-data-gradle-plugin" }
238250
}
239251
if (!ccudPluginComponent) {
240-
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS via init script")
252+
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
241253
pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
242254
}
243255
}
@@ -248,13 +260,18 @@ void enableDevelocityInjection() {
248260
if (develocityPluginVersion) {
249261
if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) && !settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) {
250262
def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS)
251-
logger.lifecycle("Applying $pluginClass via init script")
252-
applyPluginExternally(settings.pluginManager, pluginClass)
263+
applyPluginExternally(settings.pluginManager, pluginClass, develocityPluginVersion)
253264
if (develocityUrl) {
254265
logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
255266
eachDevelocitySettingsExtension(settings) { ext ->
256-
ext.server = develocityUrl
257-
ext.allowUntrustedServer = develocityAllowUntrustedServer
267+
// server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
268+
if (ext.metaClass.respondsTo(ext, 'getServer')) {
269+
ext.server = develocityUrl
270+
ext.allowUntrustedServer = develocityAllowUntrustedServer
271+
} else {
272+
ext.buildScan.server = develocityUrl
273+
ext.buildScan.allowUntrustedServer = develocityAllowUntrustedServer
274+
}
258275
}
259276
}
260277

@@ -281,48 +298,56 @@ void enableDevelocityInjection() {
281298
}
282299
)
283300
}
301+
}
284302

285-
if (develocityUrl && develocityEnforceUrl) {
286-
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
287-
}
288-
289-
eachDevelocitySettingsExtension(settings,
290-
{ develocity ->
291-
if (develocityUrl && develocityEnforceUrl) {
292-
develocity.server = develocityUrl
293-
develocity.allowUntrustedServer = develocityAllowUntrustedServer
294-
}
303+
eachDevelocitySettingsExtension(settings,
304+
{ develocity ->
305+
if (develocityUrl && develocityEnforceUrl) {
306+
printEnforcingDevelocityUrl()
307+
develocity.server = develocityUrl
308+
develocity.allowUntrustedServer = develocityAllowUntrustedServer
309+
}
295310

296-
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
297-
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
298-
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
299-
}
300-
},
301-
{ gradleEnterprise ->
302-
if (develocityUrl && develocityEnforceUrl) {
311+
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
312+
printAcceptingGradleTermsOfUse()
313+
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
314+
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
315+
}
316+
},
317+
{ gradleEnterprise ->
318+
if (develocityUrl && develocityEnforceUrl) {
319+
printEnforcingDevelocityUrl()
320+
// server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
321+
if (gradleEnterprise.metaClass.respondsTo(gradleEnterprise, 'getServer')) {
303322
gradleEnterprise.server = develocityUrl
304323
gradleEnterprise.allowUntrustedServer = develocityAllowUntrustedServer
324+
} else {
325+
gradleEnterprise.buildScan.server = develocityUrl
326+
gradleEnterprise.buildScan.allowUntrustedServer = develocityAllowUntrustedServer
305327
}
328+
}
306329

307-
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
308-
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
309-
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
310-
}
330+
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
331+
printAcceptingGradleTermsOfUse()
332+
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
333+
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
311334
}
312-
)
313-
}
335+
}
336+
)
314337

315338
if (ccudPluginVersion) {
316339
if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) {
317-
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS via init script")
340+
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
318341
settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
319342
}
320343
}
321344
}
322345
}
323346
}
324347

325-
void applyPluginExternally(def pluginManager, String pluginClassName) {
348+
void applyPluginExternally(def pluginManager, String pluginClassName, String pluginVersion) {
349+
logger.lifecycle("Applying $pluginClassName with version $pluginVersion via init script")
350+
326351
def externallyApplied = 'develocity.externally-applied'
327352
def externallyAppliedDeprecated = 'gradle.enterprise.externally-applied'
328353
def oldValue = System.getProperty(externallyApplied)
@@ -367,6 +392,32 @@ static def eachDevelocitySettingsExtension(def settings, def dvAction, def geAct
367392
}
368393
}
369394

395+
/**
396+
* Apply the `dvAction` to the 'develocity' extension.
397+
* If no 'develocity' extension is found, apply the `bsAction` to the 'buildScan' extension.
398+
* (The develocity plugin creates both extensions, and we want to prefer configuring 'develocity').
399+
*/
400+
static def eachDevelocityProjectExtension(def project, def dvAction, def bsAction = dvAction) {
401+
def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
402+
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
403+
404+
def configureDvOrBsExtension = {
405+
if (project.extensions.findByName("develocity")) {
406+
dvAction(project.develocity)
407+
} else {
408+
bsAction(project.buildScan)
409+
}
410+
}
411+
412+
project.pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID, configureDvOrBsExtension)
413+
414+
project.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
415+
// Proper extension will be configured by the build-scan callback.
416+
if (project.pluginManager.hasPlugin(BUILD_SCAN_PLUGIN_ID)) return
417+
configureDvOrBsExtension()
418+
}
419+
}
420+
370421
static boolean isAtLeast(String versionUnderTest, String referenceVersion) {
371422
GradleVersion.version(versionUnderTest) >= GradleVersion.version(referenceVersion)
372423
}
@@ -376,21 +427,13 @@ static boolean isNotAtLeast(String versionUnderTest, String referenceVersion) {
376427
}
377428

378429
void enableBuildScanLinkCapture(BuildScanCollector collector) {
379-
def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
380-
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
381-
382430
// Conditionally apply and configure the Develocity plugin
383431
if (GradleVersion.current() < GradleVersion.version('6.0')) {
384432
rootProject {
385-
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
386-
// Only execute if develocity plugin isn't applied.
387-
if (gradle.rootProject.extensions.findByName("develocity")) return
388-
buildScanPublishedAction(buildScan, collector)
389-
}
390-
391-
pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
392-
buildScanPublishedAction(develocity.buildScan, collector)
393-
}
433+
eachDevelocityProjectExtension(project,
434+
{ develocity -> buildScanPublishedAction(develocity.buildScan, collector) },
435+
{ buildScan -> buildScanPublishedAction(buildScan, collector) }
436+
)
394437
}
395438
} else {
396439
gradle.settingsEvaluated { settings ->

0 commit comments

Comments
 (0)