Skip to content

Commit

Permalink
Fix configuration cache issue with BufStep
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlebras committed Aug 8, 2023
1 parent b99ec3d commit 696a3c6
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.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.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -61,13 +62,12 @@ public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
}

private State createState() throws IOException, InterruptedException {
private State createState() {
String instructions = "https://docs.buf.build/installation";
String exeAbsPath = ForeignExe.nameAndVersion("buf", version)
.pathToExe(pathToExe)
.versionRegex(Pattern.compile("(\\S*)"))
.fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}")
.confirmVersionAndGetAbsolutePath();
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version)
.pathToExe(pathToExe)
.versionRegex(Pattern.compile("(\\S*)"))
.fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}");
return new State(this, exeAbsPath);
}

Expand All @@ -76,19 +76,23 @@ static class State implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
final transient ForeignExe exe;
// used for executing
final transient List<String> args;
private transient @Nullable List<String> args;

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

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()] = file.getAbsolutePath();
return runner.exec(input.getBytes(StandardCharsets.UTF_8), processArgs).assertExitZero(StandardCharsets.UTF_8);
if (args == null) {
args = Arrays.asList(
exe.confirmVersionAndGetAbsolutePath(),
"format",
file.getAbsolutePath());
}
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
}

FormatterFunc.Closeable toFunc() {
Expand Down

0 comments on commit 696a3c6

Please sign in to comment.