2121import java .nio .charset .StandardCharsets ;
2222import java .util .ArrayList ;
2323import java .util .List ;
24+ import java .util .Objects ;
2425
2526import javax .annotation .Nullable ;
2627
@@ -68,21 +69,20 @@ public FormatterStep create() {
6869
6970 private State createState () throws IOException , InterruptedException {
7071 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 );
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 ForeignExe exe ;
9495 // used for executing
95- final transient List <String > args ;
96+ private @ 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 () {
0 commit comments