Skip to content

Commit 599ad51

Browse files
committed
refactor: use builder for idea-step creation
1 parent 39d1653 commit 599ad51

File tree

4 files changed

+92
-39
lines changed

4 files changed

+92
-39
lines changed

lib/src/main/java/com/diffplug/spotless/generic/IdeaStep.java

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.nio.file.Files;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.Objects;
2526
import java.util.regex.Pattern;
2627
import java.util.stream.Collectors;
2728
import java.util.stream.Stream;
2829

2930
import javax.annotation.CheckForNull;
31+
import javax.annotation.Nonnull;
3032
import javax.annotation.Nullable;
3133

3234
import org.slf4j.Logger;
@@ -40,51 +42,70 @@
4042

4143
public final class IdeaStep {
4244

45+
public static final String NAME = "IDEA";
46+
4347
private static final Logger LOGGER = LoggerFactory.getLogger(IdeaStep.class);
4448

4549
private IdeaStep() {}
4650

47-
public static FormatterStep create() {
48-
return create(true);
51+
public static IdeaStepBuilder create() {
52+
return new IdeaStepBuilder();
4953
}
5054

51-
public static FormatterStep create(boolean withDefaults) {
52-
return create(withDefaults, null);
55+
private static FormatterStep create(@Nonnull IdeaStepBuilder builder) {
56+
Objects.requireNonNull(builder);
57+
return FormatterStep.createLazy(NAME,
58+
() -> createState(builder),
59+
state -> state);
5360
}
5461

55-
public static FormatterStep create(boolean withDefaults,
56-
@Nullable String binaryPath) {
57-
return create(withDefaults, binaryPath, null);
62+
private static State createState(@Nonnull IdeaStepBuilder builder) {
63+
return new State(Objects.requireNonNull(builder));
5864
}
5965

60-
public static FormatterStep create(boolean withDefaults,
61-
@Nullable String binaryPath, @Nullable String codeStyleSettingsPath) {
62-
return FormatterStep.createLazy("IDEA",
63-
() -> createState(withDefaults, binaryPath, codeStyleSettingsPath),
64-
state -> state);
65-
}
66+
public static final class IdeaStepBuilder {
67+
private static final String DEFAULT_IDEA = "idea";
68+
69+
private boolean useDefaults = true;
70+
@Nonnull
71+
private String binaryPath = DEFAULT_IDEA;
72+
@Nullable
73+
private String codeStyleSettingsPath;
74+
75+
public IdeaStepBuilder setUseDefaults(boolean useDefaults) {
76+
this.useDefaults = useDefaults;
77+
return this;
78+
}
79+
80+
public IdeaStepBuilder setBinaryPath(@Nonnull String binaryPath) {
81+
this.binaryPath = Objects.requireNonNull(binaryPath);
82+
return this;
83+
}
84+
85+
public IdeaStepBuilder setCodeStyleSettingsPath(@Nullable String codeStyleSettingsPath) {
86+
this.codeStyleSettingsPath = codeStyleSettingsPath;
87+
return this;
88+
}
6689

67-
private static State createState(boolean withDefaults,
68-
@Nullable String binaryPath, @Nullable String codeStyleSettingsPath) {
69-
return new State(withDefaults, binaryPath, codeStyleSettingsPath);
90+
public FormatterStep build() {
91+
return create(this);
92+
}
7093
}
7194

7295
private static class State
7396
implements FormatterFunc.NeedsFile, Serializable {
7497

7598
private static final long serialVersionUID = -1825662355363926318L;
76-
private static final String DEFAULT_IDEA = "idea";
7799

78100
private String binaryPath;
79101
@Nullable
80102
private String codeStyleSettingsPath;
81103
private boolean withDefaults;
82104

83-
private State(boolean withDefaults, @Nullable String binaryPath,
84-
@Nullable String codeStyleSettingsPath) {
85-
this.withDefaults = withDefaults;
86-
this.codeStyleSettingsPath = codeStyleSettingsPath;
87-
this.binaryPath = Objects.requireNonNullElse(binaryPath, DEFAULT_IDEA);
105+
private State(@Nonnull IdeaStepBuilder builder) {
106+
this.withDefaults = builder.useDefaults;
107+
this.codeStyleSettingsPath = builder.codeStyleSettingsPath;
108+
this.binaryPath = builder.binaryPath;
88109
resolveFullBinaryPathAndCheckVersion();
89110
}
90111

@@ -113,10 +134,38 @@ private String pathToExe() {
113134
if (binaryPath == null) {
114135
throw new IllegalStateException("binaryPath is not set");
115136
}
116-
if (new File(binaryPath).exists()) {
117-
return binaryPath;
137+
return macOsFix(binaryPath);
138+
}
139+
140+
@CheckForNull
141+
private static String macOsFix(String binaryPath) {
142+
if (!isMacOs()) {
143+
if (new File(binaryPath).exists()) {
144+
return binaryPath;
145+
}
146+
return null; // search in PATH
147+
}
148+
// on macOS, the binary is located in the .app bundle which might be invisible to the user
149+
// we try need to append the path to the binary
150+
File binary = new File(binaryPath);
151+
if (!binary.exists()) {
152+
// maybe it is bundle path without .app? (might be hidden by os)
153+
binary = new File(binaryPath + ".app");
154+
if (!binary.exists()) {
155+
return binaryPath; // fallback: do nothing
156+
}
157+
}
158+
if (binaryPath.endsWith(".app") || binary.isDirectory()) {
159+
binary = new File(binary, "Contents/MacOS/idea");
118160
}
119-
return null; // search in PATH
161+
if (binary.isFile() && binary.canExecute()) {
162+
return binary.getPath();
163+
}
164+
return binaryPath; // fallback: do nothing
165+
}
166+
167+
private static boolean isMacOs() {
168+
return System.getProperty("os.name").toLowerCase().contains("mac");
120169
}
121170

122171
@Override

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,35 +966,35 @@ public IdeaConfig idea() {
966966
}
967967

968968
public class IdeaConfig {
969-
private String binaryPath;
970-
private String configPath;
971-
private boolean withDefaults = false;
969+
private final IdeaStep.IdeaStepBuilder builder;
972970

973971
IdeaConfig() {
972+
this.builder = IdeaStep.create();
974973
addStep(createStep());
975974
}
976975

977976
private FormatterStep createStep() {
978-
return IdeaStep.create(withDefaults, binaryPath, configPath);
977+
return builder.build();
979978
}
980979

981980
public IdeaConfig binaryPath(String binaryPath) {
982981
requireNonNull(binaryPath);
983-
this.binaryPath = binaryPath;
982+
builder.setBinaryPath(binaryPath);
984983
replaceStep(createStep());
985984
return this;
986985
}
987986

987+
// TODO (simschla, 11.05.2025): rename
988988
public IdeaConfig configPath(String configPath) {
989989
requireNonNull(configPath);
990-
this.configPath = configPath;
990+
builder.setCodeStyleSettingsPath(configPath);
991991
replaceStep(createStep());
992992
return this;
993993
}
994994

995995
public IdeaConfig withDefaults(Boolean withDefaults) {
996996
requireNonNull(withDefaults);
997-
this.withDefaults = withDefaults;
997+
builder.setUseDefaults(withDefaults);
998998
replaceStep(createStep());
999999
return this;
10001000
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Idea.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public class Idea implements FormatterStepFactory {
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38-
return IdeaStep.create(withDefaults, binaryPath, configPath);
38+
return IdeaStep.create()
39+
.setUseDefaults(withDefaults)
40+
.setCodeStyleSettingsPath(configPath)
41+
.setBinaryPath(binaryPath)
42+
.build();
3943
}
4044
}

testlib/src/test/java/com/diffplug/spotless/generic/IdeaStepTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class IdeaStepTest extends ResourceHarness {
3131

3232
@Test
3333
void name() throws Exception {
34-
FormatterStep step = IdeaStep.create(true, "idea");
34+
FormatterStep step = IdeaStep.create().setUseDefaults(true).setBinaryPath("idea").build();
3535

3636
String name = step.getName();
3737

@@ -43,7 +43,7 @@ void notFormattings() throws Exception {
4343
File cleanFile = newFile("clean.java");
4444
String cleanJava = ResourceHarness.getTestResource("java/idea/full.clean.java");
4545
Files.write(cleanJava, cleanFile, StandardCharsets.UTF_8);
46-
FormatterStep step = IdeaStep.create(true, "idea");
46+
FormatterStep step = IdeaStep.create().setUseDefaults(true).setBinaryPath("/Users/simschla/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea").build();
4747

4848
var result = step.format(cleanJava, cleanFile);
4949

@@ -56,7 +56,7 @@ void formattings() throws Exception {
5656
File dirtyFile = newFile("dirty.java");
5757
String dirtyJava = ResourceHarness.getTestResource("java/idea/full.dirty.java");
5858
Files.write(dirtyJava, dirtyFile, StandardCharsets.UTF_8);
59-
FormatterStep step = IdeaStep.create(true, "idea");
59+
FormatterStep step = IdeaStep.create().setUseDefaults(true).setBinaryPath("/Users/simschla/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea").build();
6060

6161
var result = step.format(dirtyJava, dirtyFile);
6262

@@ -69,7 +69,7 @@ void formattingsWorkWithDefaultParameters() throws Exception {
6969
File dirtyFile = newFile("dirty.java");
7070
String dirtyJava = ResourceHarness.getTestResource("java/idea/full.dirty.java");
7171
Files.write(dirtyJava, dirtyFile, StandardCharsets.UTF_8);
72-
FormatterStep step = IdeaStep.create();
72+
FormatterStep step = IdeaStep.create().build();
7373

7474
var result = step.format(dirtyJava, dirtyFile);
7575

@@ -82,7 +82,7 @@ void formattingsWithOutDefaultDoesNothing() throws Exception {
8282
File dirtyFile = newFile("dirty.java");
8383
String dirtyJava = ResourceHarness.getTestResource("java/idea/full.dirty.java");
8484
Files.write(dirtyJava, dirtyFile, StandardCharsets.UTF_8);
85-
FormatterStep step = IdeaStep.create(false, "idea");
85+
FormatterStep step = IdeaStep.create().setUseDefaults(false).setBinaryPath("idea").build();
8686

8787
var result = step.format(dirtyJava, dirtyFile);
8888

@@ -95,7 +95,7 @@ void configureFile() throws Exception {
9595
File cleanFile = newFile("clean.java");
9696
String cleanJava = ResourceHarness.getTestResource("java/idea/full.clean.java");
9797
Files.write(cleanJava, cleanFile, StandardCharsets.UTF_8);
98-
FormatterStep step = IdeaStep.create(true, "idea");
98+
FormatterStep step = IdeaStep.create().setUseDefaults(true).setBinaryPath("idea").build();
9999

100100
var result = step.format(cleanJava, cleanFile);
101101

0 commit comments

Comments
 (0)