Replace get_analysis_projects.groovy with kotlin script (#4033)

* replace get_analysis_projects.groovy with kotlin script

* fix violations

Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
This commit is contained in:
marschwar
2021-10-12 14:29:01 +02:00
committed by GitHub
parent 5904d2501b
commit fee31b6217
3 changed files with 68 additions and 85 deletions

View File

@@ -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")
}

View File

@@ -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()))