Skip to content

Commit a8b7e1e

Browse files
committed
Extract init script logic to helper functions
1 parent 2a50cab commit a8b7e1e

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

components/scripts/gradle/gradle-init-scripts/configure-remote-build-caching.gradle

+59-35
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,12 @@ settingsEvaluated { Settings settings ->
1919
}
2020

2121
if (remoteBuildCacheType) {
22-
if (!['develocity', 'gradle-enterprise', 'http'].contains(remoteBuildCacheType)) {
23-
// The scripts already fail if the value of --remote-build-cache-type isn't valid.
24-
// This is for the sake of completeness since this init script assumes it's valid.
25-
def errorFile = new File(expDir, 'errors.txt')
26-
def message = "Invalid value '${remoteBuildCacheType}' for remote build cache type. Values are 'develocity', 'gradle-enterprise', or 'http'."
27-
errorFile.text = message
28-
throw new IllegalStateException(message)
22+
if (isInvalidRemoteBuildCacheType(remoteBuildCacheType)) {
23+
failInvalidRemoteBuildCacheType(remoteBuildCacheType, expDir)
2924
}
3025

3126
if (isTopLevelBuild && missingRequiredPlugin(settings, remoteBuildCacheType)) {
32-
// Included builds may not have the necessary plugin applied.
33-
// Only fail if the top-level build is missing the required extension.
34-
def errorFile = new File(expDir, 'errors.txt')
35-
errorFile.text = "Remote build cache type '${remoteBuildCacheType}' requested, but the required plugin is not applied."
36-
if (remoteBuildCacheType == 'develocity') {
37-
throw new IllegalStateException("Remote build cache type 'develocity' requested,\n" +
38-
"but the Develocity Gradle plugin is not applied.\n" +
39-
"Either apply it directly (see $docsRoot/current/#applying_the_plugin),\n" +
40-
"use --enable-develocity to enable the plugin,\n" +
41-
"or use --remote-build-cache-type to choose a different remote build cache type\n" +
42-
"when running the build validation script.")
43-
} else {
44-
throw new IllegalStateException("Remote build cache type 'gradle-enterprise' requested,\n" +
45-
"but the Gradle Enterprise Gradle plugin is not applied (see $docsRoot/legacy/#applying_the_plugin).")
46-
}
27+
failMissingRequiredPlugin(remoteBuildCacheType, expDir, docsRoot)
4728
}
4829

4930
def remoteBuildCacheImplementation = getRemoteBuildCacheImplementation(settings, remoteBuildCacheType)
@@ -64,30 +45,24 @@ settingsEvaluated { Settings settings ->
6445
if (remoteBuildCacheUrl) {
6546
remote.url = remoteBuildCacheUri
6647
} else if (!remote.url) {
67-
// Gradle already fails in this case, but handling it here means we can fail the experiment more
68-
// gracefully and provide guidance the user.
69-
def errorFile = new File(expDir, 'errors.txt')
70-
errorFile.text = 'A remote build cache URL has not been configured in the project or on the command line.'
71-
throw new IllegalStateException("A remote build cache URL is not configured.\n"
72-
+ "Either configure it directly (see $docsRoot/current/#using_gradles_built_in_http_connector) in the project,\n"
73-
+ "or use --remote-build-cache-url when running the build validation script.")
48+
failMissingUrlForHttpBuildCache(expDir, docsRoot)
7449
}
75-
} else if (remote.class.name.startsWith('com.gradle.develocity') || remote.class.name.startsWith('com.gradle.enterprise')) {
50+
} else if (isBuildCacheImplementationFor(remote, 'com.gradle.develocity') || isBuildCacheImplementationFor(remote, 'com.gradle.enterprise')) {
7651
if (remoteBuildCacheUri) {
7752
remote.server = toServerPart(remoteBuildCacheUri)
7853
remote.path = remoteBuildCacheUri.path
7954
}
8055
}
8156
} else if (isTopLevelBuild) {
82-
def errorFile = new File(expDir, 'errors.txt')
83-
errorFile.text = "Remote build cache is not configured for the project."
84-
throw new IllegalStateException("Remote build cache is not configured for the project.\n" +
85-
"Either configure it directly (see $docsRoot/current/#using_the_develocity_connector),\n" +
86-
"or use --remote-build-cache-type when running the build validation script.")
57+
failMissingRemoteBuildCacheConfiguration(expDir, docsRoot)
8758
}
8859
}
8960
}
9061

