Skip to content

Commit b934acf

Browse files
committed
refactor: turn spotless action into an option
instead of subcommand apply/check we now have --mode=APPLY or --mode=CHECK
1 parent 3d2b0ee commit b934acf

File tree

10 files changed

+96
-173
lines changed

10 files changed

+96
-173
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.spotless.cli.subcommands;
16+
package com.diffplug.spotless.cli;
1717

1818
import java.util.List;
1919

2020
import javax.annotation.Nonnull;
2121

2222
import com.diffplug.spotless.FormatterStep;
23-
import com.diffplug.spotless.cli.SpotlessCommand;
2423

25-
public interface SpotlessActionCommand extends SpotlessCommand {
24+
public interface SpotlessAction extends SpotlessCommand {
2625
Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps);
2726
}

cli/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,44 @@
1616
package com.diffplug.spotless.cli;
1717

1818
import java.nio.charset.Charset;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.nio.file.Paths;
1922
import java.util.List;
23+
import java.util.stream.Collectors;
2024

25+
import javax.annotation.Nonnull;
26+
27+
import com.diffplug.spotless.FormatExceptionPolicyStrict;
28+
import com.diffplug.spotless.Formatter;
29+
import com.diffplug.spotless.FormatterStep;
2130
import com.diffplug.spotless.LineEnding;
31+
import com.diffplug.spotless.ThrowingEx;
32+
import com.diffplug.spotless.cli.core.TargetResolver;
2233
import com.diffplug.spotless.cli.execution.SpotlessExecutionStrategy;
2334
import com.diffplug.spotless.cli.help.OptionConstants;
24-
import com.diffplug.spotless.cli.subcommands.SpotlessApply;
25-
import com.diffplug.spotless.cli.subcommands.SpotlessCheck;
35+
import com.diffplug.spotless.cli.steps.generic.LicenseHeader;
36+
import com.diffplug.spotless.cli.steps.generic.RemoveMeLaterSubCommand;
2637
import com.diffplug.spotless.cli.version.SpotlessCLIVersionProvider;
2738

2839
import picocli.CommandLine;
2940
import picocli.CommandLine.Command;
3041

31-
@Command(name = "spotless", mixinStandardHelpOptions = true, versionProvider = SpotlessCLIVersionProvider.class, description = "Runs spotless", subcommands = {SpotlessCheck.class, SpotlessApply.class})
32-
public class SpotlessCLI implements SpotlessCommand {
42+
@Command(name = "spotless", mixinStandardHelpOptions = true, versionProvider = SpotlessCLIVersionProvider.class, description = "Runs spotless", subcommandsRepeatable = true, subcommands = {
43+
LicenseHeader.class,
44+
RemoveMeLaterSubCommand.class})
45+
public class SpotlessCLI implements SpotlessAction, SpotlessCommand {
3346

34-
@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "Print version information and exit")
47+
@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "Print version information and exit.")
3548
boolean versionRequested;
3649

37-
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help message")
50+
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this help message and exit.")
3851
boolean usageHelpRequested;
3952

