diff --git a/src/main/kotlin/no/iktdev/ts/ClassScanner.kt b/src/main/kotlin/no/iktdev/ts/ClassScanner.kt index e1e8b97..2fa32ae 100644 --- a/src/main/kotlin/no/iktdev/ts/ClassScanner.kt +++ b/src/main/kotlin/no/iktdev/ts/ClassScanner.kt @@ -38,4 +38,13 @@ class ClassScanner { return classes } -} \ No newline at end of file + + fun scan(packageName: String, includeClasses: Collection, classLoader: ClassLoader): List> = + (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 } +} diff --git a/src/main/kotlin/no/iktdev/ts/TsGenerator.kt b/src/main/kotlin/no/iktdev/ts/TsGenerator.kt index 6af40cb..9daf64c 100644 --- a/src/main/kotlin/no/iktdev/ts/TsGenerator.kt +++ b/src/main/kotlin/no/iktdev/ts/TsGenerator.kt @@ -15,10 +15,19 @@ object TsGenerator { output: File, classLoader: ClassLoader = Thread.currentThread().contextClassLoader, writeSealedStrategy: SealedStrategy, + ) = generate(packageName, emptyList(), output, classLoader, writeSealedStrategy) + + fun generate( + packageName: String, + includeClasses: Collection, + output: File, + classLoader: ClassLoader = Thread.currentThread().contextClassLoader, + writeSealedStrategy: SealedStrategy, ) { 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") classes.forEach { println(" - ${it.qualifiedName}") } @@ -27,7 +36,7 @@ object TsGenerator { appendLine("// AUTO-GENERATED. DO NOT EDIT.") appendLine("// TSGenerator Version: $versionInfo") appendLine("// Time: $buildTime") - appendLine("// Source: $packageName") + appendLine("// Source: $packageName${if (includeClasses.isEmpty()) "" else "; extra classes: ${includeClasses.joinToString()}"}") appendLine() classes.forEach { cls -> @@ -95,4 +104,4 @@ object TsGenerator { println("TsGenerator: wrote file to ${output.absolutePath}") } -} \ No newline at end of file +} diff --git a/src/main/kotlin/no/iktdev/tsgen/TSGeneratorPlugin.kt b/src/main/kotlin/no/iktdev/tsgen/TSGeneratorPlugin.kt index 28a98c2..cf964e3 100644 --- a/src/main/kotlin/no/iktdev/tsgen/TSGeneratorPlugin.kt +++ b/src/main/kotlin/no/iktdev/tsgen/TSGeneratorPlugin.kt @@ -6,11 +6,13 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.provider.Property +import org.gradle.api.provider.ListProperty import java.io.File import java.net.URLClassLoader interface TsGeneratorExtension { val packageName: Property + val includeClasses: ListProperty val outputFile: Property val sealedStrategy: Property @@ -20,6 +22,7 @@ class TsGeneratorPlugin : Plugin { override fun apply(project: Project) { val extension = project.extensions.create("tsGenerator", TsGeneratorExtension::class.java).apply { sealedStrategy.convention(SealedStrategy.AS_INTERFACE_WITH_TYPE) + includeClasses.convention(emptyList()) } project.tasks.register("generateTs") { @@ -30,6 +33,7 @@ class TsGeneratorPlugin : Plugin { TsGenerator.buildTime = java.time.Instant.now().toString() val pkg = extension.packageName.get() + val includedClasses = extension.includeClasses.get() val out = extension.outputFile.get() val writeSealedStrategy = extension.sealedStrategy.get() @@ -41,8 +45,8 @@ class TsGeneratorPlugin : Plugin { val cl = URLClassLoader(urls, TsGenerator::class.java.classLoader) // Kall generatoren med det nye flagget - TsGenerator.generate(pkg, out, cl, writeSealedStrategy) + TsGenerator.generate(pkg, includedClasses, out, cl, writeSealedStrategy) } } } -} \ No newline at end of file +}