Skip to content

Commit

Permalink
Use Kotlin logic step to inline the script
Browse files Browse the repository at this point in the history
  • Loading branch information
krzema12 committed Jan 4, 2024
1 parent 2a45b2a commit 1f94a9e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 60 deletions.
60 changes: 56 additions & 4 deletions .github/workflows/update-metadata.main.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/usr/bin/env kotlin
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.9.0")
@file:DependsOn("it.krzeminski:snakeyaml-engine-kmp-jvm:2.7.1")
@file:DependsOn("org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r")

import io.github.typesafegithub.workflows.actions.actions.CheckoutV4
import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep
import io.github.typesafegithub.workflows.domain.RunnerType
import io.github.typesafegithub.workflows.dsl.workflow
import io.github.typesafegithub.workflows.domain.triggers.Push
import io.github.typesafegithub.workflows.yaml.writeToFile
import org.snakeyaml.engine.v2.api.Dump
import org.snakeyaml.engine.v2.api.DumpSettings
import org.snakeyaml.engine.v2.common.ScalarStyle
import java.io.File
import org.eclipse.jgit.api.Git

workflow(
name = "Update metadata",
Expand All @@ -24,13 +32,57 @@ workflow(
git config user.name "github-actions[bot]"
""".trimIndent()
)
run(
name = "Run generation logic",
command = "scripts/updateMetadata.main.kts",
)
@OptIn(ExperimentalKotlinLogicStep::class)
run(name = "Run generation logic") {
removeMetadataFiles()
generateMetadataFiles()
commitChanges()
}
run(
name = "Push new commit",
command = "git push",
)
}
}.writeToFile()

fun removeMetadataFiles() {
File("typings").walk()
.filter { it.name == "metadata.yml" }
.forEach { it.delete() }
}

fun generateMetadataFiles() {
File("typings").walk()
.filter { it.isActionRootDir() }
.forEach { actionRootDir ->
val versionsWithTypings = actionRootDir.listFiles()
.filter { it.isDirectory }
.map { it.name }
.sortedBy { it.removePrefix("v") }
writeToMetadataFile(actionRootDir, versionsWithTypings)
}
}

fun commitChanges() {
Git.open(File(".")).apply {
add().addFilepattern("typings/").call()

if (status().call().hasUncommittedChanges()) {
commit().setMessage("Update metadata").call()
}
}
}

fun writeToMetadataFile(actionRootDir: File, versionsWithTypings: List<String>) {
val structureToDump = mapOf(
"versionsWithTypings" to versionsWithTypings
)
val dumpSettings = DumpSettings.builder()
.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED)
.build()
val dump = Dump(dumpSettings)
val yamlAsString = dump.dumpToString(structureToDump)
actionRootDir.resolve("metadata.yml").writeText(yamlAsString)
}

fun File.isActionRootDir() = path.split("/").filter { it.isNotBlank() }.size == 3
2 changes: 1 addition & 1 deletion .github/workflows/update-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
git config user.name "github-actions[bot]"
- id: 'step-2'
name: 'Run generation logic'
run: 'scripts/updateMetadata.main.kts'
run: 'GHWKT_RUN_STEP=''generate:step-2'' .github/workflows/update-metadata.main.kts'
- id: 'step-3'
name: 'Push new commit'
run: 'git push'
55 changes: 0 additions & 55 deletions scripts/updateMetadata.main.kts

This file was deleted.

0 comments on commit 1f94a9e

Please sign in to comment.