62+
static boolean isInvalidRemoteBuildCacheType(String remoteBuildCacheType) {
63+
return !['develocity', 'gradle-enterprise', 'http'].contains(remoteBuildCacheType)
64+
}
65+
9166
static boolean missingRequiredPlugin(Settings settings, String type) {
9267
return type == "develocity" && !settings.pluginManager.hasPlugin('com.gradle.develocity')
9368
|| type == "gradle-enterprise" && !settings.pluginManager.hasPlugin('com.gradle.enterprise')
@@ -104,10 +79,59 @@ static Class<? extends BuildCache> getRemoteBuildCacheImplementation(Settings se
10479
return null
10580
}
10681

82+
static boolean isBuildCacheImplementationFor(BuildCache buildCache, String implementation) {
83+
return buildCache.class.name.startsWith(implementation)
84+
}
85+
10786
static URI withPathTrailingSlash(URI uri) {
10887
return uri.path.endsWith("/") ? uri : new URI(uri.scheme, uri.userInfo, uri.host, uri.port, uri.path + "/", uri.query, uri.fragment)
10988
}
11089

11190
static String toServerPart(URI uri) {
11291
return new URI(uri.scheme, uri.userInfo, uri.host, uri.port, null, uri.query, uri.fragment)
11392
}
93+
94+
// The scripts already fail if the value of --remote-build-cache-type isn't valid.
95+
// This is for the sake of completeness since this init script assumes it's valid.
96+
static void failInvalidRemoteBuildCacheType(String remoteBuildCacheType, String expDir) {
97+
def errorFile = new File(expDir, 'errors.txt')
98+
def message = "Invalid value '${remoteBuildCacheType}' for remote build cache type. Values are 'develocity', 'gradle-enterprise', or 'http'."
99+
errorFile.text = message
100+
throw new IllegalStateException(message)
101+
}
102+
103+
// Included builds may not have the necessary plugin applied.
104+
// Only fail if the top-level build is missing the required extension.
105+
static void failMissingRequiredPlugin(String remoteBuildCacheType, String expDir, String docsRoot) {
106+
def errorFile = new File(expDir, 'errors.txt')
107+
errorFile.text = "Remote build cache type '${remoteBuildCacheType}' requested, but the required plugin is not applied."
108+
if (remoteBuildCacheType == 'develocity') {
109+
throw new IllegalStateException("Remote build cache type 'develocity' requested,\n" +
110+
"but the Develocity Gradle plugin is not applied.\n" +
111+
"Either apply it directly (see $docsRoot/current/#applying_the_plugin),\n" +
112+
"use --enable-develocity to enable the plugin,\n" +
113+
"or use --remote-build-cache-type to choose a different remote build cache type\n" +
114+
"when running the build validation script.")
115+
} else {
116+
throw new IllegalStateException("Remote build cache type 'gradle-enterprise' requested,\n" +
117+
"but the Gradle Enterprise Gradle plugin is not applied (see $docsRoot/legacy/#applying_the_plugin).")
118+
}
119+
}
120+
121+
// Gradle already fails in this case, but handling it here means we can fail the experiment more
122+
// gracefully and provide guidance the user.
123+
static void failMissingUrlForHttpBuildCache(String expDir, String docsRoot) {
124+
def errorFile = new File(expDir, 'errors.txt')
125+
errorFile.text = 'A remote build cache URL has not been configured in the project or on the command line.'
126+
throw new IllegalStateException("A remote build cache URL is not configured.\n"
127+
+ "Either configure it directly (see $docsRoot/current/#using_gradles_built_in_http_connector) in the project,\n"
128+
+ "or use --remote-build-cache-url when running the build validation script.")
129+
}
130+
131+
static void failMissingRemoteBuildCacheConfiguration(String expDir, String docsRoot) {
132+
def errorFile = new File(expDir, 'errors.txt')
133+
errorFile.text = "Remote build cache is not configured for the project."
134+
throw new IllegalStateException("Remote build cache is not configured for the project.\n" +
135+
"Either configure it directly (see $docsRoot/current/#using_the_develocity_connector),\n" +
136+
"or use --remote-build-cache-type when running the build validation script.")
137+
}

0 commit comments

Comments
 (0)