1
1
/*
2
- * Copyright 2020-2024 Björn Kautler
2
+ * Copyright 2020-2025 Björn Kautler
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package net.kautler
18
18
19
+ import net.kautler.githubactions.DetermineImportedFiles
20
+ import net.kautler.githubactions.PreprocessGithubWorkflow
19
21
import org.gradle.accessors.dm.LibrariesForLibs
20
- import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY
21
- import org.jetbrains.kotlin.cli.common.messages.MessageCollector.Companion.NONE
22
- import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
23
- import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles.JVM_CONFIG_FILES
24
- import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
25
- import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
26
- import org.jetbrains.kotlin.com.intellij.openapi.vfs.local.CoreLocalFileSystem
27
- import org.jetbrains.kotlin.com.intellij.openapi.vfs.local.CoreLocalVirtualFile
28
- import org.jetbrains.kotlin.com.intellij.psi.PsiManager
29
- import org.jetbrains.kotlin.config.CompilerConfiguration
30
- import org.jetbrains.kotlin.psi.KtFile
31
- import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
32
- import org.jetbrains.kotlin.psi.KtStringTemplateExpression
33
- import java.nio.file.Path
34
22
35
23
plugins {
36
24
`java- base`
37
25
}
38
26
27
+ val compilerEmbeddableClasspath by configurations.creating {
28
+ isCanBeConsumed = false
29
+ }
30
+
39
31
val compilerClasspath by configurations.creating {
40
32
isCanBeConsumed = false
41
33
}
@@ -47,6 +39,7 @@ val scriptClasspath by configurations.creating {
47
39
val libs = the<LibrariesForLibs >()
48
40
49
41
dependencies {
42
+ compilerEmbeddableClasspath(libs.workflows.kotlin.compiler.embeddable)
50
43
compilerClasspath(libs.workflows.kotlin.compiler)
51
44
compilerClasspath(libs.workflows.kotlin.scripting.compiler)
52
45
scriptClasspath(libs.workflows.kotlin.main.kts) {
@@ -64,72 +57,23 @@ file(".github/workflows")
64
57
val pascalCasedWorkflowName = workflowName.replace(""" -\w""" .toRegex()) {
65
58
it.value.substring(1 ).replaceFirstChar(Char ::uppercaseChar)
66
59
}.replaceFirstChar(Char ::uppercaseChar)
67
- val preprocessWorkflow = tasks.register<JavaExec >(" preprocess${pascalCasedWorkflowName} Workflow" ) {
68
- group = " github actions"
69
-
70
- inputs
71
- .file(workflowScript)
72
- .withPropertyName(" workflowScript" )
73
- inputs
74
- .files(file(workflowScript).importedFiles)
75
- .withPropertyName(" importedFiles" )
76
- outputs
77
- .file(workflowScript.resolveSibling(" $workflowName .yaml" ))
78
- .withPropertyName(" workflowFile" )
79
-
80
- javaLauncher.set(javaToolchains.launcherFor {
81
- languageVersion.set(JavaLanguageVersion .of(17 ))
82
- })
83
- classpath(compilerClasspath)
84
- mainClass.set(K2JVMCompiler ::class .qualifiedName)
85
- args(" -no-stdlib" , " -no-reflect" )
86
- args(" -classpath" , scriptClasspath.asPath)
87
- args(" -script" , workflowScript.absolutePath)
88
-
89
- // work-around for https://youtrack.jetbrains.com/issue/KT-42101
90
- systemProperty(" kotlin.main.kts.compiled.scripts.cache.dir" , " " )
91
- }
60
+ val determineImportedFiles =
61
+ tasks.register<DetermineImportedFiles >(" determineImportedFilesFor${pascalCasedWorkflowName} Workflow" ) {
62
+ mainKtsFile.set(workflowScript)
63
+ importedFiles.set(layout.buildDirectory.file(" importedFilesFor${pascalCasedWorkflowName} Workflow.txt" ))
64
+ kotlinCompilerEmbeddableClasspath.from(compilerEmbeddableClasspath)
65
+ }
66
+ val preprocessWorkflow =
67
+ tasks.register<PreprocessGithubWorkflow >(" preprocess${pascalCasedWorkflowName} Workflow" ) {
68
+ this .workflowScript.set(workflowScript)
69
+ importedFiles.from(determineImportedFiles.flatMap { it.importedFiles }.map { it.asFile.readLines() })
70
+ kotlinCompilerClasspath.from(compilerClasspath)
71
+ mainKtsClasspath.from(scriptClasspath)
72
+ javaLauncher.set(javaToolchains.launcherFor {
73
+ languageVersion.set(JavaLanguageVersion .of(17 ))
74
+ })
75
+ }
92
76
preprocessWorkflows {
93
77
dependsOn(preprocessWorkflow)
94
78
}
95
79
}
96
-
97
- val File .importedFiles: List <File >
98
- get() = if (! isFile) {
99
- emptyList()
100
- } else {
101
- PsiManager
102
- .getInstance(
103
- KotlinCoreEnvironment
104
- .createForProduction(
105
- Disposer .newDisposable(),
106
- CompilerConfiguration ().apply {
107
- put(MESSAGE_COLLECTOR_KEY , NONE )
108
- },
109
- JVM_CONFIG_FILES
110
- )
111
- .project
112
- )
113
- .findFile(
114
- // work-around for API change between version we compile against and version we run against
115
- // after upgrading Gradle to a version that contains Kotlin 1.9 the embeddable compiler can
116
- // be upgraded to v2 also for compilation and then this can be removed
117
- CoreLocalVirtualFile ::class
118
- .java
119
- .getConstructor(CoreLocalFileSystem ::class .java, Path ::class .java)
120
- .newInstance(CoreLocalFileSystem (), toPath())
121
- )
122
- .let { it as KtFile }
123
- .fileAnnotationList
124
- ?.annotationEntries
125
- ?.asSequence()
126
- ?.filter { it.shortName?.asString() == " Import" }
127
- ?.flatMap { it.valueArgumentList?.arguments ? : emptyList() }
128
- ?.mapNotNull { it.getArgumentExpression() as ? KtStringTemplateExpression }
129
- ?.map { it.entries.first() }
130
- ?.mapNotNull { it as ? KtLiteralStringTemplateEntry }
131
- ?.map { resolveSibling(it.text) }
132
- ?.flatMap { it.importedFiles + it }
133
- ?.toList()
134
- ? : emptyList()
135
- }
0 commit comments