Skip to content

Commit 6bf8840

Browse files
committed
feat: poc of cli (step 2)
1 parent 271a154 commit 6bf8840

File tree

8 files changed

+120
-54
lines changed

8 files changed

+120
-54
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public class SpotlessCLI implements SpotlessCommand {
3434

3535
public static void main(String... args) {
3636
// args = new String[]{"--version"};
37-
args = new String[]{"apply", "license-header", "--header-file", "abc.txt", "license-header", "--header", "abc"};
37+
args = new String[]{"apply", "license-header", "--header-file", "CHANGES.md", "--delimiter-for", "java", "license-header", "--header", "abc"};
3838
int exitCode = new CommandLine(new SpotlessCLI())
3939
.setExecutionStrategy(new SpotlessExecutionStrategy())
40+
.setCaseInsensitiveEnumValuesAllowed(true)
4041
.execute(args);
4142
System.exit(exitCode);
4243
}

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

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import static picocli.CommandLine.executeHelpRequest;
1919

20-
import java.util.function.Function;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
2122

22-
import com.diffplug.spotless.cli.SpotlessCommand;
23+
import com.diffplug.spotless.FormatterStep;
2324
import com.diffplug.spotless.cli.subcommands.SpotlessActionCommand;
24-
import com.diffplug.spotless.cli.subcommands.steps.SpotlessCLIStep;
25+
import com.diffplug.spotless.cli.subcommands.steps.SpotlessCLIFormatterStep;
2526

2627
import picocli.CommandLine;
2728

@@ -37,42 +38,28 @@ public int execute(CommandLine.ParseResult parseResult) throws CommandLine.Execu
3738

3839
private Integer runSpotlessActions(CommandLine.ParseResult parseResult) {
3940
// 1. run setup (for combining steps handled as subcommands)
41+
List<FormatterStep> steps = prepareFormatterSteps(parseResult);
4042

41-
// TODO: maybe collect a list of steps and pass them to the spotless action in step 2?
42-
Integer prepareResult = runSpotlessRecursive(parseResult, this::prepareStep);
43-
if (prepareResult != null) {
44-
return prepareResult;
45-
}
4643
// 2. run spotless steps
47-
return runSpotlessRecursive(parseResult, this::executeSpotlessAction);
44+
return executeSpotlessAction(parseResult, steps);
4845
}
4946

50-
private Integer runSpotlessRecursive(CommandLine.ParseResult parseResult, Function<SpotlessCommand, Integer> action) {
51-
SpotlessCommand spotlessCommand = parseResult.commandSpec().commandLine().getCommand();
52-
Integer result = action.apply(spotlessCommand);
53-
if (result != null) {
54-
return result;
55-
}
56-
for (CommandLine.ParseResult subCommand : parseResult.subcommands()) {
57-
Integer subResult = runSpotlessRecursive(subCommand, action);
58-
if (subResult != null) {
59-
return subResult;
60-
}
61-
}
62-
return null;
47+
private List<FormatterStep> prepareFormatterSteps(CommandLine.ParseResult parseResult) {
48+
return parseResult.asCommandLineList().stream()
49+
.map(CommandLine::getCommand)
50+
.filter(command -> command instanceof SpotlessCLIFormatterStep)
51+
.map(SpotlessCLIFormatterStep.class::cast)
52+
.flatMap(step -> step.prepareFormatterSteps().stream())
53+
.collect(Collectors.toList());
6354
}
6455

65-
private Integer prepareStep(SpotlessCommand spotlessCommand) {
66-
if (spotlessCommand instanceof SpotlessCLIStep) {
67-
((SpotlessCLIStep) spotlessCommand).prepare();
68-
}
69-
return null;
70-
}
71-
72-
private Integer executeSpotlessAction(SpotlessCommand spotlessCommand) {
73-
if (spotlessCommand instanceof SpotlessActionCommand) {
74-
return ((SpotlessActionCommand) spotlessCommand).executeSpotlessAction();
75-
}
76-
return null;
56+
private Integer executeSpotlessAction(CommandLine.ParseResult parseResult, List<FormatterStep> steps) {
57+
return parseResult.asCommandLineList().stream()
58+
.map(CommandLine::getCommand)
59+
.filter(command -> command instanceof SpotlessActionCommand)
60+
.map(SpotlessActionCommand.class::cast)
61+
.findFirst()
62+
.map(spotlessActionCommand -> spotlessActionCommand.executeSpotlessAction(steps))
63+
.orElse(-1);
7764
}
7865
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
*/
1616
package com.diffplug.spotless.cli.subcommands;
1717

18+
import java.util.List;
19+
20+
import javax.annotation.Nonnull;
21+
22+
import com.diffplug.spotless.FormatterStep;
1823
import com.diffplug.spotless.cli.SpotlessCommand;
1924

2025
public interface SpotlessActionCommand extends SpotlessCommand {
21-
Integer executeSpotlessAction();
26+
Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps);
2227
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package com.diffplug.spotless.cli.subcommands;
1717

18+
import java.util.List;
19+
20+
import javax.annotation.Nonnull;
21+
22+
import com.diffplug.spotless.FormatterStep;
1823
import com.diffplug.spotless.cli.subcommands.steps.generic.LicenseHeader;
1924
import com.diffplug.spotless.cli.subcommands.steps.generic.RemoveMeLaterSubCommand;
2025

@@ -31,8 +36,9 @@
3136
public abstract class SpotlessActionSubCommand implements SpotlessActionCommand {
3237

3338
@Override
34-
public Integer executeSpotlessAction() {
39+
public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps) {
3540
System.out.println("Hello " + getClass().getSimpleName() + ", abc!");
41+
formatterSteps.forEach(step -> System.out.println("Step: " + step));
3642
return 0;
3743
}
3844
}

cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/SpotlessCLIStep.java renamed to cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/SpotlessCLIFormatterStep.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package com.diffplug.spotless.cli.subcommands.steps;
1717

18+
import java.util.List;
19+
20+
import javax.annotation.Nonnull;
21+
22+
import com.diffplug.spotless.FormatterStep;
1823
import com.diffplug.spotless.cli.SpotlessCommand;
1924

20-
public interface SpotlessCLIStep extends SpotlessCommand {
21-
void prepare();
25+
public interface SpotlessCLIFormatterStep extends SpotlessCommand {
26+
@Nonnull
27+
List<FormatterStep> prepareFormatterSteps();
2228
}

cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/generic/LicenseHeader.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,81 @@
1616
package com.diffplug.spotless.cli.subcommands.steps.generic;
1717

1818
import java.io.File;
19+
import java.nio.file.Files;
20+
import java.util.List;
21+
22+
import javax.annotation.Nonnull;
23+
24+
import com.diffplug.spotless.FormatterStep;
25+
import com.diffplug.spotless.ThrowingEx;
26+
import com.diffplug.spotless.antlr4.Antlr4Defaults;
27+
import com.diffplug.spotless.cpp.CppDefaults;
28+
import com.diffplug.spotless.generic.LicenseHeaderStep;
29+
import com.diffplug.spotless.kotlin.KotlinConstants;
30+
import com.diffplug.spotless.protobuf.ProtobufConstants;
1931

2032
import picocli.CommandLine;
2133

2234
@CommandLine.Command(name = "license-header", description = "Runs license header")
23-
public class LicenseHeader extends SpotlessStepSubCommand {
35+
public class LicenseHeader extends SpotlessFormatterStepSubCommand {
2436

2537
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
26-
LicenseHeaderOption licenseHeaderOption;
38+
LicenseHeaderSourceOption licenseHeaderSourceOption;
2739

28-
static class LicenseHeaderOption {
40+
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
41+
LicenseHeaderDelimiterOption licenseHeaderDelimiterOption;
42+
43+
static class LicenseHeaderSourceOption {
2944
@CommandLine.Option(names = {"--header", "-H"}, required = true)
3045
String header;
3146
@CommandLine.Option(names = {"--header-file", "-f"}, required = true)
3247
File headerFile;
3348
}
3449

50+
static class LicenseHeaderDelimiterOption {
51+
52+
@CommandLine.Option(names = {"--delimiter", "-d"}, required = true)
53+
String delimiter;
54+
55+
@CommandLine.Option(names = {"--delimiter-for", "-D"}, required = true)
56+
DefaultDelimiterType defaultDelimiterType;
57+
}
58+
59+
enum DefaultDelimiterType {
60+
JAVA(LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER), CPP(CppDefaults.DELIMITER_EXPR), ANTLR4(Antlr4Defaults.licenseHeaderDelimiter()), GROOVY(LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER), PROTOBUF(ProtobufConstants.LICENSE_HEADER_DELIMITER), KOTLIN(KotlinConstants.LICENSE_HEADER_DELIMITER);
61+
62+
private final String delimiterExpression;
63+
64+
DefaultDelimiterType(String delimiterExpression) {
65+
this.delimiterExpression = delimiterExpression;
66+
}
67+
}
68+
69+
@Nonnull
3570
@Override
36-
public void prepare() {
37-
super.prepare();
38-
System.out.println(licenseHeaderOption.header != null ? "Header: " + licenseHeaderOption.header : "HeaderFile:" + licenseHeaderOption.headerFile);
71+
public List<FormatterStep> prepareFormatterSteps() {
72+
FormatterStep licenseHeaderStep = LicenseHeaderStep.headerDelimiter(headerSource(), delimiter())
73+
// TODO add more config options
74+
.build();
75+
return List.of(licenseHeaderStep);
76+
}
77+
78+
private ThrowingEx.Supplier<String> headerSource() {
79+
if (licenseHeaderSourceOption.header != null) {
80+
return () -> licenseHeaderSourceOption.header;
81+
} else {
82+
return () -> ThrowingEx.get(() -> Files.readString(licenseHeaderSourceOption.headerFile.toPath()));
83+
}
84+
}
85+
86+
private String delimiter() {
87+
if (licenseHeaderDelimiterOption == null) {
88+
return DefaultDelimiterType.JAVA.delimiterExpression;
89+
}
90+
if (licenseHeaderDelimiterOption.delimiter != null) {
91+
return licenseHeaderDelimiterOption.delimiter;
92+
} else {
93+
return licenseHeaderDelimiterOption.defaultDelimiterType.delimiterExpression;
94+
}
3995
}
4096
}

cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/generic/RemoveMeLaterSubCommand.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
package com.diffplug.spotless.cli.subcommands.steps.generic;
1717

1818
import java.io.File;
19+
import java.util.List;
20+
21+
import javax.annotation.Nonnull;
22+
23+
import com.diffplug.spotless.FormatterStep;
1924

2025
import picocli.CommandLine;
2126

2227
@CommandLine.Command(name = "ignoreme")
23-
public class RemoveMeLaterSubCommand extends SpotlessStepSubCommand {
28+
public class RemoveMeLaterSubCommand extends SpotlessFormatterStepSubCommand {
2429

2530
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
2631
LicenseHeaderOption licenseHeaderOption;
@@ -31,4 +36,10 @@ static class LicenseHeaderOption {
3136
@CommandLine.Option(names = {"--header-file", "-f"}, required = true)
3237
File headerFile;
3338
}
39+
40+
@Nonnull
41+
@Override
42+
public List<FormatterStep> prepareFormatterSteps() {
43+
return List.of();
44+
}
3445
}

cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/generic/SpotlessStepSubCommand.java renamed to cli/src/main/java/com/diffplug/spotless/cli/subcommands/steps/generic/SpotlessFormatterStepSubCommand.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.cli.subcommands.steps.generic;
1717

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

2020
import picocli.CommandLine;
2121

2222
@CommandLine.Command(mixinStandardHelpOptions = true)
23-
public abstract class SpotlessStepSubCommand implements SpotlessCLIStep {
24-
25-
@Override
26-
public void prepare() {
27-
System.out.println("Prepare SpotlessStepSubCommand " + getClass().getSimpleName() + ", abc!");
28-
}
29-
}
23+
public abstract class SpotlessFormatterStepSubCommand implements SpotlessCLIFormatterStep {}

0 commit comments

Comments
 (0)