Skip to content

Commit

Permalink
feat(abg)!: allow running generateActionBindings from any directory (#…
Browse files Browse the repository at this point in the history
…1184)

Part of #1071.

Thanks to this, it's possible to e. g. click the green "run" arrow
in IntelliJ to run the script.
  • Loading branch information
krzema12 committed Dec 11, 2023
1 parent 8b8732c commit 12db971
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion action-binding-generator/api/action-binding-generator.api
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class io/github/typesafegithub/workflows/actionbindinggenerator/Fro
}

public final class io/github/typesafegithub/workflows/actionbindinggenerator/GenerateActionBindingsCliHelperKt {
public static final fun generateActionBindings ([Ljava/lang/String;)V
public static final fun generateActionBindings ([Ljava/lang/String;Ljava/nio/file/Path;)V
}

public final class io/github/typesafegithub/workflows/actionbindinggenerator/GenerationKt {
Expand Down
1 change: 1 addition & 0 deletions action-binding-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version = rootProject.version
dependencies {
implementation("com.squareup:kotlinpoet:1.15.3")
implementation("com.charleskorn.kaml:kaml:0.56.0")
implementation(projects.sharedInternal)

testImplementation(projects.githubWorkflowsKt)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
package io.github.typesafegithub.workflows.actionbindinggenerator

import java.io.File
import kotlin.io.path.Path
import io.github.typesafegithub.workflows.shared.internal.findGitRoot
import java.nio.file.Path
import kotlin.io.path.div

/**
* Implements the suggested logic of a script that is called to regenerate action bindings.
*
* @param args Whatever was passed to the script. The exact inputs are opaque and for now are an implementation detail
* of the library, i.e. whatever YAML was generated by the library, it will produce correct input to the script and thus
* this function.
* @param sourceFile Path to the script where this function is called from. Typically a result of an expression like
* [__FILE__.toPath()].
*/
public fun generateActionBindings(args: Array<String>) {
public fun generateActionBindings(
args: Array<String>,
sourceFile: Path,
) {
val workflowYamlFileName: String? =
when {
args.isEmpty() -> null
args.size == 1 -> args[0]
else -> error("At most one argument is supported!")
}

val githubWorkflowsDir = sourceFile.findGitRoot() / ".github" / "workflows"

val allUsedActions =
extractUsedActionsFromWorkflow(
manifest = File(".github/workflows/_used-actions.yaml").readText(),
manifest = (githubWorkflowsDir / "_used-actions.yaml").toFile().readText(),
)

val actionsToGenerateBindingsFor =
workflowYamlFileName?.let {
val actionsUsedInRequestedWorkflow =
extractUsedActionsFromWorkflow(
manifest = File(".github/workflows/$workflowYamlFileName").readText(),
manifest = (githubWorkflowsDir / workflowYamlFileName).toFile().readText(),
)
allUsedActions.intersect(actionsUsedInRequestedWorkflow.toSet())
} ?: allUsedActions

actionsToGenerateBindingsFor.forEach { action ->
Path(".github").resolve("workflows").resolve("generated").resolve(action.owner).resolve("${action.name}.kt").let {
(githubWorkflowsDir / "generated" / action.owner / "${action.name}.kt").let {
it.parent.toFile().mkdirs()
val binding =
action.generateBinding(
Expand Down

0 comments on commit 12db971

Please sign in to comment.