From fee31b6217e10c83ea176d2e80f64fde4d225e14 Mon Sep 17 00:00:00 2001 From: marschwar Date: Tue, 12 Oct 2021 14:29:01 +0200 Subject: [PATCH] Replace `get_analysis_projects.groovy` with kotlin script (#4033) * replace get_analysis_projects.groovy with kotlin script * fix violations Co-authored-by: Markus Schwarz --- .github/CONTRIBUTING.md | 6 +- scripts/get_analysis_projects.groovy | 82 ---------------------------- scripts/get_analysis_projects.kts | 65 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 85 deletions(-) delete mode 100644 scripts/get_analysis_projects.groovy create mode 100755 scripts/get_analysis_projects.kts diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index e5160c10a..2bca3cf03 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -15,9 +15,9 @@ - ... do not forget to add the new rule to a `RuleSetProvider` (e.g. StyleGuideProvider) - ... do not forget to write a description for the issue of the new rule. - ... add the [correct KDoc](#contents-and-structure-of-a-rules-kdoc) and [annotations](#rule-annotations) to your `Rule` class. This is used to generate documentation pages and the `default-detekt-config.yml` automatically. -- ... do not forget to test the new rule and/or add tests for any changes made to a rule. -Run detekt on itself and other kotlin projects with the `--run-rule RuleSet:RuleId` option to test your rule in isolation. -Make use of the `scripts/get_analysis_projects.groovy` script to automatically establish a set of analysis projects. +- ... do not forget to test the new rule and/or add tests for any changes made to a rule. Run detekt on itself and other + kotlin projects with the `--run-rule RuleSet:RuleId` option to test your rule in isolation. Make use of + the `scripts/get_analysis_projects.kts` script to automatically establish a set of analysis projects. - ... run `./gradlew generateDocumentation` to add your rule and its config options to the `default-detekt-config.yml`. - ... do not forget to run `./gradlew build`. This will execute tests locally. - To print the AST of sources you can pass the `--print-ast` flag to the CLI which will print each diff --git a/scripts/get_analysis_projects.groovy b/scripts/get_analysis_projects.groovy deleted file mode 100644 index b435866fe..000000000 --- a/scripts/get_analysis_projects.groovy +++ /dev/null @@ -1,82 +0,0 @@ -@GrabResolver(name='detekt', root='https://dl.bintray.com/arturbosch/code-analysis/', m2Compatible='true') -@Grab('org.vcsreader:vcsreader:1.1.1') -import org.vcsreader.VcsProject -import org.vcsreader.vcs.VcsError -import org.vcsreader.vcs.git.GitVcsRoot - -import java.nio.file.Files -import java.nio.file.Paths - -HELP_MESSAGE = """ -Usage: groovy get_analysis_projects.groovy [/path/to/storing/folder] -""" - -def projects = [ - "git@github.com:arrow-kt/arrow.git", - "git@github.com:shyiko/ktlint.git", - "git@github.com:vanniktech/gradle-dependency-graph-generator-plugin.git", - "git@github.com:vanniktech/lint-rules.git", - "git@github.com:vanniktech/junit-rules.git", - "git@github.com:spekframework/spek.git", - "git@github.com:Kotlin/kotlinx.coroutines.git", - "git@github.com:kotlintest/kotlintest.git", - "git@github.com:tipsy/javalin.git", - "git@github.com:arturbosch/ksh.git", - "git@github.com:arturbosch/kutils.git", - "git@github.com:arturbosch/deps.git", - "git@github.com:detekt/detekt.git", - "git@github.com:detekt/sonar-kotlin.git" -] - -if (args.size() == 0) { - println(HELP_MESSAGE) - System.exit(1) -} - -def storingFolder = Paths.get(args[0]) - -if (Files.notExists(storingFolder)) { - Files.createDirectories(storingFolder) -} - -def gits = projects.collect { asGitRepo(storingFolder, it) } -new VcsProject(gits) // sets observers, prevents NPE -gits.parallelStream().forEach { handleProject(it) } - -static asGitRepo(root, gitUrl) { - def index = gitUrl.indexOf("/") + 1 - def name = gitUrl.substring(index) - index = name.indexOf(".") - name = name.substring(0, index) - new GitVcsRoot(root.resolve(name).toString(), gitUrl) -} - -static handleProject(git) { - def filePath = Paths.get(git.repoFolder()) - def fileName = filePath.fileName.toString() - - try { - if (Files.exists(filePath)) { - def result = git.update() - if (!result.isSuccessful()) { - throw new IllegalStateException(extractVcsErrors(result.exceptions())) - } - println("Updated existing repo $fileName") - } else { - def cloneResult = git.cloneIt() - if (!cloneResult.isSuccessful()) { - throw new IllegalStateException(extractVcsErrors(cloneResult.exceptions())) - } - println("Finished cloning $fileName") - } - } catch (Exception ex) { - println("Error while handling $fileName: \n$ex.message") - } -} - -static String extractVcsErrors(exceptions) { - exceptions.stream() - .filter { VcsError.isInstance(it) } - .collect { it.message } - .join("\n") -} diff --git a/scripts/get_analysis_projects.kts b/scripts/get_analysis_projects.kts new file mode 100755 index 000000000..12d48d19a --- /dev/null +++ b/scripts/get_analysis_projects.kts @@ -0,0 +1,65 @@ +#!kotlinc -script + +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit.MINUTES + +val githubProjects = listOf( + "arrow-kt/arrow", + "pintrest/ktlint", + "vanniktech/gradle-dependency-graph-generator-plugin", + "vanniktech/lint-rules", + "vanniktech/junit-rules", + "spekframework/spek", + "Kotlin/kotlinx.coroutines", + "Kotlin/kotlinx.serialization", + "Kotlin/kotlinx-datetime", + "Kotlin/dokka", + "kotest/kotest", + "tipsy/javalin", +) + +class Downloader(private val basePath: Path, private val project: String) { + + fun download() { + if (Files.exists(basePath.resolve(project.substringAfter('/')))) { + println("Skipping $project as it already exists.") + return + } + println("Downloading $project") + cloneRepo("https://github.com/$project.git") + } + + private fun cloneRepo(repo: String) { + ProcessBuilder("git", "clone", repo) + .directory(basePath.toFile()) + .inheritIO() + .start() + .waitFor(MAX_CLONE_WAIT_MINUTES, MINUTES) + } + + companion object { + const val MAX_CLONE_WAIT_MINUTES: Long = 5 + } +} + +fun downloadAnalysisProjects(basePath: Path) { + println("Downloading analysis projects to ${basePath.toAbsolutePath()}") + Files.createDirectories(basePath) + + githubProjects.forEach { project -> + Downloader(basePath, project).download() + } +} + +if (args.size != 1) { + println( + """ + Usage: ./get_analysis_projects.kts [/path/to/storing/folder] + or kotlinc -script get_analysis_projects.kts [/path/to/storing/folder] + """.trimIndent() + ) + System.exit(1) +} + +downloadAnalysisProjects(Path.of(args.first()))