Skip to content

Commit ca5c067

Browse files
committed
Use Provider API for Gradle properties
1 parent 88c83de commit ca5c067

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Changes
1414
* Bump default `ktfmt` version to latest `0.58` -> `0.59`. ([#2681](https://github.com/diffplug/spotless/pull/2681)
1515
### Fixed
16-
- palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685))
16+
* palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685))
17+
* Use Provider API for Gradle properties. ([#2718](https://github.com/diffplug/spotless/pull/2718)
1718
### Removed
1819
* **BREAKING** Drop support for older Ktlint versions. ([#2711](https://github.com/diffplug/spotless/pull/2711))
1920

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,11 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
630630

631631
FormatterStep createStep() {
632632
return builder.withYearModeLazy(() -> {
633-
if ("true".equals(spotless.project
634-
.findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
633+
if (spotless.project
634+
.getProviders()
635+
.gradleProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY())
636+
.map(Boolean::parseBoolean)
637+
.getOrElse(false)) {
635638
return YearMode.SET_FROM_GIT;
636639
} else {
637640
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import java.io.IOException;
2020
import java.nio.file.Files;
2121

22-
import javax.annotation.Nullable;
23-
2422
import org.gradle.api.Project;
23+
import org.gradle.api.provider.Provider;
2524

2625
import com.diffplug.common.base.Errors;
2726
import com.diffplug.common.io.ByteStreams;
@@ -31,19 +30,22 @@
3130

3231
final class IdeHook {
3332
static class State extends NoLambda.EqualityBasedOnSerialization {
34-
final @Nullable String path;
35-
final boolean useStdIn;
36-
final boolean useStdOut;
33+
final Provider<String> path;
34+
final Provider<Boolean> useStdIn;
35+
final Provider<Boolean> useStdOut;
3736

3837
State(Project project) {
39-
path = (String) project.findProperty(PROPERTY);
40-
if (path != null) {
41-
useStdIn = project.hasProperty(USE_STD_IN);
42-
useStdOut = project.hasProperty(USE_STD_OUT);
43-
} else {
44-
useStdIn = false;
45-
useStdOut = false;
46-
}
38+
this.path = project.getProviders().gradleProperty(PROPERTY);
39+
40+
this.useStdIn = path.map(path -> project.getProviders()
41+
.gradleProperty(USE_STD_IN)
42+
.isPresent())
43+
.orElse(false);
44+
45+
this.useStdOut = path.map(path -> project.getProviders()
46+
.gradleProperty(USE_STD_OUT)
47+
.isPresent())
48+
.orElse(false);
4749
}
4850
}
4951

@@ -56,7 +58,7 @@ private static void dumpIsClean() {
5658
}
5759

5860
static void performHook(SpotlessTaskImpl spotlessTask, IdeHook.State state) {
59-
File file = new File(state.path);
61+
File file = new File(state.path.get());
6062
if (!file.isAbsolute()) {
6163
System.err.println("Argument passed to " + PROPERTY + " must be an absolute path");
6264
return;
@@ -71,7 +73,7 @@ static void performHook(SpotlessTaskImpl spotlessTask, IdeHook.State state) {
7173
}
7274
}
7375
byte[] bytes;
74-
if (state.useStdIn) {
76+
if (state.useStdIn.get()) {
7577
bytes = ByteStreams.toByteArray(System.in);
7678
} else {
7779
bytes = Files.readAllBytes(file.toPath());
@@ -84,7 +86,7 @@ static void performHook(SpotlessTaskImpl spotlessTask, IdeHook.State state) {
8486
System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md");
8587
} else {
8688
System.err.println("IS DIRTY");
87-
if (state.useStdOut) {
89+
if (state.useStdOut.get()) {
8890
dirty.writeCanonicalTo(System.out);
8991
} else {
9092
dirty.writeCanonicalTo(file);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void apply(Project project) {
4242
+ "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation");
4343
}
4444
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
45-
if (project.hasProperty(SPOTLESS_MODERN)) {
45+
if (project.getProviders().gradleProperty(SPOTLESS_MODERN).isPresent()) {
4646
project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it.");
4747
}
4848
// make sure there's a `clean` and a `check`

0 commit comments

Comments
 (0)