mirror of
https://github.com/apple/pkl.git
synced 2026-04-22 00:08:33 +02:00
Add setting for Kotlin package to codegen (#194)
This commit is contained in:
@@ -196,12 +196,15 @@ public class PklPlugin implements Plugin<Project> {
|
||||
configureCodeGenSpec(spec);
|
||||
|
||||
spec.getGenerateKdoc().convention(false);
|
||||
spec.getKotlinPackage().convention("");
|
||||
|
||||
createModulesTask(KotlinCodeGenTask.class, spec)
|
||||
.configure(
|
||||
task -> {
|
||||
configureCodeGenTask(task, spec);
|
||||
task.getGenerateKdoc().set(spec.getGenerateKdoc());
|
||||
task.getIndent().set(spec.getIndent());
|
||||
task.getKotlinPackage().set(spec.getKotlinPackage());
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -19,5 +19,9 @@ import org.gradle.api.provider.Property;
|
||||
|
||||
/** Configuration options for Kotlin code generators. Documented in user manual. */
|
||||
public interface KotlinCodeGenSpec extends CodeGenSpec {
|
||||
Property<String> getIndent();
|
||||
|
||||
Property<String> getKotlinPackage();
|
||||
|
||||
Property<Boolean> getGenerateKdoc();
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ public abstract class KotlinCodeGenTask extends CodeGenTask {
|
||||
@Input
|
||||
public abstract Property<Boolean> getGenerateKdoc();
|
||||
|
||||
@Input
|
||||
public abstract Property<String> getKotlinPackage();
|
||||
|
||||
@Override
|
||||
protected void doRunTask() {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
@@ -35,6 +38,7 @@ public abstract class KotlinCodeGenTask extends CodeGenTask {
|
||||
getCliBaseOptions(),
|
||||
getProject().file(getOutputDir()).toPath(),
|
||||
getIndent().get(),
|
||||
getKotlinPackage().get(),
|
||||
getGenerateKdoc().get(),
|
||||
getGenerateSpringBootConfig().get(),
|
||||
getImplementSerializable().get()))
|
||||
|
||||
@@ -65,7 +65,22 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
assertThat(personClassFile).exists()
|
||||
assertThat(addressClassFile).exists()
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `compile generated code with custom kotlin package`() {
|
||||
writeBuildFile(kotlinPackage = "my.cool.pkl.pkg")
|
||||
writePklFile()
|
||||
runTask("compileKotlin")
|
||||
|
||||
val classesDir = testProjectDir.resolve("build/classes/kotlin/main")
|
||||
val moduleClassFile = classesDir.resolve("my/cool/pkl/pkg/org/Mod.class")
|
||||
val personClassFile = classesDir.resolve("my/cool/pkl/pkg/org/Mod\$Person.class")
|
||||
val addressClassFile = classesDir.resolve("my/cool/pkl/pkg/org/Mod\$Address.class")
|
||||
assertThat(moduleClassFile).exists()
|
||||
assertThat(personClassFile).exists()
|
||||
assertThat(addressClassFile).exists()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no source modules`() {
|
||||
writeFile(
|
||||
@@ -88,7 +103,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
assertThat(result.output).contains("No source modules specified.")
|
||||
}
|
||||
|
||||
private fun writeBuildFile() {
|
||||
private fun writeBuildFile(kotlinPackage: String? = null) {
|
||||
val kotlinVersion = "1.6.0"
|
||||
|
||||
writeFile(
|
||||
@@ -125,6 +140,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
sourceModules = ["mod.pkl"]
|
||||
outputDir = file("build/generated")
|
||||
settingsModule = "pkl:settings"
|
||||
${kotlinPackage?.let { "kotlinPackage = \"$it\"" } ?: ""}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user