Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily check for foreign exes which may not be necessary for the curr… #1257

Merged
merged 6 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `diktat` version to latest `1.1.0` -> `1.2.1` ([#1246](https://github.com/diffplug/spotless/pull/1246))
* Minimum supported version also bumped to `1.2.1` (diktat is based on ktlint and has the same backward compatibility issues).
* Bump default `ktfmt` version to latest `0.37` -> `0.39` ([#1240](https://github.com/diffplug/spotless/pull/1240))
* 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))
bh-tt marked this conversation as resolved.
Show resolved Hide resolved

## [2.26.2] - 2022-06-11
### Fixed
Expand Down
56 changes: 30 additions & 26 deletions lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -68,21 +69,20 @@ public FormatterStep create() {

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

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
Expand All @@ -91,24 +91,28 @@ static class State implements Serializable {
// used for up-to-date checks and caching
final String version;
final @Nullable String style;
final ForeignExe exe;
bh-tt marked this conversation as resolved.
Show resolved Hide resolved
// used for executing
final transient List<String> args;
private @Nullable List<String> args;
bh-tt marked this conversation as resolved.
Show resolved Hide resolved

State(ClangFormatStep step, String exeAbsPath) {
State(ClangFormatStep step, ForeignExe pathToExe) {
this.version = step.version;
this.style = step.style;
args = new ArrayList<>(2);
args.add(exeAbsPath);
if (style != null) {
args.add("--style=" + style);
}
this.exe = Objects.requireNonNull(pathToExe);
}

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

FormatterFunc.Closeable toFunc() {
Expand Down
18 changes: 10 additions & 8 deletions lib/src/main/java/com/diffplug/spotless/python/BlackStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

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

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

Expand All @@ -76,15 +74,19 @@ static class State implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
final ForeignExe exe;
bh-tt marked this conversation as resolved.
Show resolved Hide resolved
// used for executing
final transient List<String> args;
private @Nullable String[] args;
bh-tt marked this conversation as resolved.
Show resolved Hide resolved

State(BlackStep step, String exeAbsPath) {
State(BlackStep step, ForeignExe exeAbsPath) {
this.version = step.version;
this.args = Arrays.asList(exeAbsPath, "-");
this.exe = Objects.requireNonNull(exeAbsPath);
}

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

Expand Down