Skip to content

Commit 3d2b0ee

Browse files
committed
feat: poc cli
1 parent dc424ec commit 3d2b0ee

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

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

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

18+
import java.nio.charset.Charset;
1819
import java.util.List;
1920

21+
import com.diffplug.spotless.LineEnding;
2022
import com.diffplug.spotless.cli.execution.SpotlessExecutionStrategy;
23+
import com.diffplug.spotless.cli.help.OptionConstants;
2124
import com.diffplug.spotless.cli.subcommands.SpotlessApply;
2225
import com.diffplug.spotless.cli.subcommands.SpotlessCheck;
2326
import com.diffplug.spotless.cli.version.SpotlessCLIVersionProvider;
@@ -37,10 +40,19 @@ public class SpotlessCLI implements SpotlessCommand {
3740
@CommandLine.Option(names = {"--target", "-t"}, required = true, arity = "1..*", description = "The target files to format", scope = CommandLine.ScopeType.INHERIT)
3841
public List<String> targets;
3942

43+
@CommandLine.Option(names = {"--encoding", "-e"}, defaultValue = "ISO8859-1", description = "The encoding of the files to format." + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
44+
public Charset encoding;
45+
46+
@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)
47+
public LineEnding lineEnding;
48+
4049
public static void main(String... args) {
41-
// args = new String[]{"--version"};
42-
// args = new String[]{"apply", "license-header", "--header-file", "CHANGES.md", "--delimiter-for", "java", "license-header", "--header", "abc"};
43-
args = new String[]{"apply", "--target", "src/poc/java/**/*.java", "license-header", "--header", "abc", "--delimiter-for", "java", "license-header", "--header-file", "TestHeader.txt"};
50+
if (args.length == 0) {
51+
// 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"};
54+
}
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"};
4456
int exitCode = new CommandLine(new SpotlessCLI())
4557
.setExecutionStrategy(new SpotlessExecutionStrategy())
4658
.setCaseInsensitiveEnumValuesAllowed(true)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.help;
17+
18+
public final class OptionConstants {
19+
20+
public static final String VALID_VALUES_SUFFIX = " One of: ${COMPLETION-CANDIDATES}";
21+
22+
public static final String DEFAULT_VALUE_SUFFIX = " (default: ${DEFAULT-VALUE})";
23+
24+
private OptionConstants() {
25+
// no instance
26+
}
27+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.diffplug.spotless.FormatExceptionPolicyStrict;
2929
import com.diffplug.spotless.Formatter;
3030
import com.diffplug.spotless.FormatterStep;
31-
import com.diffplug.spotless.LineEnding;
3231
import com.diffplug.spotless.ThrowingEx;
3332
import com.diffplug.spotless.cli.SpotlessCLI;
3433
import com.diffplug.spotless.cli.core.TargetResolver;
@@ -55,8 +54,8 @@ public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps
5554
TargetResolver targetResolver = new TargetResolver(parent.targets);
5655

5756
try (Formatter formatter = Formatter.builder()
58-
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
59-
.encoding(Charset.defaultCharset()) // TODO charset!
57+
.lineEndingsPolicy(parent.lineEnding.createPolicy())
58+
.encoding(parent.encoding)
6059
.rootDir(Paths.get(".")) // TODO root dir?
6160
.steps(formatterSteps)
6261
.exceptionPolicy(new FormatExceptionPolicyStrict())

0 commit comments

Comments
 (0)