15
15
*/
16
16
package com .diffplug .spotless .java ;
17
17
18
+ import java .io .File ;
19
+ import java .io .IOException ;
20
+ import java .io .Serializable ;
21
+ import java .nio .charset .StandardCharsets ;
22
+ import java .nio .file .Files ;
23
+ import java .util .List ;
24
+ import java .util .Objects ;
25
+ import java .util .regex .Pattern ;
26
+ import java .util .stream .Collectors ;
27
+ import java .util .stream .Stream ;
28
+
18
29
import javax .annotation .Nullable ;
19
30
31
+ import org .slf4j .Logger ;
32
+ import org .slf4j .LoggerFactory ;
33
+
34
+ import com .diffplug .spotless .ForeignExe ;
35
+ import com .diffplug .spotless .FormatterFunc ;
20
36
import com .diffplug .spotless .FormatterStep ;
37
+ import com .diffplug .spotless .ProcessRunner ;
21
38
22
39
public final class IdeaStep {
23
40
41
+ private static final Logger LOGGER = LoggerFactory .getLogger (IdeaStep .class );
42
+
24
43
private IdeaStep () {}
25
44
26
45
public static FormatterStep create () {
@@ -38,18 +57,81 @@ public static FormatterStep create(boolean withDefaults,
38
57
39
58
public static FormatterStep create (boolean withDefaults ,
40
59
@ Nullable String binaryPath , @ Nullable String configPath ) {
41
- IdeaFormatterFunc formatterFunc = getFormatterFunc ( withDefaults , binaryPath , configPath );
42
- // TODO: make it lazy
43
- return FormatterStep . createNeverUpToDate ( "IDEA" , formatterFunc );
60
+ return FormatterStep . createLazy ( "IDEA" ,
61
+ () -> createState ( withDefaults , binaryPath , configPath ),
62
+ state -> state );
44
63
}
45
64
46
- private static IdeaFormatterFunc getFormatterFunc (boolean withDefaults ,
65
+ private static State createState (boolean withDefaults ,
47
66
@ Nullable String binaryPath , @ Nullable String configPath ) {
48
- if (withDefaults ) {
49
- return IdeaFormatterFunc
50
- .allowingDefaultsWithCustomBinary (binaryPath , configPath );
51
- }
52
- return IdeaFormatterFunc .noDefaultsWithCustomBinary (binaryPath , configPath );
67
+ return new State (withDefaults , binaryPath , configPath );
53
68
}
54
69
70
+ private static class State
71
+ implements FormatterFunc .NeedsFile , Serializable {
72
+
73
+ private static final long serialVersionUID = -1825662355363926318L ;
74
+ private static final String DEFAULT_IDEA = "idea" ;
75
+
76
+ private String binaryPath ;
77
+ @ Nullable
78
+ private String configPath ;
79
+ private boolean withDefaults ;
80
+
81
+ private State (boolean withDefaults , @ Nullable String binaryPath ,
82
+ @ Nullable String configPath ) {
83
+ this .withDefaults = withDefaults ;
84
+ this .configPath = configPath ;
85
+ this .binaryPath = Objects .requireNonNullElse (binaryPath , DEFAULT_IDEA );
86
+ resolveFullBinaryPathAndCheckVersion ();
87
+ }
88
+
89
+ private void resolveFullBinaryPathAndCheckVersion () {
90
+ var exe = ForeignExe
91
+ .nameAndVersion (this .binaryPath , "IntelliJ IDEA" )
92
+ .versionRegex (Pattern .compile ("(IntelliJ IDEA) .*" ))
93
+ .fixCantFind (
94
+ "IDEA executable cannot be found on your machine, "
95
+ + "please install it and put idea binary to PATH; or report the problem" )
96
+ .fixWrongVersion ("Provided binary is not IDEA, "
97
+ + "please check it and fix the problem; or report the problem" );
98
+ try {
99
+ this .binaryPath = exe .confirmVersionAndGetAbsolutePath ();
100
+ } catch (IOException e ) {
101
+ throw new IllegalArgumentException ("binary cannot be found" , e );
102
+ } catch (InterruptedException e ) {
103
+ throw new IllegalArgumentException (
104
+ "binary cannot be found, process was interrupted" , e );
105
+ }
106
+ }
107
+
108
+ @ Override
109
+ public String applyWithFile (String unix , File file ) throws Exception {
110
+ List <String > params = getParams (file );
111
+
112
+ try (ProcessRunner runner = new ProcessRunner ()) {
113
+ var result = runner .exec (params );
114
+
115
+ LOGGER .debug ("command finished with stdout: {}" ,
116
+ result .assertExitZero (StandardCharsets .UTF_8 ));
117
+
118
+ return Files .readString (file .toPath ());
119
+ }
120
+ }
121
+
122
+ private List <String > getParams (File file ) {
123
+ var builder = Stream .<String > builder ();
124
+ builder .add (binaryPath );
125
+ builder .add ("format" );
126
+ if (withDefaults ) {
127
+ builder .add ("-allowDefaults" );
128
+ }
129
+ if (configPath != null ) {
130
+ builder .add ("-s" );
131
+ builder .add (configPath );
132
+ }
133
+ builder .add (file .toString ());
134
+ return builder .build ().collect (Collectors .toList ());
135
+ }
136
+ }
55
137
}
0 commit comments