Skip to content

Commit 061ce87

Browse files
JL0000theimpulson
authored andcommitted
Gradle script to enforce dependencies order
Signed-off-by: Aayush Gupta <[email protected]>
1 parent 1508924 commit 061ce87

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

app/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ plugins {
1212
checkstyle
1313
}
1414

15+
apply(from = "check-dependencies.gradle.kts")
16+
1517
val gitWorkingBranch = providers.exec {
1618
commandLine("git", "rev-parse", "--abbrev-ref", "HEAD")
1719
}.standardOutput.asText.map { it.trim() }
@@ -180,7 +182,7 @@ afterEvaluate {
180182
if (!System.getProperties().containsKey("skipFormatKtlint")) {
181183
dependsOn("formatKtlint")
182184
}
183-
dependsOn("runCheckstyle", "runKtlint")
185+
dependsOn("runCheckstyle", "runKtlint", "checkDependenciesOrder")
184186
}
185187
}
186188

app/check-dependencies.gradle.kts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
tasks.register("checkDependenciesOrder") {
8+
group = "verification"
9+
description = "Checks that each section in libs.versions.toml is sorted alphabetically"
10+
11+
val tomlFile = file("../gradle/libs.versions.toml")
12+
13+
doLast {
14+
if (!tomlFile.exists()) {
15+
throw GradleException("TOML file not found")
16+
}
17+
18+
val lines = tomlFile.readLines()
19+
val nonSortedBlocks = mutableListOf<List<String>>()
20+
var currentBlock = mutableListOf<String>()
21+
var prevLine = ""
22+
var prevIndex = 0
23+
24+
lines.forEachIndexed { lineIndex, line ->
25+
if (line.trim().isNotEmpty() && !line.startsWith("#")) {
26+
if (line.startsWith("[")) {
27+
prevLine = ""
28+
} else {
29+
val currIndex = lineIndex + 1
30+
if (prevLine > line) {
31+
if (currentBlock.isNotEmpty() && currentBlock.last() == "$prevIndex: $prevLine") {
32+
currentBlock.add("$currIndex: $line")
33+
} else {
34+
if (currentBlock.isNotEmpty()) {
35+
nonSortedBlocks.add(currentBlock)
36+
currentBlock = mutableListOf()
37+
}
38+
currentBlock.add("$prevIndex: $prevLine")
39+
currentBlock.add("$currIndex: $line")
40+
}
41+
}
42+
prevLine = line
43+
prevIndex = lineIndex + 1
44+
}
45+
}
46+
}
47+
48+
if (currentBlock.isNotEmpty()) {
49+
nonSortedBlocks.add(currentBlock)
50+
}
51+
52+
if (nonSortedBlocks.isNotEmpty()) {
53+
throw GradleException(
54+
"The following lines were not sorted:\n" +
55+
nonSortedBlocks.joinToString("\n\n") { it.joinToString("\n") }
56+
)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)