Skip to content

Commit

Permalink
Support configuration of P2 mirrors (#1635 fixes #1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Mar 28, 2023
2 parents 19b25be + b636ebe commit 5749a73
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,7 @@ public abstract class EquoBasedStepBuilder {
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
private String formatterVersion;
private Iterable<File> settingsFiles = new ArrayList<>();
private Map<String, String> p2Mirrors = Map.of();

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

public void setP2Mirrors(Map<String, String> 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);
Expand All @@ -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<File>();
var mavenDeps = new ArrayList<String>();
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));
Expand All @@ -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<String> p2Repos = new ArrayList<>(model.getP2repo());
p2Repos.replaceAll(url -> {
for (Map.Entry<String, String> 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.
Expand Down
11 changes: 11 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 &lt; `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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +58,12 @@ public void configFile(Object... configFiles) {
builder.setPreferences(project.files(configFiles).getFiles());
replaceStep(builder.build());
}

public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {
builder.setP2Mirrors(mirrors);
replaceStep(builder.build());
return this;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import java.util.Map;
import java.util.Objects;

import javax.inject.Inject;
Expand Down Expand Up @@ -99,6 +100,12 @@ public void configFile(Object... configFiles) {
builder.setPreferences(project.files(configFiles).getFiles());
extension.replaceStep(builder.build());
}

public GrEclipseConfig withP2Mirrors(Map<String, String> 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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,6 +233,12 @@ public void configFile(Object... configFiles) {
replaceStep(builder.build());
}

public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {
builder.setP2Mirrors(mirrors);
replaceStep(builder.build());
return this;
}

}

/** Removes newlines between type annotations and types. */
Expand Down

0 comments on commit 5749a73

Please sign in to comment.