-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Regenerate metadata on every push to main (#4)
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |