Add setting for Kotlin package to codegen (#194)

This commit is contained in:
Sam Gammon
2024-02-23 02:54:05 -08:00
committed by GitHub
parent 48a000aa1a
commit 7f404fff49
7 changed files with 145 additions and 8 deletions

View File

@@ -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());
});
});

View File

@@ -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();
}

View File

@@ -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()))

View File

@@ -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\"" } ?: ""}
}
}
}