|
| 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