Skip to content

Commit

Permalink
Moving to kts script
Browse files Browse the repository at this point in the history
  • Loading branch information
handstandsam committed Apr 22, 2024
1 parent 3dff594 commit 02d9554
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 13 deletions.
14 changes: 1 addition & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,7 @@ jobs:
run: ./scripts/publish.sh

- name: Run on https://github.com/spring-projects/spring-boot
run: ./scripts/invert-clone-and-run.sh https://github.com/spring-projects/spring-boot

- name: Create HTML
run: |
mkdir -p build/static && echo "
<html><head><link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css' crossorigin='anonymous'></head>
<body><h1>Invert Reports</h1><br/><ul>
<li><a href='invert/invert/index.html'>Invert</a></li>
<li><a href='anvil/invert/index.html'>anvil</a> (https://github.com/square/anvil)</li>
<li><a href='tivi/invert/index.html'>tivi</a> (https://github.com/chrisbanes/tivi)</li>
<li><a href='nowinandroid/invert/index.html'>nowinandroid</a> (https://github.com/android/nowinandroid)</li>
</ul></body></html>
" >> build/static/index.html
run: ./scripts/github-pages.main.kts

- name: Setup Pages
uses: actions/configure-pages@v3
Expand Down
186 changes: 186 additions & 0 deletions scripts/github-pages.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/usr/bin/env kotlin

import Github_pages_main.TerminalExecRunnerResult
import java.io.File

val PROJECT_ROOT_DIR = File(".")
val INVERT_INIT_SCRIPT = File(PROJECT_ROOT_DIR, "invert.gradle")

/**
* Data bundle that includes standard output, error output, and exit code of a process.
*/
data class TerminalExecRunnerResult(
val stdOut: String,
val stdErr: String,
val exitCode: Int
)

/**
* Runs a terminal command that returns [TerminalExecRunnerResult] when the process finishes.
*
* @param [command] shell command split into a list that includes the program and it's arguments.
*/
fun exec(command: List<String>, workingDir: File): TerminalExecRunnerResult {
println("CWD ${workingDir.path}")
println("BEGIN ${command.joinToString(" ")}")
val process = ProcessBuilder(command)
.directory(workingDir)
.start()

val stdout = process
.inputStream
.bufferedReader()
.use { it.readText().also { println(it) } }
.trim()

val stderr = process
.errorStream
.bufferedReader()
.use { it.readText().also { println(it) } }
.trim()

val exitCode = process.waitFor()
println("END (Exit Code $exitCode) ${command.joinToString(" ")}")

return TerminalExecRunnerResult(stdout, stderr, exitCode)
}

/**
* Runs a terminal command that returns [TerminalExecRunnerResult] when the process finishes.
*
* Note: This method should only be used for simple commands, as the command is manually split by
* a space character. This becomes a problem for commands with more complex arguments that include
* spaces. We don't split spaces inside of double quotes, but more advanced scenarios might fail, so
* use at your own risk.
*
* @param [command] shell command as you would enter it in the terminal.
*/
fun executeCmd(command: String, workingDir: File): TerminalExecRunnerResult {
return exec(
// https://stackoverflow.com/a/51356605
// This regex splits strings only outside of double quotes.
command.split(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*\$)".toRegex())
.map {
// Strip surrounding double quotes.
it.trim('"')
},
workingDir
)
}

data class TargetRepo(
val org: String,
val project: String,
val buildDirPath: String = "build",
) {
val url = "https://github.com/$org/$project"
}

val ALL_REPOS = listOf(
TargetRepo(
org = "android",
project = "nowinandroid"
),
TargetRepo(
org = "square",
project = "anvil",
buildDirPath = "build/root-build"
),
TargetRepo(
org = "square",
project = "okhttp"
),
TargetRepo(
org = "chrisbanes",
project = "tivi"
),
TargetRepo(
org = "gradle",
project = "gradle"
),
TargetRepo(
org = "spring-projects",
project = "spring-boot"
),
TargetRepo(
org = "spring-projects",
project = "spring-framework"
),
)

val scannedRepos = ALL_REPOS
// .filter {
// it.org == "square"
// }


val CLONES_DIR = File("build/clones").apply {
if (!exists()) {
mkdirs()
}
}


val STATIC_SITE_FOLDER = File("build/static").apply {
if (exists()) {
deleteRecursively()
}
mkdirs()
}

scannedRepos.forEach { targetRepo ->

val PROJECT_CLONE_DIR = File(CLONES_DIR, "${targetRepo.org}/${targetRepo.project}")

if (!PROJECT_CLONE_DIR.exists()) {
executeCmd(
"git clone --depth=1 ${targetRepo.url}",
PROJECT_CLONE_DIR.parentFile.apply { if (!exists()) mkdirs() }
)
} else {
executeCmd("git pull", PROJECT_CLONE_DIR)
}

// ignoreBuildJavaVersionCheck=true is needed for https://github.com/gradle/gradle for the Java 11/Java 17 mix
executeCmd(
"./gradlew --init-script ${INVERT_INIT_SCRIPT.absolutePath} :invert -Dorg.gradle.ignoreBuildJavaVersionCheck=true",
PROJECT_CLONE_DIR

)

val INVERT_REPORT_DIR = File(PROJECT_CLONE_DIR, "${targetRepo.buildDirPath}/reports/invert")

val OUTPUT_FOLDER = File(STATIC_SITE_FOLDER, "${targetRepo.org}-${targetRepo.project}").apply {
if (exists()) {
deleteRecursively()
}
}
INVERT_REPORT_DIR
.walkTopDown()
.forEach {
val relativePath = it.absolutePath.replace(INVERT_REPORT_DIR.absolutePath, "").drop(1)
val OUTPUT_FILE = File(OUTPUT_FOLDER, relativePath)
it.copyTo(
OUTPUT_FILE,
overwrite = true
)
}
}

val html = buildString {
appendLine("<html>")
appendLine("<head><link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css' crossorigin='anonymous'></head>")
appendLine("<body><h1>Invert results for GitHub Projects</h1><br/><ul>")
STATIC_SITE_FOLDER.listFiles()!!
.mapNotNull { file ->
ALL_REPOS.firstOrNull { repo -> file.name == "${repo.org}-${repo.project}" }
}
.forEach {
println("Found $it")
appendLine("<li><a href='${it.org}-${it.project}/index.html'>${it.org}/${it.project}</a> (${it.url})</li>")
}
appendLine("</ul></body></html>")
}
val HTML_FILE = File(STATIC_SITE_FOLDER, "index.html").apply {
writeText(html)
}

0 comments on commit 02d9554

Please sign in to comment.