Skip to content

Commit

Permalink
Support double slash within DOUBLE_SLASH style
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
joschi committed Jul 29, 2021
1 parent 6e73b6b commit 03a46fa
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,50 @@ class LicenserPluginFunctionalTest extends Specification {
[gradleVersion, _, extraArgs] << testMatrix
}
@Unroll
def "supports URL in header (gradle #gradleVersion)"() {
given:
def projectDir = temporaryFolder.newFolder()
def sourcesDir = projectDir.toPath().resolve(Paths.get("src", "main", "java")).toFile()
sourcesDir.mkdirs()
new File(projectDir, "settings.gradle") << ""
new File(projectDir, "header.txt") << """
Copyright header
http://example.com/path?query=value#fragment
""".stripIndent().trim()
def sourceFile = new File(sourcesDir, "MyClass.java") << "public class MyClass {}"
new File(projectDir, "build.gradle") << """
plugins {
id('java')
id('org.cadixdev.licenser')
}

license {
header = project.file("header.txt")
style {
java = 'DOUBLE_SLASH'
}
}
""".stripIndent()
when:
def runner = runner(projectDir, gradleVersion, extraArgs + "updateLicenses")
runner.debug = true
def result = runner.build()
then:
result.task(":updateLicenses").outcome == TaskOutcome.SUCCESS
sourceFile.text == """\
//
// Copyright header
// http://example.com/path?query=value#fragment
//

public class MyClass {}
""".stripIndent()
where:
[gradleVersion, _, extraArgs] << testMatrix
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

package org.cadixdev.gradle.licenser.header

import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.cadixdev.gradle.licenser.util.HeaderHelper

import javax.annotation.Nullable
import java.util.regex.Pattern

@CompileStatic
class CommentHeaderFormat implements HeaderFormat {

final String name
Expand All @@ -40,13 +42,15 @@ class CommentHeaderFormat implements HeaderFormat {
@Nullable
final Pattern skipLine

@Nullable
final String firstLine
final String prefix
@Nullable
final String lastLine

@PackageScope
CommentHeaderFormat(String name, Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine,
String firstLine, String prefix, String lastLine) {
@Nullable String firstLine, String prefix, @Nullable String lastLine) {
this.name = name
this.start = start
this.end = end
Expand All @@ -60,18 +64,19 @@ class CommentHeaderFormat implements HeaderFormat {
ensureAbsent(text, firstLine)
ensureAbsent(text, lastLine)

List<String> result = [firstLine]
List<String> result = firstLine == null ? [prefix]: [firstLine]

text.eachLine {
result << HeaderHelper.stripTrailingIndent("$prefix $it")
}

result << lastLine
result << (lastLine == null ? prefix : lastLine)

return result
}

private static void ensureAbsent(String s, String search) {
if (s.contains(search)) {
if (search != null && s.contains(search)) {
throw new IllegalArgumentException("Header contains unsupported characters $search")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ enum HeaderStyle {
BLOCK_COMMENT(~/^\s*\/\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/*', ' *', ' */',
'java', 'groovy', 'scala', 'kt', 'kts', 'gradle', 'css', 'js'),
JAVADOC(~/^\s*\/\*\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/**', ' *', ' */'),
HASH(~/^\s*#/, null, ~/^\s*#!/, '#', '#', '#', 'properties', 'yml', 'yaml', 'sh'),
HASH(~/^\s*#/, null, ~/^\s*#!/, null, '#', null, 'properties', 'yml', 'yaml', 'sh'),
XML(~/^\s*<!--/, ~/-->\s*(.*?)$/, ~/^\s*<(?:\?xml .*\?|!DOCTYPE .*)>\s*$/, '<!--', ' ', '-->',
'xml', 'xsd', 'xsl', 'fxml', 'dtd', 'html', 'xhtml'),
DOUBLE_SLASH(~/^\s*\/\//, null, null, '//', '//', '//')
DOUBLE_SLASH(~/^\s*\/\//, null, null, null, '//', null)

final CommentHeaderFormat format
private final String[] extensions

HeaderStyle(Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine,
String firstLine, String prefix, String lastLine, String... extensions) {
@Nullable String firstLine, String prefix, @Nullable String lastLine, String... extensions) {
this.format = new CommentHeaderFormat(this.name(), start, end, skipLine, firstLine, prefix, lastLine)
this.extensions = extensions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, Minecrell <https://github.com/Minecrell>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.cadixdev.gradle.licenser.header

import spock.lang.Specification
import spock.lang.Unroll

class HeaderStyleTest extends Specification {
@Unroll
def "test header style #style"() {
when:
def lines = style.format.format("Test")

then:
lines == expectedLines

where:
[style, expectedLines] << [
[HeaderStyle.BLOCK_COMMENT, ["/*", " * Test", " */"]],
[HeaderStyle.JAVADOC, ["/**", " * Test", " */"]],
[HeaderStyle.DOUBLE_SLASH, ["//", "// Test", "//"]],
[HeaderStyle.HASH, ["#", "# Test", "#"]],
[HeaderStyle.XML, ["<!--", " Test", "-->"]]
]
}

def "DOUBLE_SLASH may contain slashes"() {
given:
HeaderStyle style = HeaderStyle.DOUBLE_SLASH

when:
def lines = style.format.format("// Test //")

then:
lines == ["//", "// // Test //", "//"]
}

def "BLOCK_COMMENT may not contain end of comment"() {
given:
HeaderStyle style = HeaderStyle.BLOCK_COMMENT

when:
style.format.format("/* Test */")

then:
thrown(IllegalArgumentException)
}
}

0 comments on commit 03a46fa

Please sign in to comment.