40-
@CommandLine.Option(names = {"--target", "-t"}, required = true, arity = "1..*", description = "The target files to format", scope = CommandLine.ScopeType.INHERIT)
53+
@CommandLine.Option(names = {"--mode", "-m"}, defaultValue = "APPLY", description = "The mode to run spotless in." + OptionConstants.VALID_VALUES_SUFFIX + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
54+
SpotlessMode spotlessMode;
55+
56+
@CommandLine.Option(names = {"--target", "-t"}, required = true, arity = "1..*", description = "The target files to format.", scope = CommandLine.ScopeType.INHERIT)
4157
public List<String> targets;
4258

4359
@CommandLine.Option(names = {"--encoding", "-e"}, defaultValue = "ISO8859-1", description = "The encoding of the files to format." + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
@@ -46,17 +62,75 @@ public class SpotlessCLI implements SpotlessCommand {
4662
@CommandLine.Option(names = {"--line-ending", "-l"}, defaultValue = "UNIX", description = "The line ending of the files to format." + OptionConstants.VALID_VALUES_SUFFIX + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
4763
public LineEnding lineEnding;
4864

65+
@Override
66+
public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps) {
67+
TargetResolver targetResolver = new TargetResolver(targets);
68+
69+
try (Formatter formatter = Formatter.builder()
70+
.lineEndingsPolicy(lineEnding.createPolicy())
71+
.encoding(encoding)
72+
.rootDir(Paths.get(".")) // TODO root dir?
73+
.steps(formatterSteps)
74+
.exceptionPolicy(new FormatExceptionPolicyStrict())
75+
.build()) {
76+
77+
boolean success = targetResolver.resolveTargets()
78+
.parallel() // needed?
79+
.map(target -> this.executeFormatter(formatter, target))
80+
.filter(result -> result.success && result.updated != null)
81+
.peek(this::writeBack)
82+
.allMatch(result -> result.success);
83+
System.out.println("Hello " + getClass().getSimpleName() + ", abc! Files: " + new TargetResolver(targets).resolveTargets().collect(Collectors.toList()));
84+
System.out.println("success: " + success);
85+
formatterSteps.forEach(step -> System.out.println("Step: " + step));
86+
return 0;
87+
}
88+
}
89+
90+
private Result executeFormatter(Formatter formatter, Path target) {
91+
System.out.println("Formatting file: " + target + " in Thread " + Thread.currentThread().getName());
92+
String targetContent = ThrowingEx.get(() -> Files.readString(target, Charset.defaultCharset())); // TODO charset!
93+
94+
String computed = formatter.compute(targetContent, target.toFile());
95+
// computed is null if file already up to date
96+
return new Result(target, true, computed);
97+
}
98+
99+
private void writeBack(Result result) {
100+
if (result.updated != null) {
101+
ThrowingEx.run(() -> Files.writeString(result.target, result.updated, Charset.defaultCharset())); // TODO charset!
102+
}
103+
// System.out.println("Writing back to file:" + result.target + " with content:\n" + result.updated);
104+
}
105+
49106
public static void main(String... args) {
50107
if (args.length == 0) {
51108
// args = new String[]{"--version"};
52-
// args = new String[]{"apply", "license-header", "--header-file", "CHANGES.md", "--delimiter-for", "java", "license-header", "--header", "abc"};
53-
args = new String[]{"--version"};
109+
// args = new String[]{"license-header", "--header-file", "CHANGES.md", "--delimiter-for", "java", "license-header", "--header", "abc"};
110+
111+
args = new String[]{"--target", "src/poc/java/**/*.java", "--encoding=UTF-8", "license-header", "--header", "abc", "--delimiter-for", "java", "license-header", "--header-file", "TestHeader.txt"};
112+
// args = new String[]{"--version"};
54113
}
55-
// args = new String[]{"apply", "--target", "src/poc/java/**/*.java", "--encoding=UTF-8", "license-header", "--header", "abc", "--delimiter-for", "java", "license-header", "--header-file", "TestHeader.txt"};
56114
int exitCode = new CommandLine(new SpotlessCLI())
57115
.setExecutionStrategy(new SpotlessExecutionStrategy())
58116
.setCaseInsensitiveEnumValuesAllowed(true)
59117
.execute(args);
60118
System.exit(exitCode);
61119
}
120+
121+
private enum SpotlessMode {
122+
CHECK, APPLY
123+
}
124+
125+
private static final class Result {
126+
private final Path target;
127+
private final boolean success;
128+
private final String updated;
129+
130+
public Result(Path target, boolean success, String updated) {
131+
this.target = target;
132+
this.success = success;
133+
this.updated = updated;
134+
}
135+
}
62136
}

cli/src/main/java/com/diffplug/spotless/cli/execution/SpotlessExecutionStrategy.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.stream.Collectors;
2222

2323
import com.diffplug.spotless.FormatterStep;
24-
import com.diffplug.spotless.cli.subcommands.SpotlessActionCommand;
25-
import com.diffplug.spotless.cli.subcommands.steps.SpotlessCLIFormatterStep;
24+
import com.diffplug.spotless.cli.SpotlessAction;
25+
import com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep;
2626

2727
import picocli.CommandLine;
2828

@@ -56,10 +56,10 @@ private List<FormatterStep> prepareFormatterSteps(CommandLine.ParseResult parseR
5656
private Integer executeSpotlessAction(CommandLine.ParseResult parseResult, List<FormatterStep> steps) {
5757
return parseResult.asCommandLineList().stream()
5858
.map(CommandLine::getCommand)
59-
.filter(command -> command instanceof SpotlessActionCommand)
60-
.map(SpotlessActionCommand.class::cast)
59+
.filter(command -> command instanceof SpotlessAction)
60+
.map(SpotlessAction.class::cast)
6161
.findFirst()
62-
.map(spotlessActionCommand -> spotlessActionCommand.executeSpotlessAction(steps))
62+
.map(spotlessAction -> spotlessAction.executeSpotlessAction(steps))
6363
.orElse(-1);
6464
}
6565
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.spotless.cli.subcommands.steps;
16+
package com.diffplug.spotless.cli.steps;
1717

1818
import java.util.List;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.spotless.cli.subcommands.steps.generic;
16+
package com.diffplug.spotless.cli.steps.generic;
1717

1818
import java.io.File;
1919
import java.nio.file.Files;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.spotless.cli.subcommands.steps.generic;
16+
package com.diffplug.spotless.cli.steps.generic;
1717

1818
import java.io.File;
1919
import java.util.List;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.spotless.cli.subcommands.steps.generic;
16+
package com.diffplug.spotless.cli.steps.generic;
1717

18-
import com.diffplug.spotless.cli.subcommands.steps.SpotlessCLIFormatterStep;
18+
import com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep;
1919

2020
import picocli.CommandLine;
2121

cli/src/main/java/com/diffplug/spotless/cli/subcommands/SpotlessActionSubCommand.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

cli/src/main/java/com/diffplug/spotless/cli/subcommands/SpotlessApply.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

cli/src/main/java/com/diffplug/spotless/cli/subcommands/SpotlessCheck.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)