Skip to content

Commit 9556c8d

Browse files
authored
Leverage Maven local repository for p2 cache directory in Eclipse step (#2238)
2 parents 1b79196 + 482295d commit 9556c8d

File tree

7 files changed

+38
-2
lines changed

7 files changed

+38
-2
lines changed

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+
### Changed
14+
* Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
1315

1416
## [3.0.0.BETA2] - 2024-08-25
1517
### Changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.diffplug.spotless.SerializedFunction;
3838

3939
import dev.equo.solstice.NestedJars;
40+
import dev.equo.solstice.p2.CacheLocations;
4041
import dev.equo.solstice.p2.P2ClientCache;
4142
import dev.equo.solstice.p2.P2Model;
4243
import dev.equo.solstice.p2.P2QueryCache;
@@ -52,6 +53,7 @@ public abstract class EquoBasedStepBuilder {
5253
private String formatterVersion;
5354
private Iterable<File> settingsFiles = new ArrayList<>();
5455
private Map<String, String> p2Mirrors = Map.of();
56+
private File cacheDirectory;
5557

5658
/** Initialize valid default configuration, taking latest version */
5759
public EquoBasedStepBuilder(String formatterName, Provisioner mavenProvisioner, @Nullable String defaultVersion, SerializedFunction<State, FormatterFunc> stateToFormatter) {
@@ -77,6 +79,10 @@ public void setP2Mirrors(Collection<P2Mirror> p2Mirrors) {
7779
this.p2Mirrors = p2Mirrors.stream().collect(toMap(P2Mirror::getPrefix, P2Mirror::getUrl));
7880
}
7981

82+
public void setCacheDirectory(File cacheDirectory) {
83+
this.cacheDirectory = cacheDirectory;
84+
}
85+
8086
protected abstract P2Model model(String version);
8187

8288
protected void addPlatformRepo(P2Model model, String version) {
@@ -101,6 +107,9 @@ public FormatterStep build() {
101107
var roundtrippableState = new EquoStep(formatterVersion, FileSignature.promise(settingsFiles), JarState.promise(() -> {
102108
P2QueryResult query;
103109
try {
110+
if (null != cacheDirectory) {
111+
CacheLocations.override_p2data = cacheDirectory.toPath().resolve("dev/equo/p2-data").toFile();
112+
}
104113
query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
105114
} catch (Exception x) {
106115
throw new IOException("Failed to load " + formatterName + ": " + x, x);

plugin-maven/CHANGES.md

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

55
## [Unreleased]
6+
### Changed
7+
* Leverage local repository for Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
68

79
## [2.44.0.BETA2] - 2024-08-25
810
### Changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ private FileLocator getFileLocator() {
379379
private List<FormatterFactory> getFormatterFactories() {
380380
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go))
381381
.filter(Objects::nonNull)
382+
.map(factory -> factory.init(repositorySystemSession))
382383
.collect(toList());
383384
}
384385

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.apache.maven.plugins.annotations.Parameter;
3232
import org.apache.maven.project.MavenProject;
33+
import org.eclipse.aether.RepositorySystemSession;
3334

3435
import com.diffplug.common.collect.Sets;
3536
import com.diffplug.spotless.FormatExceptionPolicyStrict;
@@ -197,4 +198,9 @@ private static boolean formatterStepOverriden(FormatterStepFactory global, List<
197198
return allConfigured.stream()
198199
.anyMatch(configured -> configured.getClass() == global.getClass());
199200
}
201+
202+
public FormatterFactory init(RepositorySystemSession repositorySystemSession) {
203+
stepFactories.forEach(factory -> factory.init(repositorySystemSession));
204+
return this;
205+
}
200206
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepFactory.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,9 +15,15 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18+
import org.eclipse.aether.RepositorySystemSession;
19+
1820
import com.diffplug.spotless.FormatterStep;
1921

2022
public interface FormatterStepFactory {
2123

2224
FormatterStep newFormatterStep(FormatterStepConfig config);
25+
26+
default void init(RepositorySystemSession repositorySystemSession) {
27+
// nothing
28+
}
2329
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.apache.maven.plugins.annotations.Parameter;
24+
import org.eclipse.aether.RepositorySystemSession;
2425

2526
import com.diffplug.spotless.FormatterStep;
2627
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
@@ -40,6 +41,8 @@ public class Eclipse implements FormatterStepFactory {
4041
@Parameter
4142
private List<P2Mirror> p2Mirrors = new ArrayList<>();
4243

44+
private File cacheDirectory;
45+
4346
@Override
4447
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
4548
EquoBasedStepBuilder eclipseConfig = EclipseJdtFormatterStep.createBuilder(stepConfig.getProvisioner());
@@ -49,6 +52,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
4952
eclipseConfig.setPreferences(Arrays.asList(settingsFile));
5053
}
5154
eclipseConfig.setP2Mirrors(p2Mirrors);
55+
if (null != cacheDirectory) {
56+
eclipseConfig.setCacheDirectory(cacheDirectory);
57+
}
5258
return eclipseConfig.build();
5359
}
60+
61+
public void init(RepositorySystemSession repositorySystemSession) {
62+
this.cacheDirectory = repositorySystemSession.getLocalRepository().getBasedir();
63+
}
5464
}

0 commit comments

Comments
 (0)