mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Allow renaming Java/Kotlin classes/packages during code generation (#499)
Adds a `rename` field to the Java/Kotlin code generators that allows renaming packages and classes during codegen. * Add `--rename` flag to CLIs * Add `rename` property to Gradle API
This commit is contained in:
@@ -26,7 +26,11 @@ dependencies {
|
||||
//
|
||||
// To debug shaded code in IntelliJ, temporarily remove the conditional.
|
||||
if (System.getProperty("idea.sync.active") == null) {
|
||||
runtimeOnly(project(":pkl-tools", "fatJar"))
|
||||
runtimeOnly(projects.pklTools) {
|
||||
attributes {
|
||||
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testImplementation(projects.pklCommonsTest)
|
||||
|
||||
@@ -412,6 +412,7 @@ public class PklPlugin implements Plugin<Project> {
|
||||
task.getOutputDir().set(spec.getOutputDir());
|
||||
task.getGenerateSpringBootConfig().set(spec.getGenerateSpringBootConfig());
|
||||
task.getImplementSerializable().set(spec.getImplementSerializable());
|
||||
task.getPackageMapping().set(spec.getPackageMapping());
|
||||
}
|
||||
|
||||
private <T extends BasePklTask, S extends BasePklSpec> void configureBaseTask(T task, S spec) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.pkl.gradle.spec;
|
||||
|
||||
import org.gradle.api.file.DirectoryProperty;
|
||||
import org.gradle.api.provider.MapProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
|
||||
@@ -30,4 +31,6 @@ public interface CodeGenSpec extends ModulesSpec {
|
||||
Property<Boolean> getGenerateSpringBootConfig();
|
||||
|
||||
Property<Boolean> getImplementSerializable();
|
||||
|
||||
MapProperty<String, String> getPackageMapping();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.pkl.gradle.task;
|
||||
|
||||
import org.gradle.api.file.DirectoryProperty;
|
||||
import org.gradle.api.provider.MapProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.OutputDirectory;
|
||||
@@ -32,4 +33,7 @@ public abstract class CodeGenTask extends ModulesTask {
|
||||
|
||||
@Input
|
||||
public abstract Property<Boolean> getImplementSerializable();
|
||||
|
||||
@Input
|
||||
public abstract MapProperty<String, String> getPackageMapping();
|
||||
}
|
||||
|
||||
@@ -52,7 +52,8 @@ public abstract class JavaCodeGenTask extends CodeGenTask {
|
||||
getGenerateSpringBootConfig().get(),
|
||||
getParamsAnnotation().getOrNull(),
|
||||
getNonNullAnnotation().getOrNull(),
|
||||
getImplementSerializable().get()))
|
||||
getImplementSerializable().get(),
|
||||
getPackageMapping().get()))
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ public abstract class KotlinCodeGenTask extends CodeGenTask {
|
||||
getIndent().get(),
|
||||
getGenerateKdoc().get(),
|
||||
getGenerateSpringBootConfig().get(),
|
||||
getImplementSerializable().get()))
|
||||
getImplementSerializable().get(),
|
||||
getPackageMapping().get()))
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
|
||||
runTask("configClasses")
|
||||
|
||||
val baseDir = testProjectDir.resolve("build/generated/java/org")
|
||||
val baseDir = testProjectDir.resolve("build/generated/java/foo/bar")
|
||||
val moduleFile = baseDir.resolve("Mod.java")
|
||||
|
||||
assertThat(baseDir.listDirectoryEntries().count()).isEqualTo(1)
|
||||
@@ -36,7 +36,7 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
| public static final class Person {
|
||||
| public final @Nonnull String name;
|
||||
|
|
||||
| public final @Nonnull List<@Nonnull Address> addresses;
|
||||
| public final @Nonnull List<Address> addresses;
|
||||
"""
|
||||
)
|
||||
|
||||
@@ -53,28 +53,14 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
@Test
|
||||
fun `compile generated code`() {
|
||||
writeBuildFile()
|
||||
writeFile("mod.pkl", """
|
||||
module org.mod
|
||||
|
||||
class Person {
|
||||
name: String
|
||||
addresses: List<Address?>
|
||||
}
|
||||
|
||||
class Address {
|
||||
street: String
|
||||
zip: Int
|
||||
}
|
||||
|
||||
other: Any = 42
|
||||
""".trimIndent())
|
||||
writePklFile()
|
||||
|
||||
runTask("compileJava")
|
||||
|
||||
val classesDir = testProjectDir.resolve("build/classes/java/main")
|
||||
val moduleClassFile = classesDir.resolve("org/Mod.class")
|
||||
val personClassFile = classesDir.resolve("org/Mod\$Person.class")
|
||||
val addressClassFile = classesDir.resolve("org/Mod\$Address.class")
|
||||
val moduleClassFile = classesDir.resolve("foo/bar/Mod.class")
|
||||
val personClassFile = classesDir.resolve("foo/bar/Mod\$Person.class")
|
||||
val addressClassFile = classesDir.resolve("foo/bar/Mod\$Address.class")
|
||||
assertThat(moduleClassFile).exists()
|
||||
assertThat(personClassFile).exists()
|
||||
assertThat(addressClassFile).exists()
|
||||
@@ -127,6 +113,9 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
paramsAnnotation = "javax.inject.Named"
|
||||
nonNullAnnotation = "javax.annotation.Nonnull"
|
||||
settingsModule = "pkl:settings"
|
||||
packageMapping = [
|
||||
'org': 'foo.bar'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,18 +123,6 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
)
|
||||
}
|
||||
|
||||
private fun writeGradlePropertiesFile() {
|
||||
writeFile("gradle.properties", """
|
||||
systemProp.http.proxyHost=proxy.config.pcp.local
|
||||
systemProp.http.proxyPort=3128
|
||||
systemProp.http.nonProxyHosts=localhost|*.apple.com
|
||||
|
||||
systemProp.https.proxyHost=proxy.config.pcp.local
|
||||
systemProp.https.proxyPort=3128
|
||||
systemProp.https.nonProxyHosts=localhost|*.apple.com
|
||||
""")
|
||||
}
|
||||
|
||||
private fun writePklFile() {
|
||||
writeFile(
|
||||
"mod.pkl", """
|
||||
@@ -153,7 +130,7 @@ class JavaCodeGeneratorsTest : AbstractTest() {
|
||||
|
||||
class Person {
|
||||
name: String
|
||||
addresses: List<Address>
|
||||
addresses: List<Address?>
|
||||
}
|
||||
|
||||
class Address {
|
||||
|
||||
@@ -13,7 +13,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
|
||||
runTask("configClasses")
|
||||
|
||||
val baseDir = testProjectDir.resolve("build/generated/kotlin/org")
|
||||
val baseDir = testProjectDir.resolve("build/generated/kotlin/foo/bar")
|
||||
val kotlinFile = baseDir.resolve("Mod.kt")
|
||||
|
||||
assertThat(baseDir.listDirectoryEntries().count()).isEqualTo(1)
|
||||
@@ -58,9 +58,9 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
runTask("compileKotlin")
|
||||
|
||||
val classesDir = testProjectDir.resolve("build/classes/kotlin/main")
|
||||
val moduleClassFile = classesDir.resolve("org/Mod.class")
|
||||
val personClassFile = classesDir.resolve("org/Mod\$Person.class")
|
||||
val addressClassFile = classesDir.resolve("org/Mod\$Address.class")
|
||||
val moduleClassFile = classesDir.resolve("foo/bar/Mod.class")
|
||||
val personClassFile = classesDir.resolve("foo/bar/Mod\$Person.class")
|
||||
val addressClassFile = classesDir.resolve("foo/bar/Mod\$Address.class")
|
||||
assertThat(moduleClassFile).exists()
|
||||
assertThat(personClassFile).exists()
|
||||
assertThat(addressClassFile).exists()
|
||||
@@ -125,6 +125,9 @@ class KotlinCodeGeneratorsTest : AbstractTest() {
|
||||
sourceModules = ["mod.pkl"]
|
||||
outputDir = file("build/generated")
|
||||
settingsModule = "pkl:settings"
|
||||
packageMapping = [
|
||||
'org.': 'foo.bar.'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user