args() {
* @return this operation instance
*/
public CompileOptions classpath(String... paths) {
- classpath_.addAll(Arrays.stream(paths).map(File::new).toList());
- return this;
+ return classpathStrings(List.of(paths));
}
/**
@@ -357,8 +407,19 @@ public CompileOptions classpath(String... paths) {
* @return this operation instance
*/
public CompileOptions classpath(File... paths) {
- classpath_.addAll(List.of(paths));
- return this;
+ return classpath(List.of(paths));
+ }
+
+ /**
+ * Search for class files in the specified paths.
+ *
+ * The classpath can contain file and directory paths, ZIP, or JAR files.
+ *
+ * @param paths one or more path
+ * @return this operation instance
+ */
+ public CompileOptions classpath(Path... paths) {
+ return classpathPaths(List.of(paths));
}
/**
@@ -383,6 +444,39 @@ public Collection classpath() {
return classpath_;
}
+ /**
+ * Search for class files in the specified paths.
+ *
+ * The classpath can contain file and directory paths, ZIP, or JAR files.
+ *
+ * @param paths one pr more paths
+ * @return this operation instance
+ */
+ public CompileOptions classpathPaths(Collection paths) {
+ return classpath(paths.stream().map(Path::toFile).toList());
+ }
+
+ /**
+ * Search for class files in the specified paths.
+ *
+ * The classpath can contain file and directory paths, ZIP, or JAR files.
+ *
+ * @param paths one pr more paths
+ * @return this operation instance
+ */
+ public CompileOptions classpathStrings(Collection paths) {
+ return classpath(paths.stream().map(File::new).toList());
+ }
+
+ /**
+ * Retrieves the string to evaluate as a Kotlin script.
+ *
+ * @return the expression
+ */
+ public String expression() {
+ return expression_;
+ }
+
/**
* Evaluate the given string as a Kotlin script.
*
@@ -415,6 +509,69 @@ public CompileOptions includeRuntime(boolean includeRuntime) {
return this;
}
+ /**
+ * Indicates whether the {@link #includeRuntime(boolean)} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isIncludeRuntime() {
+ return includeRuntime_;
+ }
+
+ /**
+ * Indicates whether {@link #javaParameters(boolean)} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isJavaParameters() {
+ return javaParameters_;
+ }
+
+ /**
+ * Indicates whether {@link #noJdk(boolean) noJdk} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isNoJdk() {
+ return noJdk_;
+ }
+
+ /**
+ * Indicates whether {@link #noReflect(boolean) noRflect} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isNoReflect() {
+ return noReflect_;
+ }
+
+ /**
+ * Indicates whether {@link #noStdLib(boolean) noStdLib} +was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isNoStdLib() {
+ return noStdLib_;
+ }
+
+ /**
+ * Indicates whether {@link #noWarn(boolean) noWarn} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isNoWarn() {
+ return noWarn_;
+ }
+
+ /**
+ * Indicates whether {@link #progressive(boolean) progressive} was set.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isProgressive() {
+ return progressive_;
+ }
+
/**
* Indicates whether {@link #verbose(boolean)} was set.
*
@@ -424,6 +581,15 @@ public boolean isVerbose() {
return verbose_;
}
+ /**
+ * Indicates whether warnings are turned into a compilation error.
+ *
+ * @return {@code true} or {@code false}
+ */
+ public boolean isWError() {
+ return wError_;
+ }
+
/**
* Generate metadata for Java 1.8 reflection on method parameters.
*
@@ -441,8 +607,8 @@ public CompileOptions javaParameters(boolean javaParameters) {
* @param jdkHome the JDK home path
* @return this operation instance
*/
- public CompileOptions jdkHome(String jdkHome) {
- jdkHome_ = new File(jdkHome);
+ public CompileOptions jdkHome(File jdkHome) {
+ jdkHome_ = jdkHome;
return this;
}
@@ -452,18 +618,45 @@ public CompileOptions jdkHome(String jdkHome) {
* @param jdkHome the JDK home path
* @return this operation instance
*/
- public CompileOptions jdkHome(File jdkHome) {
- jdkHome_ = jdkHome;
- return this;
+ public CompileOptions jdkHome(String jdkHome) {
+ return jdkHome(new File(jdkHome));
}
/**
- * Specify the target version of the generated JVM bytecode.
+ * Use a custom JDK home directory to include into the classpath if it differs from the default {@code JAVA_HOME}.
+ *
+ * @param jdkHome the JDK home path
+ * @return this operation instance
+ */
+ public CompileOptions jdkHome(Path jdkHome) {
+ return jdkHome(jdkHome.toFile());
+ }
+
+ /**
+ * Retrieves the custom JDK home directory.
+ *
+ * @return the JDK home path.
+ */
+ public File jdkHome() {
+ return jdkHome_;
+ }
+
+ /**
+ * Return the specified JDK API version.
+ *
+ * @return the API version
+ */
+ public String jdkRelease() {
+ return jdkRelease_;
+ }
+
+ /**
+ * Compile against the specified JDK API version.
*
* Limit the API of the JDK in the classpath to the specified Java version. Automatically sets
* {@link #jvmTarget(String) JVM target} version.
*
- * Possible values are 1.8, 9, 10, ..., 21. The default value is 1.8.
+ * Possible values are 1.8, 9, 10, ..., 22. The default value is 1.8.
*
* @param version the target version
* @return this operation instance
@@ -474,7 +667,12 @@ public CompileOptions jdkRelease(String version) {
}
/**
- * Specify the target version of the generated JVM bytecode.
+ * Compile against the specified JDK API version.
+ *
+ * Limit the API of the JDK in the classpath to the specified Java version. Automatically sets
+ * {@link #jvmTarget(String) JVM target} version.
+ *
+ * Possible values are 1.8, 9, 10, ..., 22. The default value is 1.8.
*
* @param version the target version
* @return this operation instance
@@ -485,17 +683,6 @@ public CompileOptions jdkRelease(int version) {
return this;
}
- /**
- * Pass an option directly to JVM
- *
- * @param jvmOptions one or more JVM option
- * @return this operation instance
- */
- public CompileOptions jvmOptions(String... jvmOptions) {
- jvmOptions_.addAll(List.of(jvmOptions));
- return this;
- }
-
/**
* Retrieves the Java Virtual Machine options.
*
@@ -517,16 +704,13 @@ public CompileOptions jvmOptions(Collection jvmOptions) {
}
/**
- * Specify the target version of the generated JVM bytecode.
- *
- * Possible values are 1.8, 9, 10, ..., 21. The default value is 1.8.
+ * Pass an option directly to JVM
*
- * @param target the target version
+ * @param jvmOptions one or more JVM option
* @return this operation instance
*/
- public CompileOptions jvmTarget(String target) {
- jvmTarget_ = target;
- return this;
+ public CompileOptions jvmOptions(String... jvmOptions) {
+ return jvmOptions(List.of(jvmOptions));
}
/**
@@ -541,6 +725,28 @@ public CompileOptions jvmTarget(int target) {
return this;
}
+ /**
+ * Specify the target version of the generated JVM bytecode.
+ *
+ * Possible values are 1.8, 9, 10, ..., 22. The default value is 1.8.
+ *
+ * @param target the target version
+ * @return this operation instance
+ */
+ public CompileOptions jvmTarget(String target) {
+ jvmTarget_ = target;
+ return this;
+ }
+
+ /**
+ * Retrieves the target version of the generated JVM bytecode.
+ *
+ * @return the target version
+ */
+ public String jvmTarget() {
+ return jvmTarget_;
+ }
+
/**
* Specify a custom path to the Kotlin compiler used for the discovery of runtime libraries.
*
@@ -552,6 +758,25 @@ public CompileOptions kotlinHome(File path) {
return this;
}
+ /**
+ * Retrieves the custom path of the Kotlin compiler.
+ *
+ * @return the Kotlin home path
+ */
+ public File kotlinHome() {
+ return kotlinHome_;
+ }
+
+ /**
+ * Specify a custom path to the Kotlin compiler used for the discovery of runtime libraries.
+ *
+ * @param path the Kotlin home path
+ * @return this operation instance
+ */
+ public CompileOptions kotlinHome(Path path) {
+ return kotlinHome(path.toFile());
+ }
+
/**
* Specify a custom path to the Kotlin compiler used for the discovery of runtime libraries.
*
@@ -559,8 +784,7 @@ public CompileOptions kotlinHome(File path) {
* @return this operation instance
*/
public CompileOptions kotlinHome(String path) {
- kotlinHome_ = new File(path);
- return this;
+ return kotlinHome(new File(path));
}
/**
@@ -574,6 +798,15 @@ public CompileOptions languageVersion(String version) {
return this;
}
+ /**
+ * Retrieves the {@link #languageVersion(String) language version}.
+ *
+ * @return the language version
+ */
+ public String languageVersion() {
+ return languageVersion_;
+ }
+
/**
* Set a custom name for the generated {@code .kotlin_module} file.
*
@@ -585,6 +818,15 @@ public CompileOptions moduleName(String name) {
return this;
}
+ /**
+ * Retrieves the {@link #moduleName(String) module name}.
+ *
+ * @return the module name
+ */
+ public String moduleName() {
+ return moduleName_;
+ }
+
/**
* Don't automatically include the Java runtime into the classpath.
*
@@ -705,6 +947,27 @@ public CompileOptions path(File path) {
return this;
}
+ /**
+ * Retrieves the location to place generated class files into.
+ *
+ * @return the location path.
+ */
+ public File path() {
+ return path_;
+ }
+
+ /**
+ * Place the generated class files into the specified location.
+ *
+ * The location can be a directory, a ZIP, or a JAR file.
+ *
+ * @param path the location path
+ * @return this operation instance
+ */
+ public CompileOptions path(Path path) {
+ return path(path.toFile());
+ }
+
/**
* Place the generated class files into the specified location.
*
@@ -714,8 +977,7 @@ public CompileOptions path(File path) {
* @return this operation instance
*/
public CompileOptions path(String path) {
- path_ = new File(path);
- return this;
+ return path(new File(path));
}
/**
@@ -734,7 +996,7 @@ public CompileOptions plugin(String id, String optionName, String value) {
/**
* Retrieves the plugin options.
*
- * @return the plugin ofoptions.
+ * @return the plugin options.
*/
public Collection plugin() {
return plugin_;
diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java
index 231ff1e..e2e6bd5 100644
--- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java
+++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java
@@ -108,8 +108,8 @@ void testExecute() throws Exception {
var mainDir = new File(buildDir, "main");
var testDir = new File(buildDir, "test");
- assertThat(mainDir.mkdirs()).isTrue();
- assertThat(testDir.mkdirs()).isTrue();
+ assertThat(mainDir.mkdirs()).as("make mainDir").isTrue();
+ assertThat(testDir.mkdirs()).as("make testDir").isTrue();
var compileJars = new ArrayList();
for (var f : Objects.requireNonNull(new File("examples/lib/compile").listFiles())) {
@@ -136,20 +136,20 @@ void testExecute() throws Exception {
var args = op.compileOptions().args();
var matches = List.of("-Xjdk-release=17", "-no-stdlib", "-verbose");
- assertThat(args).isEqualTo(matches);
+ assertThat(args).as(args + " == " + matches).isEqualTo(matches);
op.execute();
- assertThat(tmpDir).isNotEmptyDirectory();
- assertThat(mainDir).isNotEmptyDirectory();
- assertThat(testDir).isNotEmptyDirectory();
+ assertThat(tmpDir).as("tmpDir").isNotEmptyDirectory();
+ assertThat(mainDir).as("mainDir").isNotEmptyDirectory();
+ assertThat(testDir).as("testDir").isNotEmptyDirectory();
var mainOut = Path.of(mainDir.getAbsolutePath(), "com", "example").toFile();
- assertThat(new File(mainOut, "Example.class")).exists();
- assertThat(new File(mainOut, "Example$Companion.class")).exists();
+ assertThat(new File(mainOut, "Example.class")).as("Example.class").exists();
+ assertThat(new File(mainOut, "Example$Companion.class")).as("ExampleCompanion.class").exists();
var testOut = Path.of(testDir.getAbsolutePath(), "com", "example").toFile();
- assertThat(new File(testOut, "ExampleTest.class")).exists();
+ assertThat(new File(testOut, "ExampleTest.class")).as("ExampleTest.class").exists();
} finally {
FileUtils.deleteDirectory(tmpDir);
}
diff --git a/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java b/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java
index 25df90b..b628a3c 100644
--- a/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java
+++ b/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java
@@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
@@ -43,6 +44,7 @@ private String localPath(String... fileNames) {
.collect(Collectors.joining(File.pathSeparator));
}
+
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void testArgs() {
@@ -101,7 +103,8 @@ void testArgs() {
args.add(options.apiVersion(11).jvmTarget(11).args());
for (var a : args) {
- IntStream.range(0, a.size()).forEach(i -> assertThat(a.get(i)).isEqualTo(matches.get(i)));
+ IntStream.range(0, a.size()).forEach(i -> assertThat(a.get(i))
+ .as(a.get(i) + " == " + matches.get(i)).isEqualTo(matches.get(i)));
}
}
@@ -170,10 +173,29 @@ void testArgsCollections() {
break;
}
}
- assertThat(found).as(arg).isTrue();
+ assertThat(found).as(arg + " not found.").isTrue();
}
}
+ @Test
+ void testArgsFile() {
+ var foo = new File("foo.txt");
+ var bar = new File("bar.txt");
+ var options = new CompileOptions();
+
+ options.argFile(foo, bar);
+ assertThat(options.argFile()).contains(foo, bar);
+ options.argFile().clear();
+
+ options = options.argFile(foo.toPath(), bar.toPath());
+ assertThat(options.argFile()).contains(foo, bar);
+ options.argFile().clear();
+
+ options.argFile(foo.getAbsolutePath(), bar.getAbsolutePath());
+ assertThat(options.argFile()).contains(new File(foo.getAbsolutePath()), new File(bar.getAbsolutePath()));
+ options.argFile().clear();
+ }
+
@Test
void testCheckAllParams() throws IOException {
var args = Files.readAllLines(Paths.get("src", "test", "resources", "kotlinc-args.txt"));
@@ -182,6 +204,7 @@ void testCheckAllParams() throws IOException {
var params = new CompileOptions()
.advancedOptions("Xoption")
+ .apiVersion("11")
.argFile("file")
.classpath("classpath")
.expression("expression")
@@ -191,6 +214,7 @@ void testCheckAllParams() throws IOException {
.jdkHome("jdkhome")
.jvmTarget(12)
.kotlinHome("kotlin")
+ .languageVersion("1.0")
.moduleName("moduleName")
.noJdk(true)
.noReflect(true)
@@ -216,4 +240,109 @@ void testCheckAllParams() throws IOException {
assertThat(found).as(p + " not found.").isTrue();
}
}
+
+ @Test
+ void testClasspath() {
+ var foo = new File("foo.txt");
+ var bar = new File("bar.txt");
+ var options = new CompileOptions();
+
+ options.classpath(foo, bar);
+ assertThat(options.classpath()).contains(foo, bar);
+ options.classpath().clear();
+
+ options = options.classpath(foo.toPath(), bar.toPath());
+ assertThat(options.classpath()).contains(foo, bar);
+ options.classpath().clear();
+
+ options.classpath(foo.getAbsolutePath(), bar.getAbsolutePath());
+ assertThat(options.classpath()).contains(new File(foo.getAbsolutePath()), new File(bar.getAbsolutePath()));
+ options.classpath().clear();
+ }
+
+ @Test
+ void testJdkHome() {
+ var foo = new File("foo.txt");
+ var options = new CompileOptions();
+
+ options.jdkHome(foo);
+ assertThat(options.jdkHome()).isEqualTo(foo);
+
+ options = options.jdkHome(foo.toPath());
+ assertThat(options.jdkHome()).isEqualTo(foo);
+
+ options.jdkHome(foo.getAbsolutePath());
+ assertThat(options.jdkHome().getAbsolutePath()).isEqualTo(foo.getAbsolutePath());
+ }
+
+ @Test
+ void testKotlinHome() {
+ var foo = new File("foo.txt");
+ var options = new CompileOptions();
+
+ options.kotlinHome(foo);
+ assertThat(options.kotlinHome()).isEqualTo(foo);
+
+ options = options.kotlinHome(foo.toPath());
+ assertThat(options.kotlinHome()).isEqualTo(foo);
+
+ options.kotlinHome(foo.getAbsolutePath());
+ assertThat(options.kotlinHome().getAbsolutePath()).isEqualTo(foo.getAbsolutePath());
+ }
+
+ @Test
+ void testOptions() {
+ var options = new CompileOptions()
+ .advancedOptions("xopt1", "xopt2")
+ .apiVersion("11")
+ .argFile(Path.of("args.txt"))
+ .classpath("classpath")
+ .expression("expression")
+ .includeRuntime(true)
+ .javaParameters(true)
+ .jdkHome("jdk-home")
+ .jdkRelease("22")
+ .jvmTarget("9")
+ .kotlinHome("kotlin-home")
+ .languageVersion("1.0")
+ .moduleName("module")
+ .noJdk(true)
+ .noReflect(true)
+ .noStdLib(true)
+ .noWarn(true)
+ .optIn("opt1", "opt2")
+ .options("-foo", "-bar")
+ .path(Path.of("path"))
+ .plugin("id", "name", "value")
+ .progressive(true)
+ .scriptTemplates("name", "name2")
+ .verbose(true)
+ .wError(true);
+
+ assertThat(options.advancedOptions()).containsExactly("xopt1", "xopt2");
+ assertThat(options.apiVersion()).isEqualTo("11");
+ assertThat(options.argFile()).containsExactly(new File("args.txt"));
+ assertThat(options.classpath()).containsExactly(new File("classpath"));
+ assertThat(options.expression()).isEqualTo("expression");
+ assertThat(options.isIncludeRuntime()).isTrue();
+ assertThat(options.isJavaParameters()).isTrue();
+ assertThat(options.isNoJdk()).isTrue();
+ assertThat(options.isNoReflect()).isTrue();
+ assertThat(options.isNoStdLib()).isTrue();
+ assertThat(options.isNoWarn()).isTrue();
+ assertThat(options.isProgressive()).isTrue();
+ assertThat(options.isVerbose()).isTrue();
+ assertThat(options.jdkHome()).isEqualTo(new File("jdk-home"));
+ assertThat(options.jdkRelease()).isEqualTo("22");
+ assertThat(options.jvmTarget()).isEqualTo("9");
+ assertThat(options.kotlinHome()).isEqualTo(new File("kotlin-home"));
+ assertThat(options.languageVersion()).isEqualTo("1.0");
+ assertThat(options.moduleName()).isEqualTo("module");
+ assertThat(options.optIn()).containsExactly("opt1", "opt2");
+ assertThat(options.options()).containsExactly("-foo", "-bar");
+ assertThat(options.path()).isEqualTo(new File("path"));
+ assertThat(options.plugin()).containsExactly("id:name:value");
+ assertThat(options.scriptTemplates()).containsExactly("name", "name2");
+ assertThat(options.isWError()).isTrue();
+ }
}
diff --git a/src/test/resources/kotlinc-args.txt b/src/test/resources/kotlinc-args.txt
index 1f49a77..b3c7f27 100644
--- a/src/test/resources/kotlinc-args.txt
+++ b/src/test/resources/kotlinc-args.txt
@@ -1,4 +1,5 @@
@
+-api-version
-classpath
-d
-expression
@@ -8,6 +9,7 @@
-jdk-home
-jvm-target
-kotlin-home
+-language-version
-module-name
-no-jdk
-no-reflect