Skip to content

Commit 83dbeb5

Browse files
committed
feat: implement more options for licenceHeader
1 parent ea84f73 commit 83dbeb5

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.diffplug.spotless.antlr4.Antlr4Defaults;
2727
import com.diffplug.spotless.cli.core.SpotlessActionContext;
2828
import com.diffplug.spotless.cli.core.TargetFileTypeInferer;
29+
import com.diffplug.spotless.cli.help.OptionConstants;
2930
import com.diffplug.spotless.cpp.CppDefaults;
3031
import com.diffplug.spotless.generic.LicenseHeaderStep;
3132
import com.diffplug.spotless.kotlin.KotlinConstants;
@@ -39,7 +40,7 @@ public class LicenseHeader extends SpotlessFormatterStep {
3940
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
4041
LicenseHeaderSourceOption licenseHeaderSourceOption;
4142

42-
@CommandLine.Option(names = {"--delimiter", "-d"}, required = false, description = "The delimiter to use for the license header. If not provided, the default delimiter for the file type will be used (if available, otherwise java is assumed).")
43+
@CommandLine.Option(names = {"--delimiter", "-d"}, required = false, description = "The delimiter to use for the license header. If not provided, the delimiter will be guessed based on the first few files we find. Otherwise, 'java' will be assumed.")
4344
String delimiter;
4445

4546
static class LicenseHeaderSourceOption {
@@ -49,13 +50,26 @@ static class LicenseHeaderSourceOption {
4950
File headerFile;
5051
}
5152

52-
// TODO add more config options
53+
@CommandLine.Option(names = {"--year-mode", "-m"}, required = false, defaultValue = "PRESERVE", description = "How and if the year in the copyright header should be updated." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
54+
LicenseHeaderStep.YearMode yearMode;
55+
56+
@CommandLine.Option(names = {"--year-separator", "-Y"}, required = false, defaultValue = LicenseHeaderStep.DEFAULT_YEAR_DELIMITER, description = "The separator to use for the year range in the license header." + OptionConstants.DEFAULT_VALUE_SUFFIX)
57+
String yearSeparator;
58+
59+
@CommandLine.Option(names = {"--skip-lines-matching", "-s"}, required = false, description = "Skip lines matching the given regex pattern before inserting the licence header.")
60+
String skipLinesMatching;
61+
62+
@CommandLine.Option(names = {"--content-pattern", "-c"}, required = false, description = "The pattern to match the content of the file before inserting the licence header. (If the file content does not match the pattern, the header will not be inserted/updated.)")
63+
String contentPattern;
5364

5465
@Nonnull
5566
@Override
5667
public List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
5768
FormatterStep licenseHeaderStep = LicenseHeaderStep.headerDelimiter(headerSource(context), delimiter(context.targetFileType()))
58-
// TODO add more config options
69+
.withYearMode(yearMode)
70+
.withYearSeparator(yearSeparator)
71+
.withSkipLinesMatching(skipLinesMatching)
72+
.withContentPattern(contentPattern)
5973
.build();
6074
return List.of(licenseHeaderStep);
6175
}

cli/src/test/java/com/diffplug/spotless/cli/steps/LicenseHeaderTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20+
import java.time.LocalDate;
21+
2022
import org.junit.jupiter.api.Test;
2123

2224
import com.diffplug.spotless.cli.CLIIntegrationHarness;
2325
import com.diffplug.spotless.cli.SpotlessCLIRunner;
26+
import com.diffplug.spotless.generic.LicenseHeaderStep;
2427

2528
public class LicenseHeaderTest extends CLIIntegrationHarness {
2629

@@ -75,4 +78,90 @@ void assertDelimiterIsApplied() {
7578

7679
assertFile("TestFile.java").hasContent("/* License */\n/* keep me */\npublic class TestFile {}");
7780
}
81+
82+
@Test
83+
void assertYearModeIsApplied() {
84+
setFile("TestFile.java").toContent("/* License (c) 2022 */\npublic class TestFile {}");
85+
86+
SpotlessCLIRunner.Result result = cliRunner()
87+
.withTargets("TestFile.java")
88+
.withStep(LicenseHeader.class)
89+
.withOption("--header", "/* License (c) $YEAR */")
90+
.withOption("--year-mode", LicenseHeaderStep.YearMode.UPDATE_TO_TODAY.toString())
91+
.run();
92+
93+
assertFile("TestFile.java").hasContent("/* License (c) 2022-" + LocalDate.now().getYear() + " */\npublic class TestFile {}");
94+
}
95+
96+
@Test
97+
void assertYearSeparatorIsApplied() {
98+
setFile("TestFile.java").toContent("/* License (c) 2022...2023 */\npublic class TestFile {}");
99+
100+
SpotlessCLIRunner.Result result = cliRunner()
101+
.withTargets("TestFile.java")
102+
.withStep(LicenseHeader.class)
103+
.withOption("--header", "/* License (c) $YEAR */")
104+
.withOption("--year-mode", LicenseHeaderStep.YearMode.UPDATE_TO_TODAY.toString())
105+
106+
.withOption("--year-separator", "...")
107+
.run();
108+
109+
assertFile("TestFile.java").hasContent("/* License (c) 2022..." + LocalDate.now().getYear() + " */\npublic class TestFile {}");
110+
}
111+
112+
@Test
113+
void assertSkipLinesMatchingIsApplied() {
114+
setFile("TestFile.java").toContent("/* skip me */\npublic class TestFile {}");
115+
116+
SpotlessCLIRunner.Result result = cliRunner()
117+
.withTargets("TestFile.java")
118+
.withStep(LicenseHeader.class)
119+
.withOption("--header", "/* License */")
120+
.withOption("--skip-lines-matching", ".*skip me.*")
121+
.run();
122+
123+
assertFile("TestFile.java").hasContent("/* skip me */\n/* License */\npublic class TestFile {}");
124+
}
125+
126+
@Test
127+
void assertPreserveModeIsApplied() {
128+
setFile("TestFile.java").toContent("/* License (c) 2022 */\npublic class TestFile {}");
129+
130+
SpotlessCLIRunner.Result result = cliRunner()
131+
.withTargets("TestFile.java")
132+
.withStep(LicenseHeader.class)
133+
.withOption("--header", "/* License (c) $YEAR */")
134+
.withOption("--year-mode", LicenseHeaderStep.YearMode.PRESERVE.toString())
135+
.run();
136+
137+
assertFile("TestFile.java").hasContent("/* License (c) 2022 */\npublic class TestFile {}");
138+
}
139+
140+
@Test
141+
void assertContentPatternIsAppliedIfMatching() {
142+
setFile("TestFile.java").toContent("public class TestFile {}");
143+
144+
SpotlessCLIRunner.Result result = cliRunner()
145+
.withTargets("TestFile.java")
146+
.withStep(LicenseHeader.class)
147+
.withOption("--header", "/* License */")
148+
.withOption("--content-pattern", ".*TestFile.*")
149+
.run();
150+
151+
assertFile("TestFile.java").hasContent("/* License */\npublic class TestFile {}");
152+
}
153+
154+
@Test
155+
void assertContentPatternIsNotAppliedIfNotMatching() {
156+
setFile("TestFile.java").toContent("public class TestFile {}");
157+
158+
SpotlessCLIRunner.Result result = cliRunner()
159+
.withTargets("TestFile.java")
160+
.withStep(LicenseHeader.class)
161+
.withOption("--header", "/* License */")
162+
.withOption("--content-pattern", ".*NonExistent.*")
163+
.run();
164+
165+
assertFile("TestFile.java").hasContent("public class TestFile {}");
166+
}
78167
}

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private String sanitizePattern(@Nullable String pattern) {
190190
}
191191

192192
private static final String DEFAULT_NAME_PREFIX = LicenseHeaderStep.class.getName();
193-
private static final String DEFAULT_YEAR_DELIMITER = "-";
193+
public static final String DEFAULT_YEAR_DELIMITER = "-";
194194
private static final List<String> YEAR_TOKENS = Arrays.asList("$YEAR", "$today.year");
195195

196196
private static final SerializableFileFilter UNSUPPORTED_JVM_FILES_FILTER = SerializableFileFilter.skipFilesNamed(

0 commit comments

Comments
 (0)