Skip to content

Commit f8eb16d

Browse files
committed
Support double slash within DOUBLE_SLASH style
Closes #26
1 parent 6e73b6b commit f8eb16d

4 files changed

Lines changed: 128 additions & 7 deletions

File tree

src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,50 @@ class LicenserPluginFunctionalTest extends Specification {
446446
[gradleVersion, _, extraArgs] << testMatrix
447447
448448
}
449+
450+
@Unroll
451+
def "supports URL in header (gradle #gradleVersion)"() {
452+
given:
453+
def projectDir = temporaryFolder.newFolder()
454+
def sourcesDir = projectDir.toPath().resolve(Paths.get("src", "main", "java")).toFile()
455+
sourcesDir.mkdirs()
456+
new File(projectDir, "settings.gradle") << ""
457+
new File(projectDir, "header.txt") << """
458+
Copyright header
459+
http://example.com/path?query=value#fragment
460+
""".stripIndent().trim()
461+
def sourceFile = new File(sourcesDir, "MyClass.java") << "public class MyClass {}"
462+
new File(projectDir, "build.gradle") << """
463+
plugins {
464+
id('java')
465+
id('org.cadixdev.licenser')
466+
}
467+
468+
license {
469+
header = project.file("header.txt")
470+
style {
471+
java = 'DOUBLE_SLASH'
472+
}
473+
}
474+
""".stripIndent()
475+
476+
when:
477+
def runner = runner(projectDir, gradleVersion, extraArgs + "updateLicenses")
478+
runner.debug = true
479+
def result = runner.build()
480+
481+
then:
482+
result.task(":updateLicenses").outcome == TaskOutcome.SUCCESS
483+
sourceFile.text == """\
484+
//
485+
// Copyright header
486+
// http://example.com/path?query=value#fragment
487+
//
488+
489+
public class MyClass {}
490+
""".stripIndent()
491+
492+
where:
493+
[gradleVersion, _, extraArgs] << testMatrix
494+
}
449495
}

src/main/groovy/org/cadixdev/gradle/licenser/header/CommentHeaderFormat.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
package org.cadixdev.gradle.licenser.header
2626

27+
import groovy.transform.CompileStatic
2728
import groovy.transform.PackageScope
2829
import org.cadixdev.gradle.licenser.util.HeaderHelper
2930

3031
import javax.annotation.Nullable
3132
import java.util.regex.Pattern
3233

34+
@CompileStatic
3335
class CommentHeaderFormat implements HeaderFormat {
3436

3537
final String name
@@ -40,13 +42,15 @@ class CommentHeaderFormat implements HeaderFormat {
4042
@Nullable
4143
final Pattern skipLine
4244

45+
@Nullable
4346
final String firstLine
4447
final String prefix
48+
@Nullable
4549
final String lastLine
4650

4751
@PackageScope
4852
CommentHeaderFormat(String name, Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine,
49-
String firstLine, String prefix, String lastLine) {
53+
@Nullable String firstLine, String prefix, @Nullable String lastLine) {
5054
this.name = name
5155
this.start = start
5256
this.end = end
@@ -60,18 +64,19 @@ class CommentHeaderFormat implements HeaderFormat {
6064
ensureAbsent(text, firstLine)
6165
ensureAbsent(text, lastLine)
6266

63-
List<String> result = [firstLine]
67+
List<String> result = firstLine == null ? [prefix]: [firstLine]
6468

6569
text.eachLine {
6670
result << HeaderHelper.stripTrailingIndent("$prefix $it")
6771
}
6872

69-
result << lastLine
73+
result << (lastLine == null ? prefix : lastLine)
74+
7075
return result
7176
}
7277

7378
private static void ensureAbsent(String s, String search) {
74-
if (s.contains(search)) {
79+
if (search != null && s.contains(search)) {
7580
throw new IllegalArgumentException("Header contains unsupported characters $search")
7681
}
7782
}

src/main/groovy/org/cadixdev/gradle/licenser/header/HeaderStyle.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ enum HeaderStyle {
3131
BLOCK_COMMENT(~/^\s*\/\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/*', ' *', ' */',
3232
'java', 'groovy', 'scala', 'kt', 'kts', 'gradle', 'css', 'js'),
3333
JAVADOC(~/^\s*\/\*\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/**', ' *', ' */'),
34-
HASH(~/^\s*#/, null, ~/^\s*#!/, '#', '#', '#', 'properties', 'yml', 'yaml', 'sh'),
34+
HASH(~/^\s*#/, null, ~/^\s*#!/, null, '#', null, 'properties', 'yml', 'yaml', 'sh'),
3535
XML(~/^\s*<!--/, ~/-->\s*(.*?)$/, ~/^\s*<(?:\?xml .*\?|!DOCTYPE .*)>\s*$/, '<!--', ' ', '-->',
3636
'xml', 'xsd', 'xsl', 'fxml', 'dtd', 'html', 'xhtml'),
37-
DOUBLE_SLASH(~/^\s*\/\//, null, null, '//', '//', '//')
37+
DOUBLE_SLASH(~/^\s*\/\//, null, null, null, '//', null)
3838

3939
final CommentHeaderFormat format
4040
private final String[] extensions
4141

4242
HeaderStyle(Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine,
43-
String firstLine, String prefix, String lastLine, String... extensions) {
43+
@Nullable String firstLine, String prefix, @Nullable String lastLine, String... extensions) {
4444
this.format = new CommentHeaderFormat(this.name(), start, end, skipLine, firstLine, prefix, lastLine)
4545
this.extensions = extensions
4646
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2015, Minecrell <https://github.com/Minecrell>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.cadixdev.gradle.licenser.header
26+
27+
import spock.lang.Specification
28+
import spock.lang.Unroll
29+
30+
class HeaderStyleTest extends Specification {
31+
@Unroll
32+
def "test header style #style"() {
33+
when:
34+
def lines = style.format.format("Test")
35+
36+
then:
37+
lines == expectedLines
38+
39+
where:
40+
[style, expectedLines] << [
41+
[HeaderStyle.BLOCK_COMMENT, ["/*", " * Test", " */"]],
42+
[HeaderStyle.JAVADOC, ["/**", " * Test", " */"]],
43+
[HeaderStyle.DOUBLE_SLASH, ["//", "// Test", "//"]],
44+
[HeaderStyle.HASH, ["#", "# Test", "#"]],
45+
[HeaderStyle.XML, ["<!--", " Test", "-->"]]
46+
]
47+
}
48+
49+
def "DOUBLE_SLASH may contain slashes"() {
50+
given:
51+
HeaderStyle style = HeaderStyle.DOUBLE_SLASH
52+
53+
when:
54+
def lines = style.format.format("// Test //")
55+
56+
then:
57+
lines == ["//", "// // Test //", "//"]
58+
}
59+
60+
def "BLOCK_COMMENT may not contain end of comment"() {
61+
given:
62+
HeaderStyle style = HeaderStyle.BLOCK_COMMENT
63+
64+
when:
65+
style.format.format("/* Test */")
66+
67+
then:
68+
thrown(IllegalArgumentException)
69+
}
70+
}

0 commit comments

Comments
 (0)