Skip to content

Commit

Permalink
Regenerate metadata on every push to main (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzema12 authored Dec 20, 2023
1 parent 9c26a3d commit de74a55
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/update-metadata.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env kotlin
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.7.0")

import io.github.typesafegithub.workflows.actions.actions.CheckoutV4
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

workflow(
name = "Update metadata",
sourceFile = __FILE__.toPath(),
on = listOf(Push(branches = listOf("main"))),
) {
job(
id = "generate",
runsOn = RunnerType.UbuntuLatest,
) {
uses(action = CheckoutV4())
run(
name = "Configure git",
command = """
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
""".trimIndent()
)
run(
name = "Run generation logic",
command = "scripts/updateMetadata.main.kts",
)
run(
name = "Push new commit",
command = "git push",
)
}
}.writeToFile()
41 changes: 41 additions & 0 deletions .github/workflows/update-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This file was generated using Kotlin DSL (.github/workflows/update-metadata.main.kts).
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
# Generated with https://github.com/typesafegithub/github-workflows-kt

name: 'Update metadata'
on:
push:
branches:
- 'main'
jobs:
check_yaml_consistency:
name: 'Check YAML consistency'
runs-on: 'ubuntu-latest'
steps:
- id: 'step-0'
name: 'Check out'
uses: 'actions/checkout@v4'
- id: 'step-1'
name: 'Execute script'
run: 'rm ''.github/workflows/update-metadata.yaml'' && ''.github/workflows/update-metadata.main.kts'''
- id: 'step-2'
name: 'Consistency check'
run: 'git diff --exit-code ''.github/workflows/update-metadata.yaml'''
generate:
runs-on: 'ubuntu-latest'
needs:
- 'check_yaml_consistency'
steps:
- id: 'step-0'
uses: 'actions/checkout@v4'
- id: 'step-1'
name: 'Configure git'
run: |-
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
- id: 'step-2'
name: 'Run generation logic'
run: 'scripts/updateMetadata.main.kts'
- id: 'step-3'
name: 'Push new commit'
run: 'git push'
55 changes: 55 additions & 0 deletions scripts/updateMetadata.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env kotlin
@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 org.eclipse.jgit.api.Git
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

removeMetadataFiles()
generateMetadataFiles()
commitChanges()

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

0 comments on commit de74a55

Please sign in to comment.