Skip to content

Commit

Permalink
Fix #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Jan 17, 2025
1 parent 0db4367 commit ba99627
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -9,7 +9,7 @@ plugins {
}

group = "dev.yumi"
version = "2.1.0"
version = "2.1.1"
val javaVersion = 17

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void testCHeaderParsing() {
*
* Yippee
*/
#include <stdio.h>
void main() {
printf("Hello world\\n");
}
Expand All @@ -48,7 +48,7 @@ void testCHeaderParsingSingle() {
var result = CStyleHeaderComment.INSTANCE.readHeaderComment("""
/* Smol */
#include <stdio.h>
void main() {
printf("Hello world\\n");
}
Expand All @@ -66,7 +66,7 @@ void main() {
void testCHeaderParsingNone() {
var result = CStyleHeaderComment.INSTANCE.readHeaderComment("""
#include <stdio.h>
void main() {
printf("Hello world\\n");
}
Expand All @@ -90,9 +90,9 @@ void testJavaHeaderParsing() {
*
* Yippee
*/
package dev.yumi.gradle.licenser.test;
class Test {}
""");

Expand All @@ -110,7 +110,7 @@ void testJavaHeaderParsingSingle() {
var result = CStyleHeaderComment.INSTANCE.readHeaderComment("""
/* Smol */
package dev.yumi.gradle.licenser.test;
class Test {}
""");

Expand All @@ -126,7 +126,7 @@ class Test {}
void testJavaHeaderParsingNone() {
var result = CStyleHeaderComment.INSTANCE.readHeaderComment("""
package dev.yumi.gradle.licenser.test;
class Test {}
""");

Expand All @@ -148,11 +148,11 @@ void testKotlinHeaderParsing() {
*
* Yippee
*/
@file:JvmName("TestKotlinFile")
package dev.yumi.gradle.licenser.test
fun main(args: Array<String>) {
println("Hello, world!")
}
Expand All @@ -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<String>) {
println("Hello, world!")
}
Expand All @@ -192,9 +192,9 @@ fun main(args: Array<String>) {
void testKotlinHeaderParsingNone() {
var result = CStyleHeaderComment.INSTANCE.readHeaderComment("""
@file:JvmName("TestKotlinFile")
package dev.yumi.gradle.licenser.test
fun main(args: Array<String>) {
println("Hello, world!")
}
Expand All @@ -218,7 +218,7 @@ void testScalaHeaderParsing() {
*
* Yippee
*/
@main def HelloWorld(args: String*): Unit =
println("Hello, World!")
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class XmlStyleHeaderCommentTest {
"""
<!--
Sample License Header
Yippee
-->
Expand All @@ -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)
Expand Down

0 comments on commit ba99627

Please sign in to comment.