Skip to content

Commit 1cbe523

Browse files
committed
Lazily check for foreign exes which may not be necessary for the current build
1 parent 7e484de commit 1cbe523

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.Objects;
2425

2526
import javax.annotation.Nullable;
2627

@@ -68,21 +69,20 @@ public FormatterStep create() {
6869

6970
private State createState() throws IOException, InterruptedException {
7071
String howToInstall = "" +
71-
"You can download clang-format from https://releases.llvm.org and " +
72-
"then point Spotless to it with {@code pathToExe('/path/to/clang-format')} " +
73-
"or you can use your platform's package manager:" +
74-
"\n win: choco install llvm --version {version} (try dropping version if it fails)" +
75-
"\n mac: brew install clang-format (TODO: how to specify version?)" +
76-
"\n linux: apt install clang-format (try clang-format-{version} with dropped minor versions)" +
77-
"\n github issue to handle this better: https://github.com/diffplug/spotless/issues/673";
78-
String exeAbsPath = ForeignExe.nameAndVersion("clang-format", version)
79-
.pathToExe(pathToExe)
80-
.fixCantFind(howToInstall)
81-
.fixWrongVersion(
82-
"You can tell Spotless to use the version you already have with {@code clangFormat('{versionFound}')}" +
83-
"or you can download the currently specified version, {version}.\n" + howToInstall)
84-
.confirmVersionAndGetAbsolutePath();
85-
return new State(this, exeAbsPath);
72+
"You can download clang-format from https://releases.llvm.org and " +
73+
"then point Spotless to it with {@code pathToExe('/path/to/clang-format')} " +
74+
"or you can use your platform's package manager:" +
75+
"\n win: choco install llvm --version {version} (try dropping version if it fails)" +
76+
"\n mac: brew install clang-format (TODO: how to specify version?)" +
77+
"\n linux: apt install clang-format (try clang-format-{version} with dropped minor versions)" +
78+
"\n github issue to handle this better: https://github.com/diffplug/spotless/issues/673";
79+
final ForeignExe exe = ForeignExe.nameAndVersion("clang-format", version)
80+
.pathToExe(pathToExe)
81+
.fixCantFind(howToInstall)
82+
.fixWrongVersion(
83+
"You can tell Spotless to use the version you already have with {@code clangFormat('{versionFound}')}" +
84+
"or you can download the currently specified version, {version}.\n" + howToInstall);
85+
return new State(this, exe);
8686
}
8787

8888
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@@ -91,24 +91,28 @@ static class State implements Serializable {
9191
// used for up-to-date checks and caching
9292
final String version;
9393
final @Nullable String style;
94+
final ForeignExe exe;
9495
// used for executing
95-
final transient List<String> args;
96+
private @Nullable List<String> args;
9697

97-
State(ClangFormatStep step, String exeAbsPath) {
98+
State(ClangFormatStep step, ForeignExe pathToExe) {
9899
this.version = step.version;
99100
this.style = step.style;
100-
args = new ArrayList<>(2);
101-
args.add(exeAbsPath);
102-
if (style != null) {
103-
args.add("--style=" + style);
104-
}
101+
this.exe = Objects.requireNonNull(pathToExe);
105102
}
106103

107104
String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException {
108-
String[] processArgs = args.toArray(new String[args.size() + 1]);
109-
// add an argument to the end
110-
processArgs[args.size()] = "--assume-filename=" + file.getName();
111-
return runner.exec(input.getBytes(StandardCharsets.UTF_8), processArgs).assertExitZero(StandardCharsets.UTF_8);
105+
if (args == null) {
106+
final List<String> tmpArgs = new ArrayList<>();
107+
tmpArgs.add(exe.confirmVersionAndGetAbsolutePath());
108+
if (style != null) {
109+
tmpArgs.add("--style="+ style);
110+
}
111+
args = tmpArgs;
112+
}
113+
final String[] processArgs = args.toArray(new String[args.size() + 1]);
114+
processArgs[processArgs.length - 1] = "--assume-filename=" + file.getName();
115+
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
112116
}
113117

114118
FormatterFunc.Closeable toFunc() {

lib/src/main/java/com/diffplug/spotless/python/BlackStep.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.Serializable;
2020
import java.nio.charset.StandardCharsets;
21-
import java.util.Arrays;
22-
import java.util.List;
21+
import java.util.Objects;
2322
import java.util.regex.Pattern;
2423

2524
import javax.annotation.Nullable;
@@ -62,12 +61,11 @@ public FormatterStep create() {
6261

6362
private State createState() throws IOException, InterruptedException {
6463
String trackingIssue = "\n github issue to handle this better: https://github.com/diffplug/spotless/issues/674";
65-
String exeAbsPath = ForeignExe.nameAndVersion("black", version)
64+
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("black", version)
6665
.pathToExe(pathToExe)
6766
.versionRegex(Pattern.compile("(?:black, version|black,|version) (\\S*)"))
6867
.fixCantFind("Try running {@code pip install black=={version}}, or else tell Spotless where it is with {@code black().pathToExe('path/to/executable')}" + trackingIssue)
69-
.fixWrongVersion("Try running {@code pip install --force-reinstall black=={version}}, or else specify {@code black('{versionFound}')} to Spotless" + trackingIssue)
70-
.confirmVersionAndGetAbsolutePath();
68+
.fixWrongVersion("Try running {@code pip install --force-reinstall black=={version}}, or else specify {@code black('{versionFound}')} to Spotless" + trackingIssue);
7169
return new State(this, exeAbsPath);
7270
}
7371

@@ -76,15 +74,19 @@ static class State implements Serializable {
7674
private static final long serialVersionUID = -1825662356883926318L;
7775
// used for up-to-date checks and caching
7876
final String version;
77+
final ForeignExe exe;
7978
// used for executing
80-
final transient List<String> args;
79+
private @Nullable String[] args;
8180

82-
State(BlackStep step, String exeAbsPath) {
81+
State(BlackStep step, ForeignExe exeAbsPath) {
8382
this.version = step.version;
84-
this.args = Arrays.asList(exeAbsPath, "-");
83+
this.exe = Objects.requireNonNull(exeAbsPath);
8584
}
8685

8786
String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
87+
if (args == null) {
88+
args = new String[] {exe.confirmVersionAndGetAbsolutePath(), "-"};
89+
}
8890
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
8991
}
9092

0 commit comments

Comments
 (0)