Skip to content

Commit 5749a73

Browse files
authored
Support configuration of P2 mirrors (#1635 fixes #1629)
2 parents 19b25be + b636ebe commit 5749a73

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#1629](https://github.com/diffplug/spotless/issues/1629)).
1315
### Changes
1416
* **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
1517
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

Diff for: lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.Serializable;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Properties;
2324

2425
import com.diffplug.spotless.FileSignature;
@@ -43,6 +44,7 @@ public abstract class EquoBasedStepBuilder {
4344
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
4445
private String formatterVersion;
4546
private Iterable<File> settingsFiles = new ArrayList<>();
47+
private Map<String, String> p2Mirrors = Map.of();
4648

4749
/** Initialize valid default configuration, taking latest version */
4850
public EquoBasedStepBuilder(String formatterName, Provisioner mavenProvisioner, ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
@@ -59,6 +61,10 @@ public void setPreferences(Iterable<File> settingsFiles) {
5961
this.settingsFiles = settingsFiles;
6062
}
6163

64+
public void setP2Mirrors(Map<String, String> p2Mirrors) {
65+
this.p2Mirrors = Map.copyOf(p2Mirrors);
66+
}
67+
6268
/** Returns the FormatterStep (whose state will be calculated lazily). */
6369
public FormatterStep build() {
6470
return FormatterStep.createLazy(formatterName, this::get, stateToFormatter);
@@ -85,10 +91,10 @@ protected void addPlatformRepo(P2Model model, String version) {
8591

8692
/** Creates the state of the configuration. */
8793
EquoBasedStepBuilder.State get() throws Exception {
88-
var query = model(formatterVersion).query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
94+
var query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
8995
var classpath = new ArrayList<File>();
9096
var mavenDeps = new ArrayList<String>();
91-
mavenDeps.add("dev.equo.ide:solstice:1.0.0");
97+
mavenDeps.add("dev.equo.ide:solstice:1.0.3");
9298
mavenDeps.add("com.diffplug.durian:durian-swt.os:4.1.1");
9399
mavenDeps.addAll(query.getJarsOnMavenCentral());
94100
classpath.addAll(mavenProvisioner.provisionWithTransitives(false, mavenDeps));
@@ -100,6 +106,29 @@ EquoBasedStepBuilder.State get() throws Exception {
100106
return new State(formatterVersion, jarState, FileSignature.signAsList(settingsFiles));
101107
}
102108

109+
private P2Model createModelWithMirrors() {
110+
P2Model model = model(formatterVersion);
111+
if (p2Mirrors.isEmpty()) {
112+
return model;
113+
}
114+
115+
ArrayList<String> p2Repos = new ArrayList<>(model.getP2repo());
116+
p2Repos.replaceAll(url -> {
117+
for (Map.Entry<String, String> mirror : p2Mirrors.entrySet()) {
118+
String prefix = mirror.getKey();
119+
if (url.startsWith(prefix)) {
120+
return mirror.getValue() + url.substring(prefix.length());
121+
}
122+
}
123+
124+
throw new IllegalStateException("no mirror configured for P2 repository: " + url);
125+
});
126+
127+
model.getP2repo().clear();
128+
model.getP2repo().addAll(p2Repos);
129+
return model;
130+
}
131+
103132
/**
104133
* State of Eclipse configuration items, providing functionality to derived information
105134
* based on the state.

Diff for: plugin-gradle/CHANGES.md

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Support configuration of mirrors for P2 repositories ([#1629](https://github.com/diffplug/spotless/issues/1629)):
8+
```
9+
spotless {
10+
java {
11+
eclipse().withP2Mirrors(['https://download.eclipse.org/', 'https://some.internal.mirror/eclipse'])
12+
}
13+
}
14+
```
15+
Mirrors are selected by prefix match, for example `https://download.eclipse.org/eclipse/updates/4.26/` will be redirected to `https://some.internal.mirror/eclipse/eclipse/updates/4.26/`.
16+
The same configuration exists for `greclipse` and `eclipseCdt`.
617
### Changes
718
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
819
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;
1919

20+
import java.util.Map;
21+
2022
import javax.inject.Inject;
2123

2224
import org.gradle.api.Project;
@@ -56,6 +58,12 @@ public void configFile(Object... configFiles) {
5658
builder.setPreferences(project.files(configFiles).getFiles());
5759
replaceStep(builder.build());
5860
}
61+
62+
public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {
63+
builder.setP2Mirrors(mirrors);
64+
replaceStep(builder.build());
65+
return this;
66+
}
5967
}
6068

6169
@Override

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;
1919

20+
import java.util.Map;
2021
import java.util.Objects;
2122

2223
import javax.inject.Inject;
@@ -99,6 +100,12 @@ public void configFile(Object... configFiles) {
99100
builder.setPreferences(project.files(configFiles).getFiles());
100101
extension.replaceStep(builder.build());
101102
}
103+
104+
public GrEclipseConfig withP2Mirrors(Map<String, String> mirrors) {
105+
builder.setP2Mirrors(mirrors);
106+
extension.replaceStep(builder.build());
107+
return this;
108+
}
102109
}
103110

104111
/** If the user hasn't specified the files yet, we'll assume he/she means all of the groovy files. */

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Collection;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.Objects;
2526

2627
import javax.inject.Inject;
@@ -232,6 +233,12 @@ public void configFile(Object... configFiles) {
232233
replaceStep(builder.build());
233234
}
234235

236+
public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {
237+
builder.setP2Mirrors(mirrors);
238+
replaceStep(builder.build());
239+
return this;
240+
}
241+
235242
}
236243

237244
/** Removes newlines between type annotations and types. */

0 commit comments

Comments
 (0)