diff --git a/.github/workflows/update-metadata.main.kts b/.github/workflows/update-metadata.main.kts new file mode 100755 index 0000000..7dcd99e --- /dev/null +++ b/.github/workflows/update-metadata.main.kts @@ -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() diff --git a/.github/workflows/update-metadata.yaml b/.github/workflows/update-metadata.yaml new file mode 100644 index 0000000..34a1a2d --- /dev/null +++ b/.github/workflows/update-metadata.yaml @@ -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' diff --git a/scripts/updateMetadata.main.kts b/scripts/updateMetadata.main.kts new file mode 100755 index 0000000..2c9d077 --- /dev/null +++ b/scripts/updateMetadata.main.kts @@ -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) { + 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