Skip to content

Commit d78b6e3

Browse files
authored
Lazily check for foreign exes in case they aren't needed (#1257)
2 parents 5ac8023 + ff1100c commit d78b6e3

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Clang and Black no longer break the build when the binary is unavailable, if they will not be run during that build ([#1257](https://github.com/diffplug/spotless/pull/1257)).
1315

1416
## [2.27.0] - 2022-06-30
1517
### Added

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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

@@ -75,14 +76,13 @@ private State createState() throws IOException, InterruptedException {
7576
"\n mac: brew install clang-format (TODO: how to specify version?)" +
7677
"\n linux: apt install clang-format (try clang-format-{version} with dropped minor versions)" +
7778
"\n github issue to handle this better: https://github.com/diffplug/spotless/issues/673";
78-
String exeAbsPath = ForeignExe.nameAndVersion("clang-format", version)
79+
final ForeignExe exe = ForeignExe.nameAndVersion("clang-format", version)
7980
.pathToExe(pathToExe)
8081
.fixCantFind(howToInstall)
8182
.fixWrongVersion(
8283
"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);
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 transient ForeignExe exe;
9495
// used for executing
95-
final transient List<String> args;
96+
private transient @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 transient ForeignExe exe;
7978
// used for executing
80-
final transient List<String> args;
79+
private transient @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

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Clang and Black no longer break the build when the binary is unavailable, if they will not be run during that build ([#1257](https://github.com/diffplug/spotless/pull/1257)).
68

79
## [6.8.0] - 2022-06-30
810
### Added

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Clang and Black no longer break the build when the binary is unavailable, if they will not be run during that build ([#1257](https://github.com/diffplug/spotless/pull/1257)).
68

79
## [2.23.0] - 2022-06-30
810
### Added

0 commit comments

Comments
 (0)