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 23, 2024
1 parent 3dff594 commit 3295e43
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 33 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,8 @@ jobs:
distribution: 'zulu'
java-version: 17

- name: Publish to Maven Local
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
- name: Run Invert on Test Projects and Generate GitHub Pages
run: ./scripts/github-pages.main.kts

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

import Github_pages_main.TerminalExecRunnerResult
import java.io.File
import java.io.InputStream
import java.util.concurrent.Executors


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
)

fun readStream(inputStream: InputStream): String {
return inputStream.bufferedReader().use { reader ->
val output = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
println(line) // This prints each line in real-time.
output.append(line).append("\n")
}
output.toString().trim()
}
}

/**
* 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("*****\nBEGIN ${command.joinToString(" ")}")
println("CWD ${workingDir.path}")
val process = ProcessBuilder(command)
.directory(workingDir)
.start()

val executor = Executors.newFixedThreadPool(2)

// Start reading stdout and stderr in separate threads
val stdoutFuture = executor.submit<String> { readStream(process.inputStream) }
val stderrFuture = executor.submit<String> { readStream(process.errorStream) }

// Wait for the process to terminate
val exitCode = process.waitFor()
executor.shutdown()

// Get the outputs from futures
val stdout = stdoutFuture.get()
val stderr = stderrFuture.get()

println("END (Exit Code $exitCode) ${command.joinToString(" ")}\n-----")

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

executeCmd("./scripts/publish-to-maven-local.sh", PROJECT_ROOT_DIR)

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>(
// 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"
// ),
)

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


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

ALL_REPOS.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.canonicalPath} :invert --no-daemon -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.canonicalPath.replace(INVERT_REPORT_DIR.canonicalPath, "").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)
}
2 changes: 1 addition & 1 deletion scripts/github-pages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REPOS=(
"https://github.com/spring-projects/spring-framework"
)

./scripts/publish.sh
./scripts/publish-to-maven-local.sh
./gradlew --init-script invert.gradle :invert

STATIC_SITE_FOLDER="build/static"
Expand Down
30 changes: 30 additions & 0 deletions scripts/publish-to-maven-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

echo "** Building the invert-report so it can be packaged in the invert-plugin"
./gradlew :invert-report:jsBrowserProductionWebpack --no-daemon --no-configuration-cache --rerun-tasks

# Wait 10 second for files to settle before copying them so webpack can finish
sleep 10

echo "** Copy compiled invert-report html & js into invert-plugin"
mkdir -p invert-plugin/src/main/resources/META-INF/
cp invert-report/build/dist/js/invert_web/index.html invert-plugin/src/main/resources/META-INF/index.html
cp invert-report/build/dist/js/invert_web/invert_web.js invert-plugin/src/main/resources/META-INF/invert_web.js

echo "pwd"
pwd

echo "ls invert-report/build"
ls invert-report/build

echo "find invert-report/build/dist"
find invert-report/build/dist

echo "ls invert-report/build/dist/js/invert_web"
ls invert-report/build/dist/js/invert_web

echo "ls invert-plugin/src/main/resources/META-INF"
ls invert-plugin/src/main/resources/META-INF

echo "** Publishing to Maven Local"
./gradlew publishToMavenLocal --no-daemon --no-configuration-cache --rerun-tasks
15 changes: 0 additions & 15 deletions scripts/publish.sh

This file was deleted.

0 comments on commit 3295e43

Please sign in to comment.