diff --git a/CHANGES.md b/CHANGES.md index ec06be1db5..77c49efe2a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#1629](https://github.com/diffplug/spotless/issues/1629)). ### Changes * **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) * Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java index 5283321cec..cc12a938ad 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Properties; import com.diffplug.spotless.FileSignature; @@ -43,6 +44,7 @@ public abstract class EquoBasedStepBuilder { private final ThrowingEx.Function stateToFormatter; private String formatterVersion; private Iterable settingsFiles = new ArrayList<>(); + private Map p2Mirrors = Map.of(); /** Initialize valid default configuration, taking latest version */ public EquoBasedStepBuilder(String formatterName, Provisioner mavenProvisioner, ThrowingEx.Function stateToFormatter) { @@ -59,6 +61,10 @@ public void setPreferences(Iterable settingsFiles) { this.settingsFiles = settingsFiles; } + public void setP2Mirrors(Map p2Mirrors) { + this.p2Mirrors = Map.copyOf(p2Mirrors); + } + /** Returns the FormatterStep (whose state will be calculated lazily). */ public FormatterStep build() { return FormatterStep.createLazy(formatterName, this::get, stateToFormatter); @@ -85,10 +91,10 @@ protected void addPlatformRepo(P2Model model, String version) { /** Creates the state of the configuration. */ EquoBasedStepBuilder.State get() throws Exception { - var query = model(formatterVersion).query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW); + var query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW); var classpath = new ArrayList(); var mavenDeps = new ArrayList(); - mavenDeps.add("dev.equo.ide:solstice:1.0.0"); + mavenDeps.add("dev.equo.ide:solstice:1.0.3"); mavenDeps.add("com.diffplug.durian:durian-swt.os:4.1.1"); mavenDeps.addAll(query.getJarsOnMavenCentral()); classpath.addAll(mavenProvisioner.provisionWithTransitives(false, mavenDeps)); @@ -100,6 +106,29 @@ EquoBasedStepBuilder.State get() throws Exception { return new State(formatterVersion, jarState, FileSignature.signAsList(settingsFiles)); } + private P2Model createModelWithMirrors() { + P2Model model = model(formatterVersion); + if (p2Mirrors.isEmpty()) { + return model; + } + + ArrayList p2Repos = new ArrayList<>(model.getP2repo()); + p2Repos.replaceAll(url -> { + for (Map.Entry mirror : p2Mirrors.entrySet()) { + String prefix = mirror.getKey(); + if (url.startsWith(prefix)) { + return mirror.getValue() + url.substring(prefix.length()); + } + } + + throw new IllegalStateException("no mirror configured for P2 repository: " + url); + }); + + model.getP2repo().clear(); + model.getP2repo().addAll(p2Repos); + return model; + } + /** * State of Eclipse configuration items, providing functionality to derived information * based on the state. diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index ef7b1b8dae..0a0cabcca0 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,17 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* Support configuration of mirrors for P2 repositories ([#1629](https://github.com/diffplug/spotless/issues/1629)): + ``` + spotless { + java { + eclipse().withP2Mirrors(['https://download.eclipse.org/', 'https://some.internal.mirror/eclipse']) + } + } + ``` + 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/`. + The same configuration exists for `greclipse` and `eclipseCdt`. ### Changes * **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) * Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java index a08dc07d05..6a08946c48 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java @@ -17,6 +17,8 @@ import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull; +import java.util.Map; + import javax.inject.Inject; import org.gradle.api.Project; @@ -56,6 +58,12 @@ public void configFile(Object... configFiles) { builder.setPreferences(project.files(configFiles).getFiles()); replaceStep(builder.build()); } + + public EclipseConfig withP2Mirrors(Map mirrors) { + builder.setP2Mirrors(mirrors); + replaceStep(builder.build()); + return this; + } } @Override diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java index 2062946b32..d25433293d 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java @@ -17,6 +17,7 @@ import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull; +import java.util.Map; import java.util.Objects; import javax.inject.Inject; @@ -99,6 +100,12 @@ public void configFile(Object... configFiles) { builder.setPreferences(project.files(configFiles).getFiles()); extension.replaceStep(builder.build()); } + + public GrEclipseConfig withP2Mirrors(Map mirrors) { + builder.setP2Mirrors(mirrors); + extension.replaceStep(builder.build()); + return this; + } } /** If the user hasn't specified the files yet, we'll assume he/she means all of the groovy files. */ diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 8dd514ab37..e67bb6d076 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Objects; import javax.inject.Inject; @@ -232,6 +233,12 @@ public void configFile(Object... configFiles) { replaceStep(builder.build()); } + public EclipseConfig withP2Mirrors(Map mirrors) { + builder.setP2Mirrors(mirrors); + replaceStep(builder.build()); + return this; + } + } /** Removes newlines between type annotations and types. */