1818import java .nio .charset .Charset ;
1919import java .nio .file .Path ;
2020import java .util .List ;
21- import java .util .function .BiConsumer ;
22- import java .util .stream .Collectors ;
2321
2422import javax .annotation .Nonnull ;
2523
2826import com .diffplug .spotless .LineEnding ;
2927import com .diffplug .spotless .LintState ;
3028import com .diffplug .spotless .ThrowingEx ;
29+ import com .diffplug .spotless .cli .core .SpotlessActionContext ;
30+ import com .diffplug .spotless .cli .core .TargetFileTypeInferer ;
3131import com .diffplug .spotless .cli .core .TargetResolver ;
3232import com .diffplug .spotless .cli .execution .SpotlessExecutionStrategy ;
3333import com .diffplug .spotless .cli .help .OptionConstants ;
4141@ Command (name = "spotless" , mixinStandardHelpOptions = true , versionProvider = SpotlessCLIVersionProvider .class , description = "Runs spotless" , subcommandsRepeatable = true , subcommands = {
4242 LicenseHeader .class ,
4343 RemoveMeLaterSubCommand .class })
44- public class SpotlessCLI implements SpotlessAction , SpotlessCommand {
44+ public class SpotlessCLI implements SpotlessAction , SpotlessCommand , SpotlessActionContextProvider {
4545
4646 @ CommandLine .Option (names = {"-V" , "--version" }, versionHelp = true , description = "Print version information and exit." )
4747 boolean versionRequested ;
@@ -71,70 +71,46 @@ public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps
7171 .steps (formatterSteps )
7272 .build ()) {
7373
74- ResultType resultType = targetResolver .resolveTargets () // TODO result
74+ ResultType resultType = targetResolver .resolveTargets ()
7575 .parallel () // needed?
7676 .map (path -> ThrowingEx .get (() -> new Result (path , LintState .of (formatter , path .toFile ())))) // TODO handle suppressions, see SpotlessTaskImpl
7777 .map (result -> this .handleResult (formatter , result ))
7878 .reduce (ResultType .CLEAN , ResultType ::combineWith );
79- System .out .println ("Hello " + getClass ().getSimpleName () + ", abc! Files: " + new TargetResolver (targets ).resolveTargets ().collect (Collectors .toList ()));
80- System .out .println ("result: " + resultType );
81- formatterSteps .forEach (step -> System .out .println ("Step: " + step ));
82- return 0 ;
79+ return spotlessMode .translateResultTypeToExitCode (resultType );
8380 }
8481 }
8582
8683 private ResultType handleResult (Formatter formatter , Result result ) {
8784 if (result .lintState .isClean ()) {
88- System .out .println ("File is clean: " + result .target .toFile ().getName ());
85+ // System.out.println("File is clean: " + result.target.toFile().getName());
8986 return ResultType .CLEAN ;
9087 }
9188 if (result .lintState .getDirtyState ().didNotConverge ()) {
92- System .out .println ("File did not converge: " + result .target .toFile ().getName ());
89+ System .err .println ("File did not converge: " + result .target .toFile ().getName ()); // TODO: where to print the output to?
9390 return ResultType .DID_NOT_CONVERGE ;
9491 }
95- this .spotlessMode .action . accept (formatter , result );
92+ this .spotlessMode .handleResult (formatter , result );
9693 return ResultType .DIRTY ;
9794
98- /*
99- if (lintState.getDirtyState().isClean()) {
100- // Remove previous output if it exists
101- Files.deleteIfExists(cleanFile.toPath());
102- } else if (lintState.getDirtyState().didNotConverge()) {
103- getLogger().warn("Skipping '{}' because it does not converge. Run {@code spotlessDiagnose} to understand why", relativePath);
104- } else {
105- Path parentDir = cleanFile.toPath().getParent();
106- if (parentDir == null) {
107- throw new IllegalStateException("Every file has a parent folder. But not: " + cleanFile);
108- }
109- Files.createDirectories(parentDir);
110- // Need to copy the original file to the tmp location just to remember the file attributes
111- Files.copy(input.toPath(), cleanFile.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
95+ }
11296
113- getLogger().info(String.format("Writing clean file: %s", cleanFile));
114- lintState.getDirtyState().writeCanonicalTo(cleanFile);
115- }
116- if (!lintState.isHasLints()) {
117- Files.deleteIfExists(lintFile.toPath());
118- } else {
119- LinkedHashMap<String, List<Lint>> lints = lintState.getLintsByStep(formatter);
120- SerializableMisc.toFile(lints, lintFile);
121- }
122- */
97+ private TargetResolver targetResolver () {
98+ return new TargetResolver (targets );
12399 }
124100
125- // private void writeBack(Result result) {
126- // if (result.updated != null ) {
127- // ThrowingEx.run(() -> Files.writeString(result.target, result.updated, Charset.defaultCharset())); // TODO charset!
128- // }
129- // System.out.println("Writing back to file:" + result.target + " with content:\n" + result.updated );
130- // }
101+ @ Override
102+ public SpotlessActionContext spotlessActionContext ( ) {
103+ TargetResolver targetResolver = targetResolver ();
104+ TargetFileTypeInferer targetFileTypeInferer = new TargetFileTypeInferer ( targetResolver );
105+ return new SpotlessActionContext ( targetFileTypeInferer . inferTargetFileType () );
106+ }
131107
132108 public static void main (String ... args ) {
133109 if (args .length == 0 ) {
134110 // args = new String[]{"--version"};
135111 // args = new String[]{"license-header", "--header-file", "CHANGES.md", "--delimiter-for", "java", "license-header", "--header", "abc"};
136112
137- args = new String []{"--mode=CHECK" , "--target" , "src/poc/java/**/*.java" , "--encoding=UTF-8" , "license-header" , "--header" , "abc" , "--delimiter-for" , "java" , " license-header" , "--header-file" , "TestHeader.txt" };
113+ args = new String []{"--mode=CHECK" , "--target" , "src/poc/java/**/*.java" , "--encoding=UTF-8" , "license-header" , "--header" , "abc" , "license-header" , "--header-file" , "TestHeader.txt" };
138114 // args = new String[]{"--version"};
139115 }
140116 int exitCode = new CommandLine (new SpotlessCLI ())
@@ -145,19 +121,54 @@ public static void main(String... args) {
145121 }
146122
147123 private enum SpotlessMode {
148- CHECK (((formatter , result ) -> {
149- if (result .lintState .isHasLints ()) {
150- result .lintState .asStringOneLine (result .target .toFile (), formatter );
151- } else {
152- System .out .println (String .format ("%s is violating formatting rules." , result .target ));
124+ CHECK {
125+ @ Override
126+ void handleResult (Formatter formatter , Result result ) {
127+ if (result .lintState .isHasLints ()) {
128+ result .lintState .asStringOneLine (result .target .toFile (), formatter );
129+ } else {
130+ System .out .println (String .format ("%s is violating formatting rules." , result .target ));
131+ }
153132 }
154- })), APPLY (((formatter , result ) -> ThrowingEx .run (() -> result .lintState .getDirtyState ().writeCanonicalTo (result .target .toFile ()))));
155133
156- private final BiConsumer <Formatter , Result > action ;
134+ @ Override
135+ Integer translateResultTypeToExitCode (ResultType resultType ) {
136+ if (resultType == ResultType .CLEAN ) {
137+ return 0 ;
138+ }
139+ if (resultType == ResultType .DIRTY ) {
140+ return 1 ;
141+ }
142+ if (resultType == ResultType .DID_NOT_CONVERGE ) {
143+ return -1 ;
144+ }
145+ throw new IllegalStateException ("Unexpected result type: " + resultType );
146+ }
147+ },
148+ APPLY {
149+ @ Override
150+ void handleResult (Formatter formatter , Result result ) {
151+ ThrowingEx .run (() -> result .lintState .getDirtyState ().writeCanonicalTo (result .target .toFile ()));
152+ }
157153
158- SpotlessMode (BiConsumer <Formatter , Result > action ) {
159- this .action = action ;
160- }
154+ @ Override
155+ Integer translateResultTypeToExitCode (ResultType resultType ) {
156+ if (resultType == ResultType .CLEAN ) {
157+ return 0 ;
158+ }
159+ if (resultType == ResultType .DIRTY ) {
160+ return 0 ;
161+ }
162+ if (resultType == ResultType .DID_NOT_CONVERGE ) {
163+ return -1 ;
164+ }
165+ throw new IllegalStateException ("Unexpected result type: " + resultType );
166+ }
167+ };
168+
169+ abstract void handleResult (Formatter formatter , Result result );
170+
171+ abstract Integer translateResultTypeToExitCode (ResultType resultType );
161172
162173 }
163174
0 commit comments