From ba9962774d757f3203bda991e10f6d584dbd365c Mon Sep 17 00:00:00 2001 From: LambdAurora Date: Fri, 17 Jan 2025 17:55:07 +0100 Subject: [PATCH] Fix #5. --- CHANGELOG.md | 4 +++ build.gradle.kts | 4 +-- .../api/comment/XmlStyleHeaderComment.kt | 21 ++++++------ .../test/comment/CStyleHeaderCommentTest.java | 32 +++++++++---------- .../test/comment/XmlStyleHeaderCommentTest.kt | 4 +-- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07c0475..cb66fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,4 +39,8 @@ - Made check license tasks incremental. +## 2.1.1 + +- Fixed XML comment parser not properly parsing existing comments ([#5](https://github.com/YumiProject/yumi-gradle-licenser/issues/5)). + [#3]: https://github.com/YumiProject/yumi-gradle-licenser/pull/3 diff --git a/build.gradle.kts b/build.gradle.kts index 16ffcb8..d61555b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("dev.yumi.gradle.licenser") version "2.0.+" + id("dev.yumi.gradle.licenser") version "2.1.+" id("com.gradle.plugin-publish") version "1.2.0" kotlin("jvm") version "2.0.0" @@ -9,7 +9,7 @@ plugins { } group = "dev.yumi" -version = "2.1.0" +version = "2.1.1" val javaVersion = 17 repositories { diff --git a/src/main/java/dev/yumi/gradle/licenser/api/comment/XmlStyleHeaderComment.kt b/src/main/java/dev/yumi/gradle/licenser/api/comment/XmlStyleHeaderComment.kt index cb1487e..6ecba4f 100644 --- a/src/main/java/dev/yumi/gradle/licenser/api/comment/XmlStyleHeaderComment.kt +++ b/src/main/java/dev/yumi/gradle/licenser/api/comment/XmlStyleHeaderComment.kt @@ -15,29 +15,28 @@ private const val COMMENT_END = "-->" * [HeaderComment] implementation for XML-style comments. * * @author gdude2002 + * @version 2.1.1 * @since 1.2.0 */ 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 block. + val firstBlockStart = source.indices.first { index -> !Character.isWhitespace(source[index]) } // Find the start of the comment by its opening characters - val start = firstBlock.indexOf(COMMENT_START) + val start = source.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) - } + if (start != firstBlockStart) { + // If the comment doesn't open on the first character of the block, something fishy is going on. + return HeaderComment.Result(0, 0, null, separator) } + // We're now officially in the first comment block. + // Find the last character of the comment, including the closing characters. - val end = firstBlock.indexOf(COMMENT_END) + COMMENT_END.length + val end = source.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. diff --git a/src/test/java/dev/yumi/gradle/licenser/test/comment/CStyleHeaderCommentTest.java b/src/test/java/dev/yumi/gradle/licenser/test/comment/CStyleHeaderCommentTest.java index 5b40166..da613df 100644 --- a/src/test/java/dev/yumi/gradle/licenser/test/comment/CStyleHeaderCommentTest.java +++ b/src/test/java/dev/yumi/gradle/licenser/test/comment/CStyleHeaderCommentTest.java @@ -26,9 +26,9 @@ void testCHeaderParsing() { * * Yippee */ - + #include - + void main() { printf("Hello world\\n"); } @@ -48,7 +48,7 @@ void testCHeaderParsingSingle() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" /* Smol */ #include - + void main() { printf("Hello world\\n"); } @@ -66,7 +66,7 @@ void main() { void testCHeaderParsingNone() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" #include - + void main() { printf("Hello world\\n"); } @@ -90,9 +90,9 @@ void testJavaHeaderParsing() { * * Yippee */ - + package dev.yumi.gradle.licenser.test; - + class Test {} """); @@ -110,7 +110,7 @@ void testJavaHeaderParsingSingle() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" /* Smol */ package dev.yumi.gradle.licenser.test; - + class Test {} """); @@ -126,7 +126,7 @@ class Test {} void testJavaHeaderParsingNone() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" package dev.yumi.gradle.licenser.test; - + class Test {} """); @@ -148,11 +148,11 @@ void testKotlinHeaderParsing() { * * Yippee */ - + @file:JvmName("TestKotlinFile") - + package dev.yumi.gradle.licenser.test - + fun main(args: Array) { println("Hello, world!") } @@ -172,9 +172,9 @@ void testKotlinHeaderParsingSingle() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" /* Smol */ @file:JvmName("TestKotlinFile") - + package dev.yumi.gradle.licenser.test - + fun main(args: Array) { println("Hello, world!") } @@ -192,9 +192,9 @@ fun main(args: Array) { void testKotlinHeaderParsingNone() { var result = CStyleHeaderComment.INSTANCE.readHeaderComment(""" @file:JvmName("TestKotlinFile") - + package dev.yumi.gradle.licenser.test - + fun main(args: Array) { println("Hello, world!") } @@ -218,7 +218,7 @@ void testScalaHeaderParsing() { * * Yippee */ - + @main def HelloWorld(args: String*): Unit = println("Hello, World!") """); diff --git a/src/test/java/dev/yumi/gradle/licenser/test/comment/XmlStyleHeaderCommentTest.kt b/src/test/java/dev/yumi/gradle/licenser/test/comment/XmlStyleHeaderCommentTest.kt index 470dfbf..9f74d47 100644 --- a/src/test/java/dev/yumi/gradle/licenser/test/comment/XmlStyleHeaderCommentTest.kt +++ b/src/test/java/dev/yumi/gradle/licenser/test/comment/XmlStyleHeaderCommentTest.kt @@ -19,7 +19,7 @@ class XmlStyleHeaderCommentTest { """ @@ -34,7 +34,7 @@ class XmlStyleHeaderCommentTest { ) assertEquals(0, result.start) - assertEquals(41, result.end) + assertEquals(40, result.end) assertEquals("\n", result.separator) assertNotNull(result.existing)