From 061ce870aca24005e4fbd301887aceb1ac6eeb72 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Tue, 26 Nov 2024 18:32:44 +0000 Subject: [PATCH] Gradle script to enforce dependencies order Signed-off-by: Aayush Gupta --- app/build.gradle.kts | 4 ++- app/check-dependencies.gradle.kts | 59 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/check-dependencies.gradle.kts diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e5f9ce3e4..8dd1594f2 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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") } } diff --git a/app/check-dependencies.gradle.kts b/app/check-dependencies.gradle.kts new file mode 100644 index 000000000..fc77148db --- /dev/null +++ b/app/check-dependencies.gradle.kts @@ -0,0 +1,59 @@ +/* + * SPDX-FileCopyrightText: 2024 NewPipe contributors + * SPDX-FileCopyrightText: 2025 NewPipe e.V. + * 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>() + var currentBlock = mutableListOf() + 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") } + ) + } + } +}