|
1 | 1 | /*
|
2 |
| - * Copyright 2022-2023 DiffPlug |
| 2 | + * Copyright 2022-2024 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.io.Serializable;
|
21 | 21 | import java.nio.charset.StandardCharsets;
|
22 |
| -import java.util.Arrays; |
23 | 22 | import java.util.List;
|
24 | 23 | import java.util.Objects;
|
25 | 24 | import java.util.regex.Pattern;
|
@@ -64,34 +63,33 @@ public FormatterStep create() {
|
64 | 63 |
|
65 | 64 | private State createState() {
|
66 | 65 | String instructions = "https://docs.buf.build/installation";
|
67 |
| - ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version) |
| 66 | + ForeignExe exe = ForeignExe.nameAndVersion("buf", version) |
68 | 67 | .pathToExe(pathToExe)
|
69 | 68 | .versionRegex(Pattern.compile("(\\S*)"))
|
70 | 69 | .fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}");
|
71 |
| - return new State(this, exeAbsPath); |
| 70 | + return new State(this, exe); |
72 | 71 | }
|
73 | 72 |
|
74 | 73 | @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
|
75 | 74 | static class State implements Serializable {
|
76 | 75 | private static final long serialVersionUID = -1825662356883926318L;
|
77 | 76 | // used for up-to-date checks and caching
|
78 | 77 | final String version;
|
79 |
| - final transient ForeignExe exe; |
| 78 | + |
80 | 79 | // used for executing
|
81 |
| - private transient @Nullable List<String> args; |
| 80 | + private final transient ForeignExe exe; |
| 81 | + private transient String exeAbsPath; |
82 | 82 |
|
83 |
| - State(BufStep step, ForeignExe exeAbsPath) { |
| 83 | + State(BufStep step, ForeignExe exe) { |
84 | 84 | this.version = step.version;
|
85 |
| - this.exe = Objects.requireNonNull(exeAbsPath); |
| 85 | + this.exe = Objects.requireNonNull(exe); |
86 | 86 | }
|
87 | 87 |
|
88 | 88 | String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException {
|
89 |
| - if (args == null) { |
90 |
| - args = Arrays.asList( |
91 |
| - exe.confirmVersionAndGetAbsolutePath(), |
92 |
| - "format", |
93 |
| - file.getAbsolutePath()); |
| 89 | + if (exeAbsPath == null) { |
| 90 | + exeAbsPath = exe.confirmVersionAndGetAbsolutePath(); |
94 | 91 | }
|
| 92 | + List<String> args = List.of(exeAbsPath, "format", file.getAbsolutePath()); |
95 | 93 | return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
|
96 | 94 | }
|
97 | 95 |
|
|
0 commit comments