From 85a5d429140e3073fa5df78c453ed62f286b961c Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Mon, 18 Jul 2022 15:25:32 +0200 Subject: [PATCH] wip --- .github/workflows/release.yml | 5 +- CHANGELOG.md | 7 ++ build.gradle.kts | 37 ++++++---- src/commonMain/kotlin/pga/LoginCommand.kt | 21 ------ src/commonMain/kotlin/pga/LogoutCommand.kt | 9 --- src/commonMain/kotlin/pga/MainCommand.kt | 82 +++++++++++++++++++--- src/commonMain/kotlin/pga/PinCommand.kt | 37 ---------- src/commonMain/kotlin/pga/UpdateCommand.kt | 28 -------- src/commonMain/kotlin/pga/github.kt | 2 +- 9 files changed, 108 insertions(+), 120 deletions(-) delete mode 100644 src/commonMain/kotlin/pga/LoginCommand.kt delete mode 100644 src/commonMain/kotlin/pga/LogoutCommand.kt delete mode 100644 src/commonMain/kotlin/pga/PinCommand.kt delete mode 100644 src/commonMain/kotlin/pga/UpdateCommand.kt diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fc1d135..7ae60d1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@d0651293c4a5a52e711f25b41b05b2212f385d28 #v3 - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 #v2.2.1 - uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b #v1 - - run: ./gradlew x64zip arm64zip + - run: ./gradlew distZip env: GRADLE_OPTS: "-Dorg.gradle.daemon=false" - name: Extract release notes @@ -26,7 +26,6 @@ jobs: with: body: ${{ steps.release_notes.outputs.release_notes }} files: | - build/distributions/pin-github-actions-x64.zip - build/distributions/pin-github-actions-arm64.zip + build/distributions/pin-github-actions.zip env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 4867fb4..c920f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] +## [0.3.0] - 2022-07-17 + +### Changed + +- There are no subcommands anymore. It's now possible to omit "pin": `pin-github-actions .` +- The binary is now released as a jar to avoid having to notarize it. + ## [0.2.0] - 2022-07-17 ### Fixed diff --git a/build.gradle.kts b/build.gradle.kts index 088afad..f3e7d5f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,13 +1,14 @@ plugins { id("org.jetbrains.kotlin.multiplatform").version("1.7.10") id("org.jetbrains.kotlin.plugin.serialization").version("1.7.10") + id("distribution") } repositories { mavenCentral() } -tasks.register("fatJar", Jar::class.java) { +tasks.register("allJar", Jar::class.java) { dependsOn("jvmJar") archiveClassifier.set("all") @@ -19,6 +20,14 @@ tasks.register("fatJar", Jar::class.java) { exclude("META-INF/versions/9/module-info.class") } +val startScriptTaskProvider = + tasks.register("createStartScript", org.gradle.jvm.application.tasks.CreateStartScripts::class.java) { + outputDir = file("build/start_scripts/") + mainClass.set("pga.MainKt") + applicationName = "pin-github-actions" + classpath = files(configurations["jvmRuntimeClasspath"], tasks.named("jvmJar").map { it.outputs.files.files }) + } + kotlin { targets { listOf(macosArm64(), macosX64()).forEach { @@ -71,20 +80,22 @@ kotlin { } } -kotlin.targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java) { - binaries.all { - binaryOptions["memoryModel"] = "experimental" +distributions.named("main").configure { + contents { + from(configurations["jvmRuntimeClasspath"]) { + into("lib") + } + from(tasks.named("jvmJar")) { + into("lib") + } + from(startScriptTaskProvider) { + into("bin") + } } } -listOf("X64", "Arm64").forEach { arch -> - tasks.register("${arch.toLowerCase()}zip", Zip::class.java) { - into("pin-github-actions") - from(tasks.named("linkReleaseExecutableMacos$arch")) { - rename { - it.replace(".kexe","") - } - } - archiveClassifier.set(arch.toLowerCase()) +kotlin.targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java) { + binaries.all { + binaryOptions["memoryModel"] = "experimental" } } diff --git a/src/commonMain/kotlin/pga/LoginCommand.kt b/src/commonMain/kotlin/pga/LoginCommand.kt deleted file mode 100644 index ce83eb8..0000000 --- a/src/commonMain/kotlin/pga/LoginCommand.kt +++ /dev/null @@ -1,21 +0,0 @@ -package pga - -import com.github.ajalt.clikt.core.CliktCommand - -class LoginCommand: CliktCommand( - help = "Authenticate your calls to the GitHub APIs to get a higher rate limit" -) { - override fun run() { - println("token: ") - // Uncomment when https://github.com/ajalt/mordant/pull/63 is released - // val token = terminal.readLineOrNull(true) - val token = readPassword() - check (token != null) { - println("Cannot read password") - } - - writeConfig(Config(token)) - - terminal.println("Your calls will now be authenticated") - } -} \ No newline at end of file diff --git a/src/commonMain/kotlin/pga/LogoutCommand.kt b/src/commonMain/kotlin/pga/LogoutCommand.kt deleted file mode 100644 index e5470c4..0000000 --- a/src/commonMain/kotlin/pga/LogoutCommand.kt +++ /dev/null @@ -1,9 +0,0 @@ -package pga - -import com.github.ajalt.clikt.core.CliktCommand - -class LogoutCommand: CliktCommand() { - override fun run() { - deleteConfig() - } -} \ No newline at end of file diff --git a/src/commonMain/kotlin/pga/MainCommand.kt b/src/commonMain/kotlin/pga/MainCommand.kt index de7bed4..95e9f1e 100644 --- a/src/commonMain/kotlin/pga/MainCommand.kt +++ b/src/commonMain/kotlin/pga/MainCommand.kt @@ -2,6 +2,8 @@ package pga import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.subcommands +import com.github.ajalt.clikt.parameters.arguments.argument +import com.github.ajalt.clikt.parameters.arguments.multiple import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.option @@ -9,17 +11,81 @@ class MainCommand: CliktCommand( name = "pin-github-actions", invokeWithoutSubcommand = true ) { - init { - subcommands(PinCommand(), UpdateCommand(), LoginCommand(), LogoutCommand()) - } - private val version by option().flag() + private val login by option().flag() + private val logout by option().flag() + private val update by option(help = "update to the latest known tag instead of just using the current").flag() + + private val paths by argument( + help = "The yaml files/directories to process. You can also pass a directory in which case it will process all yaml files in that directory." + ).multiple() override fun run() { - if (version) { - echo("pin-github-actions $VERSION") - } else { - echo(getFormattedHelp()) + when { + version -> echo("pin-github-actions $VERSION") + login -> login() + logout -> logout() + else -> { + if (paths.isEmpty()) { + echo(getFormattedHelp()) + exitProcess(1) + } + if (update) { + update(paths) + } else { + pin(paths) + } + } } } +} + + +private fun update(paths: List) { + process(paths) { + val tag = getLatestTag(it.owner, it.name) + if (tag == null) { + return@process it + } + + return@process it.copy( + version = tag.sha, + comment = tag.name + ) + } +} + +private fun pin(paths: List) { + process(paths, ::pinCallback) +} + +internal fun pinCallback(actionUsage: ActionUsage): ActionUsage? { + val sha = getSha(actionUsage.owner, actionUsage.name, actionUsage.version) + + return if (sha == null) { + null + } else { + actionUsage.copy( + version = sha, + comment = actionUsage.version + ) + } +} + +private fun login() { + println("token: ") + // Uncomment when https://github.com/ajalt/mordant/pull/63 is released + // val token = terminal.readLineOrNull(true) + val token = readPassword() + check (token != null) { + println("Cannot read password") + } + + writeConfig(Config(token)) + + terminal.println("Your calls will now be authenticated") +} + +private fun logout() { + deleteConfig() } \ No newline at end of file diff --git a/src/commonMain/kotlin/pga/PinCommand.kt b/src/commonMain/kotlin/pga/PinCommand.kt deleted file mode 100644 index f87f406..0000000 --- a/src/commonMain/kotlin/pga/PinCommand.kt +++ /dev/null @@ -1,37 +0,0 @@ -package pga - -import com.github.ajalt.clikt.core.CliktCommand -import com.github.ajalt.clikt.parameters.arguments.argument -import com.github.ajalt.clikt.parameters.arguments.multiple - -class PinCommand : CliktCommand( - name = "pin", - help = "replaces tags and branches references by their actual sha" -) { - private val files by argument( - help = "The yaml files to process. You can also pass a directory in which case it will process all yaml files in that directory." - ).multiple() - - override fun run() { - val paths = if (files.isEmpty()) { - listOf(".github/workflows/") - } else { - files - } - process(paths, ::pinCallback) - } -} - - -internal fun pinCallback(actionUsage: ActionUsage): ActionUsage? { - val sha = getSha(actionUsage.owner, actionUsage.name, actionUsage.version) - - return if (sha == null) { - null - } else { - actionUsage.copy( - version = sha, - comment = actionUsage.version - ) - } -} \ No newline at end of file diff --git a/src/commonMain/kotlin/pga/UpdateCommand.kt b/src/commonMain/kotlin/pga/UpdateCommand.kt deleted file mode 100644 index 72d82ff..0000000 --- a/src/commonMain/kotlin/pga/UpdateCommand.kt +++ /dev/null @@ -1,28 +0,0 @@ -package pga - -import com.github.ajalt.clikt.core.CliktCommand -import com.github.ajalt.clikt.parameters.arguments.argument -import com.github.ajalt.clikt.parameters.arguments.multiple - -class UpdateCommand : CliktCommand( - name = "update", - help = "For each action, update to the latest tag. If no tag exist, leaves the action unchanged" -) { - private val files by argument( - help = "The yaml files to process. You can also pass a directory in which case it will process all yaml files in that directory." - ).multiple(required = true) - - override fun run() { - process(files) { - val tag = getLatestTag(it.owner, it.name) - if (tag == null) { - return@process it - } - - return@process it.copy( - version = tag.sha, - comment = tag.name - ) - } - } -} \ No newline at end of file diff --git a/src/commonMain/kotlin/pga/github.kt b/src/commonMain/kotlin/pga/github.kt index b54e146..3094b3d 100644 --- a/src/commonMain/kotlin/pga/github.kt +++ b/src/commonMain/kotlin/pga/github.kt @@ -81,7 +81,7 @@ private fun getJson(url: String): Any? { println( """You have reached the GitHub unauthenticated rate limit, please either: |- wait $expires - |- or authenticate with `pin-github-actions login` + |- or authenticate with `pin-github-actions --login` """.trimMargin() ) exitProcess(1)