Skip to content

Commit 1025821

Browse files
committed
feat: add support for absolute binary path
1 parent a0bde46 commit 1025821

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Collectors;
2727
import java.util.stream.Stream;
2828

29+
import javax.annotation.CheckForNull;
2930
import javax.annotation.Nullable;
3031

3132
import org.slf4j.Logger;
@@ -56,15 +57,15 @@ public static FormatterStep create(boolean withDefaults,
5657
}
5758

5859
public static FormatterStep create(boolean withDefaults,
59-
@Nullable String binaryPath, @Nullable String configPath) {
60+
@Nullable String binaryPath, @Nullable String codeStyleSettingsPath) {
6061
return FormatterStep.createLazy("IDEA",
61-
() -> createState(withDefaults, binaryPath, configPath),
62+
() -> createState(withDefaults, binaryPath, codeStyleSettingsPath),
6263
state -> state);
6364
}
6465

6566
private static State createState(boolean withDefaults,
66-
@Nullable String binaryPath, @Nullable String configPath) {
67-
return new State(withDefaults, binaryPath, configPath);
67+
@Nullable String binaryPath, @Nullable String codeStyleSettingsPath) {
68+
return new State(withDefaults, binaryPath, codeStyleSettingsPath);
6869
}
6970

7071
private static class State
@@ -75,24 +76,25 @@ private static class State
7576

7677
private String binaryPath;
7778
@Nullable
78-
private String configPath;
79+
private String codeStyleSettingsPath;
7980
private boolean withDefaults;
8081

8182
private State(boolean withDefaults, @Nullable String binaryPath,
82-
@Nullable String configPath) {
83+
@Nullable String codeStyleSettingsPath) {
8384
this.withDefaults = withDefaults;
84-
this.configPath = configPath;
85+
this.codeStyleSettingsPath = codeStyleSettingsPath;
8586
this.binaryPath = Objects.requireNonNullElse(binaryPath, DEFAULT_IDEA);
8687
resolveFullBinaryPathAndCheckVersion();
8788
}
8889

8990
private void resolveFullBinaryPathAndCheckVersion() {
9091
var exe = ForeignExe
9192
.nameAndVersion(this.binaryPath, "IntelliJ IDEA")
93+
.pathToExe(pathToExe())
9294
.versionRegex(Pattern.compile("(IntelliJ IDEA) .*"))
9395
.fixCantFind(
9496
"IDEA executable cannot be found on your machine, "
95-
+ "please install it and put idea binary to PATH; or report the problem")
97+
+ "please install it and put idea binary to PATH, provide a valid path to the executable or report the problem")
9698
.fixWrongVersion("Provided binary is not IDEA, "
9799
+ "please check it and fix the problem; or report the problem");
98100
try {
@@ -105,6 +107,17 @@ private void resolveFullBinaryPathAndCheckVersion() {
105107
}
106108
}
107109

110+
@CheckForNull
111+
private String pathToExe() {
112+
if (binaryPath == null) {
113+
throw new IllegalStateException("binaryPath is not set");
114+
}
115+
if (new File(binaryPath).exists()) {
116+
return binaryPath;
117+
}
118+
return null; // search in PATH
119+
}
120+
108121
@Override
109122
public String applyWithFile(String unix, File file) throws Exception {
110123
List<String> params = getParams(file);
@@ -126,9 +139,9 @@ private List<String> getParams(File file) {
126139
if (withDefaults) {
127140
builder.add("-allowDefaults");
128141
}
129-
if (configPath != null) {
142+
if (codeStyleSettingsPath != null) {
130143
builder.add("-s");
131-
builder.add(configPath);
144+
builder.add(codeStyleSettingsPath);
132145
}
133146
builder.add(file.toString());
134147
return builder.build().collect(Collectors.toList());

0 commit comments

Comments
 (0)