Optional include
This commit is contained in:
1 parent
21556018c8
commit
9a288d7728
3 files changed
+28
-6
No files matched your search
@@ -38,4 +38,13 @@ class ClassScanner {
|
|||||||
|
|
||||||
return classes
|
return classes
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
fun scan(packageName: String, includeClasses: Collection<String>, classLoader: ClassLoader): List<KClass<*>> =
|
||||||
|
(scan(packageName, classLoader) + includeClasses.map { className ->
|
||||||
|
try {
|
||||||
|
Class.forName(className, false, classLoader).kotlin
|
||||||
|
} catch (e: ClassNotFoundException) {
|
||||||
|
throw IllegalArgumentException("Included class '$className' was not found", e)
|
||||||
|
}
|
||||||
|
}).distinctBy { it.qualifiedName }
|
||||||
|
}
|
||||||
@@ -15,10 +15,19 @@ object TsGenerator {
|
|||||||
output: File,
|
output: File,
|
||||||
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
||||||
writeSealedStrategy: SealedStrategy,
|
writeSealedStrategy: SealedStrategy,
|
||||||
|
) = generate(packageName, emptyList(), output, classLoader, writeSealedStrategy)
|
||||||
|
|
||||||
|
fun generate(
|
||||||
|
packageName: String,
|
||||||
|
includeClasses: Collection<String>,
|
||||||
|
output: File,
|
||||||
|
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
||||||
|
writeSealedStrategy: SealedStrategy,
|
||||||
) {
|
) {
|
||||||
println("TsGenerator: scanning package: $packageName")
|
println("TsGenerator: scanning package: $packageName")
|
||||||
|
if (includeClasses.isNotEmpty()) println("TsGenerator: including classes: ${includeClasses.joinToString()}")
|
||||||
|
|
||||||
val classes = ClassScanner().scan(packageName, classLoader)
|
val classes = ClassScanner().scan(packageName, includeClasses, classLoader)
|
||||||
|
|
||||||
println("TsGenerator: found ${classes.size} classes total")
|
println("TsGenerator: found ${classes.size} classes total")
|
||||||
classes.forEach { println(" - ${it.qualifiedName}") }
|
classes.forEach { println(" - ${it.qualifiedName}") }
|
||||||
@@ -27,7 +36,7 @@ object TsGenerator {
|
|||||||
appendLine("// AUTO-GENERATED. DO NOT EDIT.")
|
appendLine("// AUTO-GENERATED. DO NOT EDIT.")
|
||||||
appendLine("// TSGenerator Version: $versionInfo")
|
appendLine("// TSGenerator Version: $versionInfo")
|
||||||
appendLine("// Time: $buildTime")
|
appendLine("// Time: $buildTime")
|
||||||
appendLine("// Source: $packageName")
|
appendLine("// Source: $packageName${if (includeClasses.isEmpty()) "" else "; extra classes: ${includeClasses.joinToString()}"}")
|
||||||
appendLine()
|
appendLine()
|
||||||
|
|
||||||
classes.forEach { cls ->
|
classes.forEach { cls ->
|
||||||
@@ -95,4 +104,4 @@ object TsGenerator {
|
|||||||
|
|
||||||
println("TsGenerator: wrote file to ${output.absolutePath}")
|
println("TsGenerator: wrote file to ${output.absolutePath}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,13 @@ import org.gradle.api.Plugin
|
|||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.plugins.JavaPluginExtension
|
import org.gradle.api.plugins.JavaPluginExtension
|
||||||
import org.gradle.api.provider.Property
|
import org.gradle.api.provider.Property
|
||||||
|
import org.gradle.api.provider.ListProperty
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
import java.net.URLClassLoader
|
||||||
|
|
||||||
interface TsGeneratorExtension {
|
interface TsGeneratorExtension {
|
||||||
val packageName: Property<String>
|
val packageName: Property<String>
|
||||||
|
val includeClasses: ListProperty<String>
|
||||||
val outputFile: Property<File>
|
val outputFile: Property<File>
|
||||||
|
|
||||||
val sealedStrategy: Property<SealedStrategy>
|
val sealedStrategy: Property<SealedStrategy>
|
||||||
@@ -20,6 +22,7 @@ class TsGeneratorPlugin : Plugin<Project> {
|
|||||||
override fun apply(project: Project) {
|
override fun apply(project: Project) {
|
||||||
val extension = project.extensions.create("tsGenerator", TsGeneratorExtension::class.java).apply {
|
val extension = project.extensions.create("tsGenerator", TsGeneratorExtension::class.java).apply {
|
||||||
sealedStrategy.convention(SealedStrategy.AS_INTERFACE_WITH_TYPE)
|
sealedStrategy.convention(SealedStrategy.AS_INTERFACE_WITH_TYPE)
|
||||||
|
includeClasses.convention(emptyList())
|
||||||
}
|
}
|
||||||
|
|
||||||
project.tasks.register("generateTs") {
|
project.tasks.register("generateTs") {
|
||||||
@@ -30,6 +33,7 @@ class TsGeneratorPlugin : Plugin<Project> {
|
|||||||
TsGenerator.buildTime = java.time.Instant.now().toString()
|
TsGenerator.buildTime = java.time.Instant.now().toString()
|
||||||
|
|
||||||
val pkg = extension.packageName.get()
|
val pkg = extension.packageName.get()
|
||||||
|
val includedClasses = extension.includeClasses.get()
|
||||||
val out = extension.outputFile.get()
|
val out = extension.outputFile.get()
|
||||||
val writeSealedStrategy = extension.sealedStrategy.get()
|
val writeSealedStrategy = extension.sealedStrategy.get()
|
||||||
|
|
||||||
@@ -41,8 +45,8 @@ class TsGeneratorPlugin : Plugin<Project> {
|
|||||||
val cl = URLClassLoader(urls, TsGenerator::class.java.classLoader)
|
val cl = URLClassLoader(urls, TsGenerator::class.java.classLoader)
|
||||||
|
|
||||||
// Kall generatoren med det nye flagget
|
// Kall generatoren med det nye flagget
|
||||||
TsGenerator.generate(pkg, out, cl, writeSealedStrategy)
|
TsGenerator.generate(pkg, includedClasses, out, cl, writeSealedStrategy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user