diff --git a/.github/workflows/update-metadata.main.kts b/.github/workflows/update-metadata.main.kts index 3cca3f2..5130df5 100755 --- a/.github/workflows/update-metadata.main.kts +++ b/.github/workflows/update-metadata.main.kts @@ -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", @@ -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) { + 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 diff --git a/.github/workflows/update-metadata.yaml b/.github/workflows/update-metadata.yaml index 34a1a2d..4485e76 100644 --- a/.github/workflows/update-metadata.yaml +++ b/.github/workflows/update-metadata.yaml @@ -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' diff --git a/scripts/updateMetadata.main.kts b/scripts/updateMetadata.main.kts deleted file mode 100755 index 2c9d077..0000000 --- a/scripts/updateMetadata.main.kts +++ /dev/null @@ -1,55 +0,0 @@ -#!/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