Skip to content

Commit d884a58

Browse files
committed
Merge branch 'release/0.0.8'
2 parents 94db706 + e4dbb90 commit d884a58

35 files changed

+305
-132
lines changed

_build/parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-root</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../../pom.xml</relativePath>
1010
</parent>
1111

_build/report-generator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-parent</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../parent/pom.xml</relativePath>
1010
</parent>
1111

_itest/builder-itest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-itest-root</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
</parent>
1010

1111
<groupId>io.toolisticon.kotlin.generation.itest</groupId>

_itest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-parent</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../_build/parent/pom.xml</relativePath>
1010
</parent>
1111

_itest/spi-itest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-itest-root</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
</parent>
1010

1111
<groupId>io.toolisticon.kotlin.generation.itest</groupId>

kotlin-code-generation-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-root</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
</parent>
1010

1111
<groupId>io.toolisticon.kotlin.generation</groupId>

kotlin-code-generation-test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-parent</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../_build/parent/pom.xml</relativePath>
1010
</parent>
1111

kotlin-code-generation-test/src/main/kotlin/model/KotlinCompilationCommand.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.toolisticon.kotlin.generation.test.model
33
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
44
import com.tschuchort.compiletesting.SourceFile
55
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
6-
import io.toolisticon.kotlin.generation.spec.KotlinFileSpecs
6+
import io.toolisticon.kotlin.generation.spec.KotlinFileSpecList
77
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.sourceFile
88
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
99

@@ -13,10 +13,10 @@ import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
1313
@ExperimentalKotlinPoetApi
1414
@ExperimentalCompilerApi
1515
data class KotlinCompilationCommand(
16-
val fileSpecs: KotlinFileSpecs
16+
val fileSpecs: KotlinFileSpecList
1717
) {
1818

19-
constructor(fileSpec: KotlinFileSpec) : this(KotlinFileSpecs(fileSpec))
19+
constructor(fileSpec: KotlinFileSpec) : this(KotlinFileSpecList(fileSpec))
2020

2121
operator fun plus(fileSpec: KotlinFileSpec) = copy(fileSpecs = fileSpecs + fileSpec)
2222

kotlin-code-generation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.toolisticon.kotlin.generation._</groupId>
77
<artifactId>kotlin-code-generation-parent</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../_build/parent/pom.xml</relativePath>
1010
</parent>
1111

kotlin-code-generation/src/main/kotlin/KotlinCodeGeneration.kt

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.propertyBui
2121
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.runtimeExceptionClassBuilder
2222
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.typeAliasBuilder
2323
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.valueClassBuilder
24-
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.className
2524
import io.toolisticon.kotlin.generation.builder.*
2625
import io.toolisticon.kotlin.generation.builder.extra.*
2726
import io.toolisticon.kotlin.generation.builder.extra.DelegateMapValueClassSpecBuilder.Companion.DEFAULT_KEY_TYPE
2827
import io.toolisticon.kotlin.generation.poet.FormatSpecifier.asCodeBlock
2928
import io.toolisticon.kotlin.generation.spec.*
3029
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContext
30+
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContextFactory
3131
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationSpiRegistry
32+
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationStrategy
3233
import io.toolisticon.kotlin.generation.spi.registry.KotlinCodeGenerationServiceLoader
33-
import io.toolisticon.kotlin.generation.spi.strategy.KotlinFileSpecStrategy
3434
import io.toolisticon.kotlin.generation.spi.strategy.executeAll
3535
import io.toolisticon.kotlin.generation.support.SUPPRESS_MEMBER_VISIBILITY_CAN_BE_PRIVATE
3636
import mu.KLogging
3737
import kotlin.reflect.KClass
38+
import kotlin.reflect.full.isSubclassOf
3839

3940
/**
4041
* Kotlin Code Generation is a wrapper lib for kotlin poet. This is the central class that allows access to builders and tools via simple static helpers.
@@ -559,34 +560,65 @@ object KotlinCodeGeneration : KLogging() {
559560
const val NBSP = "·"
560561
}
561562

563+
/**
564+
* Generator Function that takes a context and an input, finds matching strategies and generates source file(s).
565+
*
566+
* Invokes the contextFactory and calls `generateFiles(context, input)`.
567+
*
568+
* @param INPUT the type of the input (base source of generation)
569+
* @param CONTEXT the context (containing registry, ...) used for generation.
570+
* @param input the instance of the input
571+
* @param contextFactory factory fn to create the context (containing the spi registry) used for generation from input
572+
* @return [KotlinFileSpecList] containing the generated files
573+
* @throws IllegalStateException when no matching strategy is found.
574+
*/
575+
inline fun <reified CONTEXT : KotlinCodeGenerationContext<CONTEXT>, reified INPUT : Any> generateFiles(
576+
contextFactory: KotlinCodeGenerationContextFactory<CONTEXT,INPUT>,
577+
input: INPUT
578+
): KotlinFileSpecList = generateFiles(context = contextFactory.invoke(input), input = input)
562579

563580
/**
564-
* Generator Function that takes an input and generates source file(s).
581+
* Generator Function that takes a context and an input, finds matching strategies and generates source file(s).
565582
*
566583
* @param INPUT the type of the input (base source of generation)
567584
* @param CONTEXT the context (containing registry, ...) used for generation.
568-
* @param STRATEGY the [KotlinFileSpecStrategy] to apply (using `executeAll()`
569585
* @param input the instance of the input
570-
* @param contextFactory fn that creates the context based on input.
571-
* @return list of [KotlinFileSpec]
572-
* @throws IllegalStateException when no strategy is found.
573-
*/
574-
inline fun <INPUT : Any,
575-
CONTEXT : KotlinCodeGenerationContext<CONTEXT>,
576-
reified STRATEGY : KotlinFileSpecStrategy<CONTEXT, INPUT>> generateFiles(
577-
input: INPUT,
578-
contextFactory: (INPUT) -> CONTEXT,
579-
): List<KotlinFileSpec> {
580-
val context = contextFactory.invoke(input)
581-
val strategies: List<STRATEGY> = context.registry.strategies.filter(STRATEGY::class).mapNotNull {
586+
* @param context the context (containing the spi registry) used for generation
587+
* @return [KotlinFileSpecList] containing the generated files
588+
* @throws IllegalStateException when no matching strategy is found.
589+
*/
590+
inline fun <reified CONTEXT : KotlinCodeGenerationContext<CONTEXT>, reified INPUT : Any> generateFiles(
591+
context: CONTEXT,
592+
input: INPUT
593+
): KotlinFileSpecList {
594+
595+
val strategyCandidates = context.registry.strategies.filter { it.specType.isSubclassOf(KotlinFileSpecIterable::class) }
596+
.filter { it.contextType.isSubclassOf(CONTEXT::class) }
597+
.filter { it.inputType.isSubclassOf(INPUT::class) }
598+
.map {
599+
@Suppress("UNCHECKED_CAST")
600+
it as KotlinCodeGenerationStrategy<CONTEXT, INPUT, KotlinFileSpecIterable>
601+
}
602+
603+
// find all matching strategies
604+
val matchingStrategies: List<KotlinCodeGenerationStrategy<CONTEXT, INPUT, KotlinFileSpecIterable>> = strategyCandidates.mapNotNull {
582605
if (it.test(context, input)) {
583606
it
584607
} else {
585608
logger.info { "strategy-filter: removing ${it.name}" }
586609
null
587610
}
611+
}.also {
612+
check(it.isNotEmpty()) { "No applicable strategy found/filtered for context=`${CONTEXT::class.simpleName}`, input=`${input::class.simpleName}`." }
613+
}
614+
615+
// generate files
616+
val sourceFiles = matchingStrategies.executeAll(context, input).flatten().also {
617+
check(it.isNotEmpty()) { "No files where generated for context=`${CONTEXT::class.simpleName}`, input=`${input::class.simpleName}`." }
588618
}
589-
check(strategies.isNotEmpty()) { "No applicable strategy found/filtered for `${STRATEGY::class}`." }
590-
return context.registry.strategies.filter(STRATEGY::class).executeAll(context, input)
619+
620+
// wrap to spec list
621+
return KotlinFileSpecList(sourceFiles)
591622
}
623+
592624
}

0 commit comments

Comments
 (0)