Skip to content

Commit

Permalink
Ignore single-line comments in version catalogs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed Jan 31, 2024
1 parent 8370b90 commit da9b302
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 43 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add the plugin to your Gradle build file.

```kotlin
plugins {
id("io.github.pemistahl.version-catalog-linter") version "1.0.2"
id("io.github.pemistahl.version-catalog-linter") version "1.0.3"
}
```

Expand Down Expand Up @@ -60,7 +60,8 @@ tasks.check {
## Example

Below, you find examples for a totally messed up version catalog and how the output
of the plugin's Gradle tasks looks like.
of the plugin's Gradle tasks looks like. Comments are currently filtered out in the output.
It is planned to preserve comments in a later release.

### Version catalog input

Expand All @@ -77,9 +78,10 @@ jgoodiesDesktop = { group = "com.jgoodies", name = "jgoodies-desktop", version =
jgoodiesDialogs = { group = "com.jgoodies", name = "jgoodies-dialogs", version = "1.20.0" }
antisamy = { group = "org.owasp.antisamy", name = "antisamy", version = "1.5.2" }
antlr = { module = "antlr:antlr", version = "2.7.7" }
# This is a single-line comment.
apacheHttpClient = { group = "org.apache.httpcomponents", name = "httpclient", version = "4.5.14" }
apacheHttpCore = { group = "org.apache.httpcomponents", name = "httpcore", version = "4.4.16" }
apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" }
apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } # This comment is for a key-value pair.

groovyTemplates = {name = "groovy-templates", group = "org.codehaus.groovy", version.ref = "groovy" }

Expand Down
7 changes: 7 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Version Catalog Linter 1.0.3 (released on Jan 31 2024)

### Bug Fixes

- The Gradle tasks failed when a single-line comment was part of a version catalog.
This has been fixed. (#5)

## Version Catalog Linter 1.0.2 (released on Jan 16 2024)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {
}

group = "io.github.pemistahl"
version = "1.1.0"
version = "1.0.3"

kotlin {
compilerOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ internal fun readVersionCatalog(versionCatalogFile: File): VersionCatalog {
var pluginsRead = false

for ((lineNumber, line) in versionCatalogFile.readLines().withIndex()) {
val actualLineNumber = lineNumber + 1
val trimmedLine = line.trim()
val trimmedLineIsNotEmpty = trimmedLine.isNotEmpty()

if (trimmedLine == VersionCatalogSection.VERSIONS.label) {
versionsRead = true
Expand All @@ -54,21 +52,26 @@ internal fun readVersionCatalog(versionCatalogFile: File): VersionCatalog {
librariesRead = false
bundlesRead = false
pluginsRead = true
} else if (versionsRead && trimmedLineIsNotEmpty) {
versions.add(Pair(actualLineNumber..actualLineNumber, line))
} else if (librariesRead && trimmedLineIsNotEmpty) {
libraries.add(Pair(actualLineNumber..actualLineNumber, line))
} else if (bundlesRead && trimmedLineIsNotEmpty) {
if (line.contains("[")) {
bundles.add(Pair(actualLineNumber..actualLineNumber, line))
} else {
val (currentRange, currentLine) = bundles.removeAt(bundles.size - 1)
val newRange = currentRange.first..actualLineNumber
val newLine = currentLine + "\n" + line
bundles.add(Pair(newRange, newLine))
} else if (trimmedLine.isNotEmpty() && !trimmedLine.startsWith('#')) {
val actualLineNumber = lineNumber + 1
val element = Pair(actualLineNumber..actualLineNumber, line)

if (versionsRead) {
versions.add(element)
} else if (librariesRead) {
libraries.add(element)
} else if (bundlesRead) {
if (line.contains("[")) {
bundles.add(element)
} else {
val (currentRange, currentLine) = bundles.removeAt(bundles.size - 1)
val newRange = currentRange.first..actualLineNumber
val newLine = currentLine + "\n" + line
bundles.add(Pair(newRange, newLine))
}
} else if (pluginsRead) {
plugins.add(element)
}
} else if (pluginsRead && trimmedLineIsNotEmpty) {
plugins.add(Pair(actualLineNumber..actualLineNumber, line))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class IOUtilsTest {
VersionCatalog(
versions =
listOf(
30..30 to "duns = \"V0\"",
31..31 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }",
32..32 to " exact = \"1.0\"",
33..33 to "groovy = \"2.5.7\"",
34..34 to "axis = \"1.3\"",
35..35 to "ktlint = \"12.0.2\"",
36..36 to "byteBuddy = \"1.12.9\"",
37..37 to "springCore = {require=\"4.2.9.RELEASE\",reject=[\"4.3.18.RELEASE\",\"4.3.16.RELEASE\"] }",
38..38 to "cache2k = \"2.0.0.Final\"",
39..39 to "dockerJava = \"3.2.12\"",
31..31 to "duns = \"V0\"",
32..32 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }",
33..33 to " exact = \"1.0\"",
34..34 to "groovy = \"2.5.7\"",
35..35 to "axis = \"1.3\"",
36..36 to "ktlint = \"12.0.2\"",
37..37 to "byteBuddy = \"1.12.9\"",
38..38 to "springCore = {require=\"4.2.9.RELEASE\",reject=[\"4.3.18.RELEASE\",\"4.3.16.RELEASE\"] }",
39..39 to "cache2k = \"2.0.0.Final\"",
40..40 to "dockerJava = \"3.2.12\"",
),
libraries =
listOf(
Expand All @@ -47,17 +47,18 @@ class IOUtilsTest {
10..10 to "jgoodiesDialogs = { group = \"com.jgoodies\", name = \"jgoodies-dialogs\", version = \"1.20.0\" }",
11..11 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" }",
12..12 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" }",
13..13 to
14..14 to
"apacheHttpClient = { group = \"org.apache.httpcomponents\", name = \"httpclient\", version = \"4.5.14\" }",
14..14 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }",
15..15 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" }",
17..17 to
15..15 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }",
16..16 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" } " +
"# This comment is for a key-value pair.",
18..18 to
"groovyTemplates = {name = \"groovy-templates\", group = \"org.codehaus.groovy\", version.ref = \"groovy\" }",
),
bundles =
listOf(
20..20 to "groovy = [\"groovyTemplates\", \"groovy\"]",
21..25 to
21..21 to "groovy = [\"groovyTemplates\", \"groovy\"]",
22..26 to
"""
jgoodies = [
"jgoodiesDesktop",
Expand All @@ -68,8 +69,8 @@ class IOUtilsTest {
),
plugins =
listOf(
44..44 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }",
45..45 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }",
45..45 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }",
46..46 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }",
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class VersionCatalogCheckerTest {
),
)

assertEquals(
emptyList(),
task.checkVersions(
listOf(
1..1 to "byteBuddy = \"1.12.9\"",
2..2 to "cache2k = \"2.0.0.Final\" # This is a comment.",
3..3 to "slf4j = { strictly = \"[1.7, 1.8[\", prefer = \"1.7.25\" }",
),
),
)

assertEquals(
listOf("Line 1: Entry with key 'byteBuddy' in section '[versions]' must not have leading whitespace."),
task.checkVersions(
Expand Down Expand Up @@ -162,6 +173,16 @@ class VersionCatalogCheckerTest {
),
)

assertEquals(
emptyList(),
task.checkLibraries(
listOf(
1..1 to "activation = { group = \"com.sun.activation\", name = \"javax.activation\", version = \"1.2.0\" }",
2..2 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" } # This is a comment.",
),
),
)

assertEquals(
emptyList(),
task.checkLibraries(
Expand Down Expand Up @@ -332,6 +353,30 @@ class VersionCatalogCheckerTest {
),
)

assertEquals(
emptyList(),
task.checkBundles(
listOf(
1..5 to
"""
groovy = [
"groovy",
"groovyTemplates", # This is a comment.
"spock"
]
""".trimIndent(),
6..10 to
"""
jgoodies = [ # This a another comment.
"jgoodiesDesktop",
"jgoodiesDialogs",
"jgoodiesFramework"
]
""".trimIndent(),
),
),
)

assertEquals(
listOf(
"Lines 1-5: Bundle with key 'groovy' must be indented with " +
Expand Down Expand Up @@ -578,6 +623,16 @@ class VersionCatalogCheckerTest {
),
)

assertEquals(
emptyList(),
task.checkPlugins(
listOf(
1..1 to "ktlint = { id = \"org.jlleitschuh.gradle.ktlint\", version.ref = \"ktlint\" } # This is a comment.",
2..2 to "shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }",
),
),
)

assertEquals(
listOf(
"Line 1: Attributes of plugin with key 'ktlint' are not sorted correctly. Required order: id, version(.ref)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class VersionCatalogFormatterTest {
2..2 to "duns = \"V0\"",
3..3 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }",
4..4 to " exact = \"1.0\"",
5..5 to "groovy = \"2.5.7\"",
5..5 to "groovy = \"2.5.7\" # This is a comment. ",
6..6 to "axis = \"1.3\"",
7..7 to "ktlint = \"12.0.2\"",
8..8 to "byteBuddy = \"1.12.9\" ",
Expand Down Expand Up @@ -132,7 +132,7 @@ class VersionCatalogFormatterTest {
19..19 to " jgoodiesFramework = \"com.jgoodies:jgoodies-framework:1.34.0\"",
20..20 to "jgoodiesDialogs = { group = \"com.jgoodies\", name = \"jgoodies-dialogs\", version = \"1.20.0\" }",
21..21 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" }",
22..22 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" }",
22..22 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" } # This is a comment.",
23..23 to "apacheHttpClient = { group = \"org.apache.httpcomponents\", name = \"httpclient\", version = \"4.5.14\" }",
24..24 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }",
25..25 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" }",
Expand All @@ -157,7 +157,7 @@ class VersionCatalogFormatterTest {
private fun createBundles(): Pair<List<Pair<IntRange, String>>, List<String>> {
return Pair(
listOf(
30..30 to "groovy = [\"groovyTemplates\", \"groovy\"]",
30..30 to "groovy = [\"groovyTemplates\", \"groovy\"] # This is a comment.",
31..32 to " jgoodies = [ \"jgoodiesDesktop\", \n \"jgoodiesDialogs\", \"jgoodiesFramework\" ]",
),
listOf(
Expand All @@ -182,7 +182,7 @@ class VersionCatalogFormatterTest {
return Pair(
listOf(
35..35 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" } ",
36..36 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }",
36..36 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" } # This is a comment.",
),
listOf(
"ktlint = { id = \"org.jlleitschuh.gradle.ktlint\", version.ref = \"ktlint\" }",
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/inputVersionCatalog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jgoodiesDesktop = { group = "com.jgoodies", name = "jgoodies-desktop", version =
jgoodiesDialogs = { group = "com.jgoodies", name = "jgoodies-dialogs", version = "1.20.0" }
antisamy = { group = "org.owasp.antisamy", name = "antisamy", version = "1.5.2" }
antlr = { module = "antlr:antlr", version = "2.7.7" }
# This is a single-line comment.
apacheHttpClient = { group = "org.apache.httpcomponents", name = "httpclient", version = "4.5.14" }
apacheHttpCore = { group = "org.apache.httpcomponents", name = "httpcore", version = "4.4.16" }
apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" }
apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } # This comment is for a key-value pair.

groovyTemplates = {name = "groovy-templates", group = "org.codehaus.groovy", version.ref = "groovy" }

Expand Down

0 comments on commit da9b302

Please sign in to comment.