Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage Maven local repository for p2 cache directory in Eclipse step #2238

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
### Changed
* Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))

## [3.0.0.BETA2] - 2024-08-25
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.diffplug.spotless.SerializedFunction;

import dev.equo.solstice.NestedJars;
import dev.equo.solstice.p2.CacheLocations;
import dev.equo.solstice.p2.P2ClientCache;
import dev.equo.solstice.p2.P2Model;
import dev.equo.solstice.p2.P2QueryCache;
Expand All @@ -52,6 +53,7 @@ public abstract class EquoBasedStepBuilder {
private String formatterVersion;
private Iterable<File> settingsFiles = new ArrayList<>();
private Map<String, String> p2Mirrors = Map.of();
private File cacheDirectory;

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

public void setCacheDirectory(File cacheDirectory) {
this.cacheDirectory = cacheDirectory;
}

protected abstract P2Model model(String version);

protected void addPlatformRepo(P2Model model, String version) {
Expand All @@ -101,6 +107,9 @@ public FormatterStep build() {
var roundtrippableState = new EquoStep(formatterVersion, FileSignature.promise(settingsFiles), JarState.promise(() -> {
P2QueryResult query;
try {
if (null != cacheDirectory) {
CacheLocations.override_p2data = cacheDirectory.toPath().resolve("dev/equo/p2-data").toFile();
}
query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
} catch (Exception x) {
throw new IOException("Failed to load " + formatterName + ": " + x, x);
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.44.0.BETA2] - 2024-08-25
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ private FileLocator getFileLocator() {
private List<FormatterFactory> getFormatterFactories() {
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go))
.filter(Objects::nonNull)
.map(factory -> factory.init(repositorySystemSession))
.collect(toList());
}

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

import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;

import com.diffplug.common.collect.Sets;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
Expand Down Expand Up @@ -197,4 +198,9 @@ private static boolean formatterStepOverriden(FormatterStepFactory global, List<
return allConfigured.stream()
.anyMatch(configured -> configured.getClass() == global.getClass());
}

public FormatterFactory init(RepositorySystemSession repositorySystemSession) {
stepFactories.forEach(factory -> factory.init(repositorySystemSession));
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,15 @@
*/
package com.diffplug.spotless.maven;

import org.eclipse.aether.RepositorySystemSession;

import com.diffplug.spotless.FormatterStep;

public interface FormatterStepFactory {

FormatterStep newFormatterStep(FormatterStepConfig config);

default void init(RepositorySystemSession repositorySystemSession) {
// nothing
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.util.List;

import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.RepositorySystemSession;

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

private File cacheDirectory;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
EquoBasedStepBuilder eclipseConfig = EclipseJdtFormatterStep.createBuilder(stepConfig.getProvisioner());
Expand All @@ -49,6 +52,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
eclipseConfig.setPreferences(Arrays.asList(settingsFile));
}
eclipseConfig.setP2Mirrors(p2Mirrors);
if (null != cacheDirectory) {
eclipseConfig.setCacheDirectory(cacheDirectory);
}
return eclipseConfig.build();
}

public void init(RepositorySystemSession repositorySystemSession) {
this.cacheDirectory = repositorySystemSession.getLocalRepository().getBasedir();
}
}
Loading