Relocate toml lint task to buildSrc and extend against default task
Fixes build errors after Gradle 9.x upgrade Ref: https://docs.gradle.org/current/userguide/implementing_custom_tasks.html Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
parent
0a0f28e801
commit
b06b7c35ca
3 changed files with 35 additions and 12 deletions
68
buildSrc/src/main/kotlin/CheckDependenciesOrder.kt
Normal file
68
buildSrc/src/main/kotlin/CheckDependenciesOrder.kt
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.tasks.InputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
abstract class CheckDependenciesOrder : DefaultTask() {
|
||||
|
||||
@get:InputFile
|
||||
abstract val tomlFile: RegularFileProperty
|
||||
|
||||
init {
|
||||
group = "verification"
|
||||
description = "Checks that each section in libs.versions.toml is sorted alphabetically"
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
fun run() {
|
||||
val file = tomlFile.get().asFile
|
||||
if (!file.exists()) error("TOML file not found")
|
||||
|
||||
val lines = file.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()) {
|
||||
error(
|
||||
"The following lines were not sorted:\n" +
|
||||
nonSortedBlocks.joinToString("\n\n") { it.joinToString("\n") }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue