Skip to content

Commit

Permalink
Kotlin compile args (#294)
Browse files Browse the repository at this point in the history
* #293 allow to pass kotlin compiler args via Gradle

* #293 allow to pass kotlin compiler args via Maven

* #293 run kte-runtime tests as well
  • Loading branch information
casid authored Oct 30, 2023
1 parent dd8b9f5 commit fd9466d
Show file tree
Hide file tree
Showing 29 changed files with 602 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Temporary Items
/test/jte-runtime-test-gradle-convention/jte-classes/
/test/jte-runtime-test-gradle-kotlin-convention/jte-classes/
/test/jte-hotreload-test/jte-classes/
/test/kte-runtime-test-gradle-kotlin-convention/jte-classes/
.run

### VS Code / VS Code Java extension (uses Eclipse code) ###
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public JteExtension(ObjectFactory objectFactory) {
public abstract ConfigurableFileCollection getCompilePath();
public abstract Property<String> getHtmlPolicyClass();
public abstract Property<String[]> getCompileArgs();
public abstract Property<String[]> getKotlinCompileArgs();
public abstract Property<String> getProjectNamespace();
public abstract ListProperty<JteExtensionSettings> getJteExtensions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public void setCompileArgs(String[] compileArgs) {
setterCalled();
}

@Input
@Optional
public String[] getKotlinCompileArgs() {
return extension.getKotlinCompileArgs().getOrNull();
}

public void setKotlinCompileArgs(String[] compileArgs) {
extension.getKotlinCompileArgs().set(compileArgs);
setterCalled();
}

@TaskAction
public void execute() {
// Prevent Kotlin compiler to leak file handles, see https://github.com/casid/jte/issues/77
Expand All @@ -67,7 +78,7 @@ public void execute() {
Logger logger = getLogger();
long start = System.nanoTime();

Path sourceDirectory = getSourceDirectory();
Path sourceDirectory = getSourceDirectory();
logger.info("Precompiling jte templates found in " + sourceDirectory);

Path targetDirectory = getTargetDirectory();
Expand All @@ -85,6 +96,7 @@ public void execute() {
templateEngine.setHtmlCommentsPreserved(Boolean.TRUE.equals(getHtmlCommentsPreserved()));
templateEngine.setBinaryStaticContent(Boolean.TRUE.equals(getBinaryStaticContent()));
templateEngine.setCompileArgs(getCompileArgs());
templateEngine.setKotlinCompileArgs(getKotlinCompileArgs());
templateEngine.setTargetResourceDirectory(getTargetResourceDirectory());

int amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public void compile(String[] files, List<String> classPath, TemplateConfig confi

compilerArguments.setClasspath(ClassUtils.join(classPath));

if (config.kotlinCompileArgs != null) {
applyCompileArgs(compilerArguments, config.kotlinCompileArgs);
}

K2JVMCompiler compiler = new K2JVMCompiler();

SimpleKotlinCompilerMessageCollector messageCollector = new SimpleKotlinCompilerMessageCollector(templateByClassName, config.packageName);
Expand All @@ -46,6 +50,15 @@ public void compile(String[] files, List<String> classPath, TemplateConfig confi
}
}

private void applyCompileArgs(K2JVMCompilerArguments compilerArguments, String[] kotlinCompileArgs) {
for (int i = 0; i < kotlinCompileArgs.length; i++) {
if ("-jvm-target".equals(kotlinCompileArgs[i])) {
i++;
compilerArguments.setJvmTarget(kotlinCompileArgs[i]);
}
}
}

private static class SimpleKotlinCompilerMessageCollector implements MessageCollector {

private final Map<String, ClassInfo> templateByClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,13 @@ void rawWithJavaScript() {
"</script>\n");
}

@Test
void kotlinCompileArgs() {
templateEngine.setKotlinCompileArgs("-jvm-target", "17");
givenRawTemplate("@param model:gg.jte.kotlin.TemplateEngineTest.Model\nHello ${model.x}");
thenOutputIs("Hello 42");
}

private void givenTag(String name, String code) {
dummyCodeResolver.givenCode("tag/" + name + ".kte", code);
}
Expand Down
7 changes: 7 additions & 0 deletions jte-maven-plugin/src/main/java/gg/jte/maven/CompilerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public class CompilerMojo extends AbstractMojo {
@Parameter
public String[] compileArgs;

/**
* Sets additional compiler arguments for kte templates.
*/
@Parameter
public String[] kotlinCompileArgs;

/**
* The package name, where template classes are generated to.
*/
Expand Down Expand Up @@ -131,6 +137,7 @@ public void execute() {
templateEngine.setHtmlCommentsPreserved(htmlCommentsPreserved);
templateEngine.setBinaryStaticContent(binaryStaticContent);
templateEngine.setCompileArgs(calculateCompileArgs());
templateEngine.setCompileArgs(kotlinCompileArgs);

int amount;
try {
Expand Down
1 change: 1 addition & 0 deletions jte-runtime/src/main/java/gg/jte/TemplateConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class TemplateConfig {
public final ContentType contentType;
public final String packageName;
public String[] compileArgs;
public String[] kotlinCompileArgs;
public boolean trimControlStructures;
public HtmlPolicy htmlPolicy = new OwaspHtmlPolicy();
public String[] htmlTags;
Expand Down
11 changes: 11 additions & 0 deletions jte-runtime/src/main/java/gg/jte/TemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,17 @@ public void setCompileArgs(String ... compileArgs) {
config.compileArgs = compileArgs;
}

/**
* Sets additional compiler arguments for kte templates.
* <br>
* This is a template compilation setting and has no effect when loading precompiled templates.
* Currently, only -jvm-target is supported.
* @param compileArgs for instance templateEngine.setCompileArgs("-jvm-target", "17");
*/
public void setKotlinCompileArgs(String ... compileArgs) {
config.kotlinCompileArgs = compileArgs;
}

/**
* Trims control structures, resulting in prettier output.
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static List<String> getTestGradleVersions() {
public static Stream<Arguments> runGradleBuild() throws IOException {
return Files.find(Paths.get(".."), 2, (p, attr) -> p.getFileName().toString().startsWith("settings.gradle"))
.map(Path::getParent)
.filter(p -> p.getFileName().toString().startsWith("jte-runtime"))
.filter(p -> p.getFileName().toString().startsWith("jte-runtime") || p.getFileName().toString().startsWith("kte-runtime"))
.flatMap(p -> GRADLE_VERSIONS.stream().map(v -> Arguments.arguments(p, v)));
}

Expand Down
32 changes: 32 additions & 0 deletions test/kte-runtime-test-gradle-kotlin-convention/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

plugins {
kotlin("jvm") version "1.9.10"
id("gg.jte.gradle") version("3.1.4-SNAPSHOT")
}

repositories {
mavenCentral()
mavenLocal()
}

tasks.test {
useJUnitPlatform()
}

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.junit.jupiter:junit-jupiter:5.9.2")
implementation("gg.jte:jte-runtime:3.1.4-SNAPSHOT")
}

jte {
precompile()
kotlinCompileArgs.set(arrayOf("-jvm-target", "17"))
}

tasks.jar {
dependsOn(tasks.precompileJte)
from(fileTree("jte-classes") {
include("**/*.class")
})
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit fd9466d

Please sign in to comment.