Skip to content

Commit 9123527

Browse files
committed
Create State object
This object is now resides in IdeaStep
1 parent bf5918c commit 9123527

File tree

2 files changed

+91
-121
lines changed

2 files changed

+91
-121
lines changed

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

Lines changed: 0 additions & 112 deletions
This file was deleted.

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

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,31 @@
1515
*/
1616
package com.diffplug.spotless.java;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.Serializable;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.util.List;
24+
import java.util.Objects;
25+
import java.util.regex.Pattern;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
28+
1829
import javax.annotation.Nullable;
1930

31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import com.diffplug.spotless.ForeignExe;
35+
import com.diffplug.spotless.FormatterFunc;
2036
import com.diffplug.spotless.FormatterStep;
37+
import com.diffplug.spotless.ProcessRunner;
2138

2239
public final class IdeaStep {
2340

41+
private static final Logger LOGGER = LoggerFactory.getLogger(IdeaStep.class);
42+
2443
private IdeaStep() {}
2544

2645
public static FormatterStep create() {
@@ -38,18 +57,81 @@ public static FormatterStep create(boolean withDefaults,
3857

3958
public static FormatterStep create(boolean withDefaults,
4059
@Nullable String binaryPath, @Nullable String configPath) {
41-
IdeaFormatterFunc formatterFunc = getFormatterFunc(withDefaults, binaryPath, configPath);
42-
// TODO: make it lazy
43-
return FormatterStep.createNeverUpToDate("IDEA", formatterFunc);
60+
return FormatterStep.createLazy("IDEA",
61+
() -> createState(withDefaults, binaryPath, configPath),
62+
state -> state);
4463
}
4564

46-
private static IdeaFormatterFunc getFormatterFunc(boolean withDefaults,
65+
private static State createState(boolean withDefaults,
4766
@Nullable String binaryPath, @Nullable String configPath) {
48-
if (withDefaults) {
49-
return IdeaFormatterFunc
50-
.allowingDefaultsWithCustomBinary(binaryPath, configPath);
51-
}
52-
return IdeaFormatterFunc.noDefaultsWithCustomBinary(binaryPath, configPath);
67+
return new State(withDefaults, binaryPath, configPath);
5368
}
5469

70+
private static class State
71+
implements FormatterFunc.NeedsFile, Serializable {
72+
73+
private static final long serialVersionUID = -1825662355363926318L;
74+
private static final String DEFAULT_IDEA = "idea";
75+
76+
private String binaryPath;
77+
@Nullable
78+
private String configPath;
79+
private boolean withDefaults;
80+
81+
private State(boolean withDefaults, @Nullable String binaryPath,
82+
@Nullable String configPath) {
83+
this.withDefaults = withDefaults;
84+
this.configPath = configPath;
85+
this.binaryPath = Objects.requireNonNullElse(binaryPath, DEFAULT_IDEA);
86+
resolveFullBinaryPathAndCheckVersion();
87+
}
88+
89+
private void resolveFullBinaryPathAndCheckVersion() {
90+
var exe = ForeignExe
91+
.nameAndVersion(this.binaryPath, "IntelliJ IDEA")
92+
.versionRegex(Pattern.compile("(IntelliJ IDEA) .*"))
93+
.fixCantFind(
94+
"IDEA executable cannot be found on your machine, "
95+
+ "please install it and put idea binary to PATH; or report the problem")
96+
.fixWrongVersion("Provided binary is not IDEA, "
97+
+ "please check it and fix the problem; or report the problem");
98+
try {
99+
this.binaryPath = exe.confirmVersionAndGetAbsolutePath();
100+
} catch (IOException e) {
101+
throw new IllegalArgumentException("binary cannot be found", e);
102+
} catch (InterruptedException e) {
103+
throw new IllegalArgumentException(
104+
"binary cannot be found, process was interrupted", e);
105+
}
106+
}
107+
108+
@Override
109+
public String applyWithFile(String unix, File file) throws Exception {
110+
List<String> params = getParams(file);
111+
112+
try (ProcessRunner runner = new ProcessRunner()) {
113+
var result = runner.exec(params);
114+
115+
LOGGER.debug("command finished with stdout: {}",
116+
result.assertExitZero(StandardCharsets.UTF_8));
117+
118+
return Files.readString(file.toPath());
119+
}
120+
}
121+
122+
private List<String> getParams(File file) {
123+
var builder = Stream.<String> builder();
124+
builder.add(binaryPath);
125+
builder.add("format");
126+
if (withDefaults) {
127+
builder.add("-allowDefaults");
128+
}
129+
if (configPath != null) {
130+
builder.add("-s");
131+
builder.add(configPath);
132+
}
133+
builder.add(file.toString());
134+
return builder.build().collect(Collectors.toList());
135+
}
136+
}
55137
}

0 commit comments

Comments
 (0)