Skip to content

Commit

Permalink
Consider imported files in workflow scripts as task inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Jan 26, 2023
1 parent 1833593 commit 372b80f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation("org.kohsuke:github-api".withVersion)
implementation("com.charleskorn.kaml:kaml".withVersion)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime".withVersion)
implementation(embeddedKotlin("compiler-embeddable"))
}

kotlinDslPluginOptions {
Expand Down
59 changes: 57 additions & 2 deletions buildSrc/src/main/kotlin/net/kautler/github_actions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
package net.kautler

import groovy.lang.Closure
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles.JVM_CONFIG_FILES
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.com.intellij.openapi.vfs.local.CoreLocalFileSystem
import org.jetbrains.kotlin.com.intellij.openapi.vfs.local.CoreLocalVirtualFile
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression

plugins {
`java-base`
Expand Down Expand Up @@ -47,8 +59,16 @@ file(".github/workflows")
it.value.substring(1).capitalize()
}.capitalize()
val preprocessWorkflow = tasks.register<JavaExec>("preprocess${camelCasedWorkflowName}Workflow") {
inputs.file(workflowScript)
outputs.file(workflowScript.resolveSibling("$workflowName.yaml"))
inputs
.file(workflowScript)
.withPropertyName("workflowScript")
inputs
.files(file(workflowScript).importedFiles)
.withPropertyName("importedFiles")
outputs
.file(workflowScript.resolveSibling("$workflowName.yaml"))
.withPropertyName("workflowFile")

javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(17))
})
Expand All @@ -62,3 +82,38 @@ file(".github/workflows")
dependsOn(preprocessWorkflow)
}
}

val File.importedFiles: List<File>
get() = if (!isFile) {
emptyList()
} else {
PsiManager
.getInstance(
KotlinCoreEnvironment
.createForProduction(
Disposer.newDisposable(),
CompilerConfiguration().apply {
put(MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
},
JVM_CONFIG_FILES
)
.project
)
.findFile(
CoreLocalVirtualFile(
CoreLocalFileSystem(),
this
)
)
.let { it as KtFile }
.fileAnnotationList
?.annotationEntries
?.filter { it.shortName?.asString() == "Import" }
?.flatMap { it.valueArgumentList?.arguments ?: emptyList() }
?.mapNotNull { it.getArgumentExpression() as? KtStringTemplateExpression }
?.map { it.entries.first() }
?.mapNotNull { it as? KtLiteralStringTemplateEntry }
?.map { resolveSibling(it.text) }
?.flatMap { it.importedFiles + it }
?: emptyList()
}

0 comments on commit 372b80f

Please sign in to comment.