Skip to content

Commit 57214bf

Browse files
committed
feat: Supports P2 in predeclare
1 parent 8aee8ac commit 57214bf

File tree

5 files changed

+639
-11
lines changed

5 files changed

+639
-11
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ enum Policy {
5151
INDEPENDENT, ROOT_PROJECT, ROOT_BUILDSCRIPT;
5252

5353
public DedupingProvisioner dedupingProvisioner(Project project) {
54-
switch (this) {
55-
case ROOT_PROJECT:
56-
return new DedupingProvisioner(forProject(project));
57-
case ROOT_BUILDSCRIPT:
58-
return new DedupingProvisioner(forRootProjectBuildscript(project));
59-
case INDEPENDENT:
60-
default:
61-
throw Unhandled.enumException(this);
62-
}
54+
return switch (this) {
55+
case ROOT_PROJECT -> new DedupingProvisioner(forProject(project));
56+
case ROOT_BUILDSCRIPT -> new DedupingProvisioner(forRootProjectBuildscript(project));
57+
default -> throw Unhandled.enumException(this);
58+
};
59+
}
60+
61+
public DedupingP2Provisioner dedupingP2Provisioner(Project project) {
62+
return switch (this) {
63+
case ROOT_PROJECT, ROOT_BUILDSCRIPT -> new DedupingP2Provisioner(P2Provisioner.createDefault());
64+
default -> throw Unhandled.enumException(this);
65+
};
6366
}
6467
}
6568

@@ -222,6 +225,25 @@ public synchronized List<File> provisionP2Dependencies(
222225
return result;
223226
}
224227

228+
/** A child P2Provisioner which retrieves cached elements only. */
229+
final P2Provisioner cachedOnly = (modelWrapper, mavenProvisioner, cacheDirectory) -> {
230+
P2Request req = new P2Request(
231+
List.copyOf(modelWrapper.getP2Repos()),
232+
List.copyOf(modelWrapper.getInstallList()),
233+
Set.copyOf(modelWrapper.getFilterNames()),
234+
List.copyOf(modelWrapper.getPureMaven()),
235+
modelWrapper.isUseMavenCentral(),
236+
cacheDirectory);
237+
List<File> result;
238+
synchronized (cache) {
239+
result = cache.get(req);
240+
}
241+
if (result != null) {
242+
return result;
243+
}
244+
throw new GradleException("P2 dependencies not predeclared. Add Eclipse formatter configuration to the `spotlessPredeclare` block in the root project.");
245+
};
246+
225247
/**
226248
* Cache key capturing all P2Model state that affects query results.
227249
* Based on P2Model fields from equo-ide:

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionPredeclare.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2025 DiffPlug
2+
* Copyright 2021-2026 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.
@@ -29,6 +29,7 @@ public class SpotlessExtensionPredeclare extends SpotlessExtension {
2929
public SpotlessExtensionPredeclare(Project project, GradleProvisioner.Policy policy) {
3030
super(project);
3131
getRegisterDependenciesTask().getTaskService().get().predeclaredProvisioner = policy.dedupingProvisioner(project);
32+
getRegisterDependenciesTask().getTaskService().get().predeclaredP2Provisioner = policy.dedupingP2Provisioner(project);
3233
project.afterEvaluate(unused -> toSetup.forEach((name, formatExtension) -> {
3334
for (Action<FormatExtension> lazyAction : formatExtension.lazyActions) {
3435
lazyAction.execute(formatExtension);

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ P2Provisioner p2ProvisionerFor(SpotlessExtension spotless) {
8080
return predeclaredP2Provisioner;
8181
} else {
8282
if (predeclaredP2Provisioner != null) {
83-
return predeclaredP2Provisioner; // P2 doesn't have a cachedOnly variant
83+
return predeclaredP2Provisioner.cachedOnly;
8484
} else {
8585
return p2Provisioner.computeIfAbsent(spotless.project.getPath(),
8686
unused -> new GradleProvisioner.DedupingP2Provisioner(P2Provisioner.createDefault()));

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleProvisionerTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,47 @@ void identicalModelsDifferentInstancesUsesCache() throws IOException {
240240
assertThat(callCount.get()).as("Only called once").isEqualTo(1);
241241
}
242242

243+
@Test
244+
void cachedOnlyCacheHitReturnsResult() throws IOException {
245+
P2Provisioner underlying = mockP2Provisioner(new AtomicInteger(0));
246+
GradleProvisioner.DedupingP2Provisioner deduping = new GradleProvisioner.DedupingP2Provisioner(underlying);
247+
248+
P2ModelWrapper model = createMockModel(
249+
List.of("https://download.eclipse.org/eclipse/updates/4.26/"),
250+
List.of("org.eclipse.jdt.core"),
251+
Set.of(),
252+
List.of(),
253+
true,
254+
null);
255+
256+
// Populate cache
257+
deduping.provisionP2Dependencies(model, mockProvisioner(), null);
258+
259+
// cachedOnly should return cached result
260+
List<File> result = deduping.cachedOnly.provisionP2Dependencies(model, mockProvisioner(), null);
261+
262+
assertThat(result).isNotEmpty();
263+
}
264+
265+
@Test
266+
void cachedOnlyCacheMissThrowsException() {
267+
P2Provisioner underlying = mockP2Provisioner(new AtomicInteger(0));
268+
GradleProvisioner.DedupingP2Provisioner deduping = new GradleProvisioner.DedupingP2Provisioner(underlying);
269+
270+
P2ModelWrapper model = createMockModel(
271+
List.of("https://download.eclipse.org/eclipse/updates/4.26/"),
272+
List.of("org.eclipse.jdt.core"),
273+
Set.of(),
274+
List.of(),
275+
true,
276+
null);
277+
278+
// cachedOnly should throw when not cached
279+
assertThatThrownBy(() -> deduping.cachedOnly.provisionP2Dependencies(model, mockProvisioner(), null))
280+
.isInstanceOf(GradleException.class)
281+
.hasMessageContaining("spotlessPredeclare");
282+
}
283+
243284
private P2Provisioner mockP2Provisioner(AtomicInteger callCount) {
244285
return (modelWrapper, mavenProvisioner, cacheDirectory) -> {
245286
callCount.incrementAndGet();

0 commit comments

Comments
 (0)