diff --git a/examples/src/bld/java/com/example/ExampleBuild.java b/examples/src/bld/java/com/example/ExampleBuild.java index 91d5762..aa12e0d 100644 --- a/examples/src/bld/java/com/example/ExampleBuild.java +++ b/examples/src/bld/java/com/example/ExampleBuild.java @@ -61,17 +61,11 @@ public class ExampleBuild extends Project { @Override public void compile() throws Exception { // The source code located in src/main/kotlin and src/test/kotlin will be compiled - var options = new CompileOptions().verbose(true); - var op = new CompileKotlinOperation() + new CompileKotlinOperation() // .kotlinHome("path/to/kotlin") // .kotlinc("path/to/kotlinc") - .compileOptions(options) - .fromProject(this); - - if (!CompileKotlinOperation.isWindows()) { - op.jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED); - } - - op.execute(); + .compileOptions(new CompileOptions().verbose(true)) + .fromProject(this) + .execute(); } } diff --git a/src/main/java/rife/bld/extension/CompileKotlinOperation.java b/src/main/java/rife/bld/extension/CompileKotlinOperation.java index 26437a8..bb6f209 100644 --- a/src/main/java/rife/bld/extension/CompileKotlinOperation.java +++ b/src/main/java/rife/bld/extension/CompileKotlinOperation.java @@ -42,7 +42,7 @@ public class CompileKotlinOperation extends AbstractOperation compileMainClasspath_ = new ArrayList<>(); private final Collection compileTestClasspath_ = new ArrayList<>(); private final JvmOptions jvmOptions_ = new JvmOptions(); @@ -191,17 +191,6 @@ public class CompileKotlinOperation extends AbstractOperation command.add("-J" + s)); } - // compiler options - if (compileOptions_ != null) { - args.addAll(compileOptions_.args()); + // classpath + if (compileOptions_ != null && !compileOptions_.classpath().isEmpty()) { cp.addAll(compileOptions_.classpath().stream().map(this::cleanPath).toList()); } - - // classpath if (!cp.isEmpty()) { args.add("-cp"); args.add('"' + FileUtils.joinPaths(cp.stream().map(this::cleanPath).toList()) + '"'); } + // compile options + if (compileOptions_ != null && !compileOptions_.args().isEmpty()) { + args.addAll(compileOptions_.args()); + } + // destination args.add("-d"); args.add('"' + cleanPath(destination) + '"'); @@ -616,10 +596,10 @@ public class CompileKotlinOperation extends AbstractOperation advancedOptions_ = new ArrayList<>(); private final Collection argFile_ = new ArrayList<>(); private final Collection classpath_ = new ArrayList<>(); @@ -248,7 +255,27 @@ public class CompileOptions { // @argfile if (!argFile_.isEmpty()) { - argFile_.forEach(f -> args.add("@" + f.getAbsolutePath())); + argFile_.forEach(f -> { + if (f.exists()) { + try { + try (var reader = Files.newBufferedReader(f.toPath(), Charset.defaultCharset())) { + var tokenizer = new AbstractToolProviderOperation.CommandLineTokenizer(reader); + String token; + while ((token = tokenizer.nextToken()) != null) { + args.add(token); + } + } + } catch (IOException e) { + if (LOGGER.isLoggable(Level.WARNING)) { + LOGGER.log(Level.WARNING, "Could not read: " + f.getAbsolutePath(), e); + } + } + } else { + if (LOGGER.isLoggable(Level.WARNING)) { + LOGGER.warning("File not found: " + f.getAbsolutePath()); + } + } + }); } // expression diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java index eeaf75e..4e0182e 100644 --- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java +++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java @@ -180,7 +180,7 @@ class CompileKotlinOperationTest { .compileTestClasspath(mainDir.getAbsolutePath()); op.compileOptions().verbose(true); - op.compileOptions().jdkRelease("17"); + op.compileOptions().argFile("src/test/resources/argfile.txt", "src/test/resources/argfile2.txt"); if (!CompileKotlinOperation.isWindows()) { op.jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED); @@ -188,7 +188,8 @@ class CompileKotlinOperationTest { } var args = op.compileOptions().args(); - var matches = List.of("-Xjdk-release=17", "-no-stdlib", "-verbose"); + var matches = List.of("-Xjdk-release=17", "-no-reflect", "-progressive", "-include-runtime", "-no-stdlib", + "-verbose"); assertThat(args).as(args + " == " + matches).isEqualTo(matches); op.execute(); diff --git a/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java b/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java index 83d29fc..5b36483 100644 --- a/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java +++ b/src/test/java/rife/bld/extension/kotlin/CompileOptionsTest.java @@ -52,7 +52,6 @@ class CompileOptionsTest { void testArgs() { var options = new CompileOptions() .apiVersion("11") - .argFile(new File("file.txt"), new File("file2.txt")) .javaParameters(true) .jvmTarget("11") .includeRuntime(true) @@ -76,7 +75,6 @@ class CompileOptionsTest { var matches = List.of( "-api-version", "11", - "@" + localPath("file.txt"), "@" + localPath("file2.txt"), "-java-parameters", "-jvm-target", "11", "-include-runtime", @@ -187,17 +185,23 @@ class CompileOptionsTest { var bar = new File("bar.txt"); var options = new CompileOptions(); + options = options.argFile(foo); + assertThat(options.argFile()).contains(foo); + options.argFile().clear(); + assertThat(options.argFile()).isEmpty(); + options.argFile(foo, bar); assertThat(options.argFile()).contains(foo, bar); options.argFile().clear(); + assertThat(options.argFile()).isEmpty(); options = options.argFile(foo.toPath(), bar.toPath()); assertThat(options.argFile()).contains(foo, bar); options.argFile().clear(); + assertThat(options.argFile()).isEmpty(); - options.argFile(foo.getAbsolutePath(), bar.getAbsolutePath()); + options = options.argFile(foo.getAbsolutePath(), bar.getAbsolutePath()); assertThat(options.argFile()).contains(new File(foo.getAbsolutePath()), new File(bar.getAbsolutePath())); - options.argFile().clear(); } @Test @@ -210,7 +214,6 @@ class CompileOptionsTest { var params = new CompileOptions() .advancedOptions("Xoption") .apiVersion("11") - .argFile("file") .expression("expression") .includeRuntime(true) .javaParameters(true) @@ -233,7 +236,7 @@ class CompileOptionsTest { .wError(true) .wExtra(true); - var skipArgs = List.of("-J", "-classpath"); + var skipArgs = List.of("-J", "-classpath", "@"); assertThat(args).as(skipArgs + " not found.").containsAll(skipArgs); args.removeAll(skipArgs); @@ -257,31 +260,45 @@ class CompileOptionsTest { var bar = new File("bar.txt"); var options = new CompileOptions(); + options = options.classpath(foo); + assertThat(options.classpath()).as("File").containsExactly(foo); + options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); + + options.classpath(foo, bar); assertThat(options.classpath()).as("File...").containsExactly(foo, bar); options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); + options.classpath(List.of(foo, bar)); assertThat(options.classpath()).as("List(File...)").containsExactly(foo, bar); options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); + options = options.classpath(foo.toPath(), bar.toPath()); assertThat(options.classpath()).as("Path...").containsExactly(foo, bar); options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); + options = options.classpathPaths(List.of(foo.toPath(), bar.toPath())); assertThat(options.classpath()).as("List(Path...)").containsExactly(foo, bar); options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); + options.classpath(foo.getAbsolutePath(), bar.getAbsolutePath()); assertThat(options.classpath()).as("String...") .containsExactly(new File(foo.getAbsolutePath()), new File(bar.getAbsolutePath())); options.classpath().clear(); + assertThat(options.argFile()).isEmpty(); options.classpathStrings(List.of(foo.getAbsolutePath(), bar.getAbsolutePath())); assertThat(options.classpath()).as("List(String...)") .containsExactly(new File(foo.getAbsolutePath()), new File(bar.getAbsolutePath())); - options.classpath().clear(); } @Test diff --git a/src/test/resources/argfile.txt b/src/test/resources/argfile.txt new file mode 100644 index 0000000..d128d62 --- /dev/null +++ b/src/test/resources/argfile.txt @@ -0,0 +1,3 @@ +-Xjdk-release=17 -no-reflect + +-progressive diff --git a/src/test/resources/argfile2.txt b/src/test/resources/argfile2.txt new file mode 100644 index 0000000..93f9181 --- /dev/null +++ b/src/test/resources/argfile2.txt @@ -0,0 +1 @@ +-include-runtime \ No newline at end of file