-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
RepoScriptBlockUtil.groovy
230 lines (197 loc) · 9.03 KB
/
RepoScriptBlockUtil.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.integtests.fixtures
import groovy.transform.CompileStatic
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.test.fixtures.dsl.GradleDsl
import static org.gradle.api.artifacts.ArtifactRepositoryContainer.GOOGLE_URL
import static org.gradle.api.artifacts.ArtifactRepositoryContainer.MAVEN_CENTRAL_URL
import static org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.BINTRAY_JCENTER_URL
import static org.gradle.test.fixtures.dsl.GradleDsl.GROOVY
import static org.gradle.test.fixtures.dsl.GradleDsl.KOTLIN
@CompileStatic
class RepoScriptBlockUtil {
static String repositoryDefinition(GradleDsl dsl = GROOVY, String type, String name, String url) {
if (dsl == KOTLIN) {
"""
${type} {
name = "${name}"
url = uri("${url}")
}
"""
} else {
"""
${type} {
name '${name}'
url '${url}'
}
"""
}
}
private static enum MirroredRepository {
JCENTER(BINTRAY_JCENTER_URL, System.getProperty('org.gradle.integtest.mirrors.jcenter'), "maven"),
MAVEN_CENTRAL(MAVEN_CENTRAL_URL, System.getProperty('org.gradle.integtest.mirrors.mavencentral'), "maven"),
GOOGLE(GOOGLE_URL, System.getProperty('org.gradle.integtest.mirrors.google'), "maven"),
LIGHTBEND_MAVEN("https://repo.lightbend.com/lightbend/maven-releases", System.getProperty('org.gradle.integtest.mirrors.lightbendmaven'), "maven"),
LIGHTBEND_IVY("https://repo.lightbend.com/lightbend/ivy-releases", System.getProperty('org.gradle.integtest.mirrors.lightbendivy'), "ivy"),
SPRING_RELEASES('https://repo.spring.io/release', System.getProperty('org.gradle.integtest.mirrors.springreleases'), 'maven'),
SPRING_SNAPSHOTS('https://repo.spring.io/snapshot/', System.getProperty('org.gradle.integtest.mirrors.springsnapshots'), 'maven'),
GRADLE('https://repo.gradle.org/gradle/repo', System.getProperty('org.gradle.integtest.mirrors.gradle'), 'maven'),
JBOSS('https://repository.jboss.org/maven2/', System.getProperty('org.gradle.integtest.mirrors.jboss'), 'maven'),
GRADLE_PLUGIN("https://plugins.gradle.org/m2", System.getProperty('org.gradle.integtest.mirrors.gradle-prod-plugins'), 'maven'),
GRADLE_LIB_RELEASES('https://repo.gradle.org/gradle/libs-releases', System.getProperty('org.gradle.integtest.mirrors.gradle'), 'maven'),
GRADLE_LIB_MILESTONES('https://repo.gradle.org/gradle/libs-milestones', System.getProperty('org.gradle.integtest.mirrors.gradle'), 'maven'),
GRADLE_LIB_SNAPSHOTS('https://repo.gradle.org/gradle/libs-snapshots', System.getProperty('org.gradle.integtest.mirrors.gradle'), 'maven'),
GRADLE_JAVASCRIPT('https://repo.gradle.org/gradle/javascript-public', System.getProperty('org.gradle.integtest.mirrors.gradlejavascript'), 'maven')
String originalUrl
String mirrorUrl
String type
private MirroredRepository(String originalUrl, String mirrorUrl, String type) {
this.originalUrl = originalUrl
this.mirrorUrl = mirrorUrl ?: originalUrl
this.type = type
}
String getRepositoryDefinition(GradleDsl dsl = GROOVY) {
repositoryDefinition(dsl, type, getName(), mirrorUrl)
}
void configure(RepositoryHandler repositories) {
repositories.maven { MavenArtifactRepository repo ->
repo.name = getName()
repo.url = mirrorUrl
}
}
String getName() {
return mirrorUrl ? name() + "_MIRROR" : name()
}
}
private RepoScriptBlockUtil() {
}
static String jcenterRepository(GradleDsl dsl = GROOVY) {
"""
repositories {
${jcenterRepositoryDefinition(dsl)}
}
"""
}
static void configureMavenCentral(RepositoryHandler repositories) {
MirroredRepository.MAVEN_CENTRAL.configure(repositories)
}
static String mavenCentralRepository(GradleDsl dsl = GROOVY) {
return """
repositories {
${mavenCentralRepositoryDefinition(dsl)}
}
"""
}
static String googleRepository(GradleDsl dsl = GROOVY) {
return """
repositories {
${googleRepositoryDefinition(dsl)}
}
"""
}
static String jcenterRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.JCENTER.getRepositoryDefinition(dsl)
}
static String mavenCentralRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.MAVEN_CENTRAL.getRepositoryDefinition(dsl)
}
static String lightbendMavenRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.LIGHTBEND_MAVEN.getRepositoryDefinition(dsl)
}
static String lightbendIvyRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.LIGHTBEND_IVY.getRepositoryDefinition(dsl)
}
static String googleRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.GOOGLE.getRepositoryDefinition(dsl)
}
static String gradleRepositoryMirrorUrl() {
MirroredRepository.GRADLE.mirrorUrl
}
static String gradleRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.GRADLE.getRepositoryDefinition(dsl)
}
static String gradlePluginRepositoryMirrorUrl() {
MirroredRepository.GRADLE_PLUGIN.mirrorUrl
}
static String gradlePluginRepositoryDefinition(GradleDsl dsl = GROOVY) {
MirroredRepository.GRADLE_PLUGIN.getRepositoryDefinition(dsl)
}
static File createMirrorInitScript() {
File mirrors = File.createTempFile("mirrors", ".gradle")
mirrors.deleteOnExit()
mirrors << mirrorInitScript()
return mirrors
}
static String mirrorInitScript() {
def mirrorConditions = MirroredRepository.values().collect { MirroredRepository mirror ->
"""
if (normalizeUrl(repo.url) == normalizeUrl('${mirror.originalUrl}')) {
repo.url = '${mirror.mirrorUrl}'
}
"""
}.join("")
return """
import groovy.transform.CompileStatic
import groovy.transform.CompileDynamic
apply plugin: MirrorPlugin
@CompileStatic
class MirrorPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.allprojects { Project project ->
project.buildscript.configurations["classpath"].incoming.beforeResolve {
withMirrors(project.buildscript.repositories)
}
project.afterEvaluate {
withMirrors(project.repositories)
}
}
maybeConfigurePluginManagement(gradle)
}
@CompileDynamic
void maybeConfigurePluginManagement(Gradle gradle) {
if (gradle.gradleVersion >= "4.4") {
gradle.settingsEvaluated { Settings settings ->
withMirrors(settings.pluginManagement.repositories)
}
}
}
void withMirrors(RepositoryHandler repos) {
repos.all { repo ->
if (repo instanceof MavenArtifactRepository) {
mirror(repo)
} else if (repo instanceof IvyArtifactRepository) {
mirror(repo)
}
}
}
void mirror(MavenArtifactRepository repo) {
${mirrorConditions}
}
void mirror(IvyArtifactRepository repo) {
${mirrorConditions}
}
// We see them as equal:
// https://repo.maven.apache.org/maven2/ and http://repo.maven.apache.org/maven2
String normalizeUrl(Object url) {
String result = url.toString().replace('https://', 'http://')
return result.endsWith("/") ? result : result + "/"
}
}
"""
}
}