Skip to content

Commit a3a00ac

Browse files
authored
refactor: prepare for generating more than one binding class (#1586)
Part of #1585. Thanks to this, we'll be able to return e.g. `Checkout` and `Checkout_Untyped` classes in a single JAR.
1 parent 0bea0bf commit a3a00ac

4 files changed

Lines changed: 49 additions & 40 deletions

File tree

action-binding-generator/api/action-binding-generator.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public final class io/github/typesafegithub/workflows/actionbindinggenerator/gen
7272
}
7373

7474
public final class io/github/typesafegithub/workflows/actionbindinggenerator/generation/GenerationKt {
75-
public static final fun generateBinding (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;)Lio/github/typesafegithub/workflows/actionbindinggenerator/generation/ActionBinding;
76-
public static synthetic fun generateBinding$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/actionbindinggenerator/generation/ActionBinding;
75+
public static final fun generateBinding (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;)Ljava/util/List;
76+
public static synthetic fun generateBinding$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;ILjava/lang/Object;)Ljava/util/List;
7777
}
7878

7979
public final class io/github/typesafegithub/workflows/actionbindinggenerator/metadata/Input {

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/Generation.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public fun ActionCoords.generateBinding(
5757
metadataRevision: MetadataRevision,
5858
metadata: Metadata? = null,
5959
inputTypings: Pair<Map<String, Typing>, TypingActualSource?>? = null,
60-
): ActionBinding? {
61-
val metadataResolved = metadata ?: this.fetchMetadata(metadataRevision) ?: return null
60+
): List<ActionBinding> {
61+
val metadataResolved = metadata ?: this.fetchMetadata(metadataRevision) ?: return emptyList()
6262
val metadataProcessed = metadataResolved.removeDeprecatedInputsIfNameClash()
6363

6464
val inputTypingsResolved = inputTypings ?: this.provideTypes(metadataRevision)
@@ -67,12 +67,14 @@ public fun ActionCoords.generateBinding(
6767
val actionBindingSourceCode =
6868
generateActionBindingSourceCode(metadataProcessed, this, inputTypingsResolved.first, className)
6969
val packageName = owner.toKotlinPackageName()
70-
return ActionBinding(
71-
kotlinCode = actionBindingSourceCode,
72-
filePath = "kotlin/io/github/typesafegithub/workflows/actions/$packageName/$className.kt",
73-
className = className,
74-
packageName = packageName,
75-
typingActualSource = inputTypingsResolved.second,
70+
return listOf(
71+
ActionBinding(
72+
kotlinCode = actionBindingSourceCode,
73+
filePath = "kotlin/io/github/typesafegithub/workflows/actions/$packageName/$className.kt",
74+
className = className,
75+
packageName = packageName,
76+
typingActualSource = inputTypingsResolved.second,
77+
),
7678
)
7779
}
7880

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/GenerationTest.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.typing.IntegerW
1515
import io.github.typesafegithub.workflows.actionbindinggenerator.typing.ListOfTypings
1616
import io.github.typesafegithub.workflows.actionbindinggenerator.typing.StringTyping
1717
import io.kotest.core.spec.style.FunSpec
18-
import io.kotest.matchers.shouldNotBe
18+
import io.kotest.matchers.collections.shouldHaveSize
1919

2020
class GenerationTest :
2121
FunSpec({
@@ -143,8 +143,8 @@ class GenerationTest :
143143
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
144144

145145
// then
146-
binding shouldNotBe null
147-
binding?.shouldMatchFile("SimpleActionWithRequiredStringInputs.kt")
146+
binding shouldHaveSize 1
147+
binding.first().shouldMatchFile("SimpleActionWithRequiredStringInputs.kt")
148148
}
149149

150150
test("action with various combinations of input parameters describing being required or optional") {
@@ -189,8 +189,8 @@ class GenerationTest :
189189
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
190190

191191
// then
192-
binding shouldNotBe null
193-
binding?.shouldMatchFile("ActionWithSomeOptionalInputs.kt")
192+
binding shouldHaveSize 1
193+
binding.first().shouldMatchFile("ActionWithSomeOptionalInputs.kt")
194194
}
195195

196196
test("action with all types of inputs") {
@@ -210,8 +210,8 @@ class GenerationTest :
210210
)
211211

212212
// then
213-
binding shouldNotBe null
214-
binding?.shouldMatchFile("ActionWithAllTypesOfInputs.kt")
213+
binding shouldHaveSize 1
214+
binding.first().shouldMatchFile("ActionWithAllTypesOfInputs.kt")
215215
}
216216

217217
test("action with outputs") {
@@ -241,8 +241,8 @@ class GenerationTest :
241241
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
242242

243243
// then
244-
binding shouldNotBe null
245-
binding?.shouldMatchFile("ActionWithOutputs.kt")
244+
binding shouldHaveSize 1
245+
binding.first().shouldMatchFile("ActionWithOutputs.kt")
246246
}
247247

248248
test("action with no inputs") {
@@ -261,8 +261,8 @@ class GenerationTest :
261261
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
262262

263263
// then
264-
binding shouldNotBe null
265-
binding?.shouldMatchFile("ActionWithNoInputs.kt")
264+
binding shouldHaveSize 1
265+
binding.first().shouldMatchFile("ActionWithNoInputs.kt")
266266
}
267267

268268
test("action with deprecated input resolving to the same Kotlin field name") {
@@ -295,8 +295,8 @@ class GenerationTest :
295295
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
296296

297297
// then
298-
binding shouldNotBe null
299-
binding?.shouldMatchFile("ActionWithDeprecatedInputAndNameClash.kt")
298+
binding shouldHaveSize 1
299+
binding.first().shouldMatchFile("ActionWithDeprecatedInputAndNameClash.kt")
300300
}
301301

302302
test("action with inputs sharing type") {
@@ -343,8 +343,8 @@ class GenerationTest :
343343
)
344344

345345
// then
346-
binding shouldNotBe null
347-
binding?.shouldMatchFile("ActionWithInputsSharingType.kt")
346+
binding shouldHaveSize 1
347+
binding.first().shouldMatchFile("ActionWithInputsSharingType.kt")
348348
}
349349

350350
test("action with input descriptions with fancy characters") {
@@ -371,7 +371,7 @@ class GenerationTest :
371371
val binding = coords.generateBinding(metadataRevision = NewestForVersion, metadata = actionManifest)
372372

373373
// then
374-
binding shouldNotBe null
375-
binding?.shouldMatchFile("ActionWithFancyCharsInDocs.kt")
374+
binding shouldHaveSize 1
375+
binding.first().shouldMatchFile("ActionWithFancyCharsInDocs.kt")
376376
}
377377
})

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/JarBuilding.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ internal fun buildJars(
3131
name: String,
3232
version: String,
3333
): Jars? {
34-
val binding = generateBinding(owner = owner, name = name, version = version) ?: return null
35-
val (sourceFilePath, compilationInputDir) = binding.prepareDirectoryWithSources()
34+
val binding =
35+
generateBinding(owner = owner, name = name, version = version).also {
36+
if (it.isEmpty()) return null
37+
}
38+
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
3639

37-
val pathWithJarContents = compileBinding(sourceFilePath = sourceFilePath)
40+
val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
3841
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
3942
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
4043

@@ -51,7 +54,7 @@ private fun generateBinding(
5154
owner: String,
5255
name: String,
5356
version: String,
54-
): ActionBinding? {
57+
): List<ActionBinding> {
5558
val actionCoords =
5659
ActionCoords(
5760
owner = owner,
@@ -63,14 +66,14 @@ private fun generateBinding(
6366
)
6467
}
6568

66-
private fun compileBinding(sourceFilePath: Path): Path {
69+
private fun compileBinding(sourceFilePaths: List<Path>): Path {
6770
val compilationOutput = createTempDirectory()
6871

6972
val args =
7073
K2JVMCompilerArguments().apply {
7174
destination = compilationOutput.toString()
7275
classpath = System.getProperty("java.class.path")
73-
freeArgs = listOf(sourceFilePath.toString())
76+
freeArgs = sourceFilePaths.map { it.toString() }
7477
noStdlib = true
7578
noReflect = true
7679
includeRuntime = false
@@ -94,12 +97,16 @@ private fun compileBinding(sourceFilePath: Path): Path {
9497
return compilationOutput
9598
}
9699

97-
private fun ActionBinding.prepareDirectoryWithSources(): Pair<Path, Path> {
100+
private fun List<ActionBinding>.prepareDirectoryWithSources(): Pair<List<Path>, Path> {
98101
val directory = createTempDirectory()
99-
val sourceFilePath = directory / Path(this.filePath)
100-
sourceFilePath.also {
101-
it.createParentDirectories()
102-
it.writeText(this.kotlinCode)
103-
}
104-
return Pair(sourceFilePath, directory)
102+
val sourceFilePaths =
103+
this
104+
.map { binding ->
105+
val sourceFilePath = directory / Path(binding.filePath)
106+
sourceFilePath.also {
107+
it.createParentDirectories()
108+
it.writeText(binding.kotlinCode)
109+
}
110+
}
111+
return Pair(sourceFilePaths, directory)
105112
}

0 commit comments

Comments
 (0)