21
21
import java .nio .charset .StandardCharsets ;
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
+ import java .util .Objects ;
24
25
25
26
import javax .annotation .Nullable ;
26
27
@@ -68,21 +69,20 @@ public FormatterStep create() {
68
69
69
70
private State createState () throws IOException , InterruptedException {
70
71
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 );
86
86
}
87
87
88
88
@ SuppressFBWarnings ("SE_TRANSIENT_FIELD_NOT_RESTORED" )
@@ -91,24 +91,28 @@ static class State implements Serializable {
91
91
// used for up-to-date checks and caching
92
92
final String version ;
93
93
final @ Nullable String style ;
94
+ final ForeignExe exe ;
94
95
// used for executing
95
- final transient List <String > args ;
96
+ private @ Nullable List <String > args ;
96
97
97
- State (ClangFormatStep step , String exeAbsPath ) {
98
+ State (ClangFormatStep step , ForeignExe pathToExe ) {
98
99
this .version = step .version ;
99
100
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 );
105
102
}
106
103
107
104
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 );
112
116
}
113
117
114
118
FormatterFunc .Closeable toFunc () {
0 commit comments