From 2e07d5ee4229f83d562d8596e266ec2f901ceda8 Mon Sep 17 00:00:00 2001 From: Artur Bosch Date: Tue, 16 Jun 2020 13:00:31 +0200 Subject: [PATCH] Refactor compare_releases script to use clikt (#2801) --- scripts/compare_releases.kts | 109 ---------------------- scripts/compare_releases.main.kts | 110 +++++++++++++++++++++++ scripts/github-milestone-report.main.kts | 7 +- 3 files changed, 113 insertions(+), 113 deletions(-) delete mode 100644 scripts/compare_releases.kts create mode 100755 scripts/compare_releases.main.kts diff --git a/scripts/compare_releases.kts b/scripts/compare_releases.kts deleted file mode 100644 index 71b58eb4f..000000000 --- a/scripts/compare_releases.kts +++ /dev/null @@ -1,109 +0,0 @@ -// kotlinc throws a "const is only allowed for top level properties" for unknown reasons -@file:Suppress("detekt.VariableNaming") - -import java.nio.file.Files -import java.nio.file.Path -import java.nio.file.Paths -import java.util.regex.Pattern - -/* - Working directory is expected to be [path/to/detekt]. - When running the script from IntelliJ (Ctrl+Shift+F10), you have to change the working directory before. - Setup: "Shift+Alt+F10 -> Right -> Edit... -> Working Directory" - - This script must find '/scripts/compare_releases_config.yml' and the detekt-cli module. - It automatically picks up the shadowJar's called "detekt-cli-[version]-all.jar", runs them - and uses 'diff' or a given diff tool to compare the results. - */ - -val NUMBER_OF_ARGUMENTS_EXPECTED = 3 -val FIRST_VERSION_ARG = 0 -val SECOND_VERSION_ARG = 1 -val ANALYSIS_PROJECT_ARG = 2 -val IS_CUSTOM_DIFF_TOOL_USED = 4 -val DIFF_TOOL = 3 - -val arguments = args.toList() -check(arguments.size >= NUMBER_OF_ARGUMENTS_EXPECTED) { - "Usage: [version1] [version2] [analysis-path] [diff-tool]?" -} - -val analysisPath = Paths.get(arguments[ANALYSIS_PROJECT_ARG]).toAbsolutePath().normalize() -check(Files.exists(analysisPath)) { "analysis path '$analysisPath' does not exist" } - -val configPath = Paths.get(".", "scripts/compare_releases_config.yml") - .toAbsolutePath().normalize() -check(Files.exists(configPath)) { "config at '$configPath' must exist" } - -// the diff tool is expected to exist and accept two files -// default is to use 'diff' which should exist on unix systems -val diffTool = if (arguments.size == IS_CUSTOM_DIFF_TOOL_USED) arguments[DIFF_TOOL] else "diff" - -fun findJar(root: Path, version: String): Path { - val pattern = Pattern.compile("detekt-cli-$version-all.jar").asPredicate() - return Files.walk(root) - .filter { pattern.test(it.fileName.toString()) } - .findFirst() - .orElseThrow { IllegalArgumentException("no jar with version $version found") } -} - -val rootForJars = Paths.get(".", "detekt-cli/build/libs") - -val version1 = arguments[0] -val version2 = arguments[1] -val jar1 = findJar(rootForJars, version1).toAbsolutePath().normalize() -val jar2 = findJar(rootForJars, version2).toAbsolutePath().normalize() - -println("Comparing: \n$jar1\n$jar2") - -fun javaExec(jar: Path, output: Path) { - val command = listOf( - "java", - "-jar", - jar.toString(), - "--input", - analysisPath.toString(), - "--build-upon-default-config", - "--fail-fast", - "--config", - configPath.toString(), - "--excludes", - "**/resources/**,**/build/**,**/out/**,**/target/**", - "--report", - "txt:$output" - ) - println("Executing ${command.joinToString(" ")}") - ProcessBuilder(command) - .inheritIO() - .start() - .waitFor() -} - -val diff1 = Files.createTempFile("detekt", "compare") -val diff2 = Files.createTempFile("detekt", "compare") - -fun executeDetekt() { - javaExec(jar1, diff1) - javaExec(jar2, diff2) - println("Detekt txt results are saved at:\n$diff1\n$diff2") -} - -executeDetekt() - -fun performDiff() { - val command = listOf("diff", diff1.toString(), diff2.toString()) - val diffResult = Files.createTempFile("detekt", "diff").toFile() - ProcessBuilder(command) - .redirectOutput(diffResult) - .start() - .waitFor() - val diff = diffResult.readText().trim() - if (diff.isNotEmpty()) { - println(diff) - println("There were differences beween results.") - } else { - println("No differences between results.") - } -} - -performDiff() diff --git a/scripts/compare_releases.main.kts b/scripts/compare_releases.main.kts new file mode 100755 index 000000000..b5d5b81b6 --- /dev/null +++ b/scripts/compare_releases.main.kts @@ -0,0 +1,110 @@ +#!/bin/sh +//bin/true; exec kotlinc -script "$0" -- "$@" + +/** + * Script to compare two detekt releases by running both versions and diffing the txt results. + * + * Working directory is expected to be [path/to/detekt]. + * When running the script from IntelliJ (Ctrl+Shift+F10), you have to change the working directory before. + * Setup: "Shift+Alt+F10 -> Right -> Edit... -> Working Directory" + * + * This script must find '/scripts/compare_releases_config.yml' and the detekt-cli module. + * It automatically picks up the shadowJar's called "detekt-cli--all.jar", runs them + * and uses 'diff' or a given diff tool to compare the results. + * + * You need kotlin 1.3.70+ installed on your machine + */ + +@file:DependsOn("com.github.ajalt:clikt:2.7.1") + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.options.required + +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import java.util.regex.Pattern + +class CompareReleases : CliktCommand() { + + private val version1: String by option("-v1", help = "First detekt release version.").required() + private val version2: String by option("-v2", help = "Second detekt release version.").required() + private val analysisPath: String by option("-p", help = "Path to a project.").required() + private val configPath: String by option( + "-c", + help = "Path to a config file. Default: scripts/compare_releases_config.yml." + ).default("scripts/compare_releases_config.yml") + private val diffTool: String by option("-d", help = "Diff tool. Default: diff.").default("diff") + + override fun run() { + require(version1 != version2) { "Same version '$version1' used as input." } + val project = Paths.get(analysisPath).toAbsolutePath().normalize() + require(Files.exists(project)) { "analysis path '$analysisPath' does not exist." } + val config = Paths.get(".", configPath).toAbsolutePath().normalize() + require(Files.exists(config)) { "config at '$configPath' must exist." } + + val rootForJars = Paths.get(".", "detekt-cli/build/libs") + val jar1 = findJar(rootForJars, version1).toAbsolutePath().normalize() + val jar2 = findJar(rootForJars, version2).toAbsolutePath().normalize() + println("Comparing: \n$jar1\n$jar2") + + val diff1 = Files.createTempFile("detekt", "compare") + val diff2 = Files.createTempFile("detekt", "compare") + javaExec(jar1, diff1) + javaExec(jar2, diff2) + println("Detekt txt results are saved at:\n$diff1\n$diff2") + + performDiff(diff1, diff2) + } + + private fun findJar(root: Path, version: String): Path { + val pattern = Pattern.compile("detekt-cli-$version-all.jar").asPredicate() + return Files.walk(root) + .filter { pattern.test(it.fileName.toString()) } + .findFirst() + .orElseThrow { IllegalArgumentException("no jar with version $version found") } + } + + private fun javaExec(jar: Path, output: Path) { + val command = listOf( + "java", + "-jar", + jar.toString(), + "--input", + analysisPath, + "--build-upon-default-config", + "--fail-fast", + "--config", + configPath, + "--excludes", + "**/resources/**,**/build/**,**/out/**,**/target/**", + "--report", + "txt:$output" + ) + println("Executing ${command.joinToString(" ")}") + ProcessBuilder(command) + .inheritIO() + .start() + .waitFor() + } + + private fun performDiff(diff1: Path, diff2: Path) { + val command = listOf(diffTool, diff1.toString(), diff2.toString()) + val diffResult = Files.createTempFile("detekt", "diff").toFile() + ProcessBuilder(command) + .redirectOutput(diffResult) + .start() + .waitFor() + val diff = diffResult.readText().trim() + if (diff.isNotEmpty()) { + println(diff) + println("There were differences beween results.") + } else { + println("No differences between results.") + } + } +} + +CompareReleases().main(args) diff --git a/scripts/github-milestone-report.main.kts b/scripts/github-milestone-report.main.kts index dbcd542b7..4d531cec0 100755 --- a/scripts/github-milestone-report.main.kts +++ b/scripts/github-milestone-report.main.kts @@ -24,10 +24,9 @@ import java.net.URL class GithubMilestoneReport : CliktCommand() { - // arguments parsing - val user : String by option("-u", help = "Github user or organization. Default: detekt").default("detekt") - val project : String by option("-p", help = "Github project. Default: detekt").default("detekt") - val milestone : Int? by option("-m", help = "Milestone number. Default: latest milestone.").int() + private val user: String by option("-u", help = "Github user or organization. Default: detekt").default("detekt") + private val project: String by option("-p", help = "Github project. Default: detekt").default("detekt") + private val milestone: Int? by option("-m", help = "Milestone number. Default: latest milestone.").int() override fun run() {