1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-11-17 15:47:12 +00:00

Gradle script to enforce dependencies order

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Jie Li
2024-11-26 18:32:44 +00:00
committed by Aayush Gupta
parent 15089245bb
commit 061ce870ac
2 changed files with 62 additions and 1 deletions

View File

@@ -12,6 +12,8 @@ plugins {
checkstyle
}
apply(from = "check-dependencies.gradle.kts")
val gitWorkingBranch = providers.exec {
commandLine("git", "rev-parse", "--abbrev-ref", "HEAD")
}.standardOutput.asText.map { it.trim() }
@@ -180,7 +182,7 @@ afterEvaluate {
if (!System.getProperties().containsKey("skipFormatKtlint")) {
dependsOn("formatKtlint")
}
dependsOn("runCheckstyle", "runKtlint")
dependsOn("runCheckstyle", "runKtlint", "checkDependenciesOrder")
}
}

View File

@@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: 2024 NewPipe contributors <https://newpipe.net>
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
tasks.register("checkDependenciesOrder") {
group = "verification"
description = "Checks that each section in libs.versions.toml is sorted alphabetically"
val tomlFile = file("../gradle/libs.versions.toml")
doLast {
if (!tomlFile.exists()) {
throw GradleException("TOML file not found")
}
val lines = tomlFile.readLines()
val nonSortedBlocks = mutableListOf<List<String>>()
var currentBlock = mutableListOf<String>()
var prevLine = ""
var prevIndex = 0
lines.forEachIndexed { lineIndex, line ->
if (line.trim().isNotEmpty() && !line.startsWith("#")) {
if (line.startsWith("[")) {
prevLine = ""
} else {
val currIndex = lineIndex + 1
if (prevLine > line) {
if (currentBlock.isNotEmpty() && currentBlock.last() == "$prevIndex: $prevLine") {
currentBlock.add("$currIndex: $line")
} else {
if (currentBlock.isNotEmpty()) {
nonSortedBlocks.add(currentBlock)
currentBlock = mutableListOf()
}
currentBlock.add("$prevIndex: $prevLine")
currentBlock.add("$currIndex: $line")
}
}
prevLine = line
prevIndex = lineIndex + 1
}
}
}
if (currentBlock.isNotEmpty()) {
nonSortedBlocks.add(currentBlock)
}
if (nonSortedBlocks.isNotEmpty()) {
throw GradleException(
"The following lines were not sorted:\n" +
nonSortedBlocks.joinToString("\n\n") { it.joinToString("\n") }
)
}
}
}