Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Jul 18, 2022
1 parent 0e86461 commit 85a5d42
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 120 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 24 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
Expand 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 {
Expand Down Expand Up @@ -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"
}
}
21 changes: 0 additions & 21 deletions src/commonMain/kotlin/pga/LoginCommand.kt

This file was deleted.

9 changes: 0 additions & 9 deletions src/commonMain/kotlin/pga/LogoutCommand.kt

This file was deleted.

82 changes: 74 additions & 8 deletions src/commonMain/kotlin/pga/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,90 @@ 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

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<String>) {
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<String>) {
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()
}
37 changes: 0 additions & 37 deletions src/commonMain/kotlin/pga/PinCommand.kt

This file was deleted.

28 changes: 0 additions & 28 deletions src/commonMain/kotlin/pga/UpdateCommand.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/commonMain/kotlin/pga/github.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 85a5d42

Please sign in to comment.