-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XML-style headers, Kotlin, extra patterns
- Loading branch information
Showing
5 changed files
with
224 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/dev/yumi/gradle/licenser/api/comment/XmlStyleHeaderComment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023 Yumi Project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.yumi.gradle.licenser.api.comment | ||
|
||
private const val COMMENT_START = "<!--" | ||
private const val COMMENT_END = "-->" | ||
|
||
/** | ||
* [HeaderComment] implementation for XML-style comments. | ||
* | ||
* @author gdude2002 | ||
* @since 1.1.3 | ||
*/ | ||
public open class XmlStyleHeaderComment protected constructor() : HeaderComment { | ||
override fun readHeaderComment(source: String): HeaderComment.Result { | ||
val separator = this.extractLineSeparator(source) | ||
|
||
// Find the first comment block using a blank line | ||
val firstBlock = source.split(separator.repeat(2)).first() | ||
|
||
// Find the start of the comment by its opening characters | ||
val start = firstBlock.indexOf(COMMENT_START) | ||
|
||
if (start != 0) { // If the comment doesn't open on the first character of the block... | ||
// ...check whether all prefixing characters are spaces. | ||
val allWhitespacePrefixed = (0 until start).all { source[it] in arrayOf(' ', separator) } | ||
|
||
if (!allWhitespacePrefixed) { // If not, this isn't a licence header – bail out. | ||
return HeaderComment.Result(0, 0, null, separator) | ||
} | ||
} | ||
|
||
// Find the last character of the comment, including the closing characters. | ||
val end = firstBlock.indexOf(COMMENT_END) + COMMENT_END.length | ||
|
||
if (start < 0 || end < 0) { | ||
// If we can't find the start or end of the block, there's no licence header – bail out. | ||
return HeaderComment.Result(0, 0, null, separator) | ||
} | ||
|
||
// Grab the licence header comment, and split it into lines. | ||
val result: MutableList<String> = source.substring(start, end).split(separator).toMutableList() | ||
|
||
// Remove the first and last lines, as those are simply comment start/end characters, and not the licence text. | ||
result.removeFirst() | ||
result.removeLast() | ||
|
||
// Remove any indents from the licence header text, and return the result. | ||
return HeaderComment.Result(start, end, result.map { it.trimIndent() }, separator) | ||
} | ||
|
||
override fun writeHeaderComment(header: List<String>, separator: String): String = | ||
buildString { // Use a string builder to generate the licence header. | ||
append("$COMMENT_START$separator") | ||
|
||
header.forEach { | ||
append("\t$it$separator") | ||
} | ||
|
||
append(COMMENT_END) | ||
} | ||
|
||
public companion object { | ||
/** Instance of this header comment type. **/ | ||
@JvmField // Otherwise this would be `XmlStyleHeaderComment.Companion.getINSTANCE` | ||
public val INSTANCE: XmlStyleHeaderComment = XmlStyleHeaderComment() | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/dev/yumi/gradle/licenser/test/comment/XmlStyleHeaderCommentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2023 Yumi Project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.yumi.gradle.licenser.test.comment | ||
|
||
import dev.yumi.gradle.licenser.api.comment.XmlStyleHeaderComment | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class XmlStyleHeaderCommentTest { | ||
@Test | ||
fun `Parsing with existing header`() { | ||
val result = XmlStyleHeaderComment.INSTANCE.readHeaderComment( | ||
""" | ||
<!-- | ||
Sample License Header | ||
Yippee | ||
--> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
</body> | ||
</html | ||
""".trimIndent() | ||
) | ||
|
||
assertEquals(0, result.start) | ||
assertEquals(41, result.end) | ||
assertEquals("\n", result.separator) | ||
|
||
assertNotNull(result.existing) | ||
|
||
assertEquals(3, result.existing?.size) | ||
assertEquals("Sample License Header", result.existing?.first()) | ||
assertEquals("Yippee", result.existing?.last()) | ||
|
||
assert(result.existing != null) | ||
} | ||
|
||
@Test | ||
fun `Parsing with missing header`() { | ||
val result = XmlStyleHeaderComment.INSTANCE.readHeaderComment( | ||
""" | ||
<!doctype html> | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
</body> | ||
</html | ||
""".trimIndent() | ||
) | ||
|
||
assertEquals(0, result.start) | ||
assertEquals(0, result.end) | ||
assertEquals("\n", result.separator) | ||
|
||
assertNull(result.existing) { "Expected no result." } | ||
} | ||
|
||
@Test | ||
fun `Writing a header`() { | ||
val expected = """ | ||
<!-- | ||
Sample License Header | ||
Yippee | ||
--> | ||
""".trimIndent() | ||
|
||
val result = XmlStyleHeaderComment.INSTANCE.writeHeaderComment( | ||
listOf( | ||
"Sample License Header", | ||
"", | ||
"Yippee", | ||
), | ||
|
||
"\n" | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |