From 76819ce55d4eebc7bca87e03f18a99a06007ade6 Mon Sep 17 00:00:00 2001 From: Max Rydahl Andersen Date: Fri, 20 Sep 2024 00:04:40 +0200 Subject: [PATCH] feat: allow use of remote file args in compiler arguments --- itests/flags.json | 12 ++++ src/main/java/dev/jbang/cli/Run.java | 4 +- .../dev/jbang/source/CmdGeneratorBuilder.java | 12 +--- .../source/buildsteps/CompileBuildStep.java | 2 +- .../source/generators/JarCmdGenerator.java | 4 +- src/main/java/dev/jbang/util/Util.java | 8 +++ src/test/java/dev/jbang/cli/TestRun.java | 62 +++++++++++++++++++ 7 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 itests/flags.json diff --git a/itests/flags.json b/itests/flags.json new file mode 100644 index 000000000..fe8eb1523 --- /dev/null +++ b/itests/flags.json @@ -0,0 +1,12 @@ +[ + { + match: ["*.*"], + inline: [ + "+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations::dotProduct*", + "+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations::quantize*", + "+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations*::mpack*", + "+com.github.tjake.jlama.tensor.AbstractTensor::getOffset*", + "+com.github.tjake.jlama.tensor.*::getVector*" + ] + } +] diff --git a/src/main/java/dev/jbang/cli/Run.java b/src/main/java/dev/jbang/cli/Run.java index fd047436f..afc082e46 100644 --- a/src/main/java/dev/jbang/cli/Run.java +++ b/src/main/java/dev/jbang/cli/Run.java @@ -90,6 +90,9 @@ public Integer doCall() throws IOException { buildAgents(ctx); + // swizzle remote files after agents been added. + runMixin.javaRuntimeOptions = Util.handleRemoteFiles(runMixin.javaRuntimeOptions); + String cmdline = updateGeneratorForRun(genb).build().generate(); Util.verboseMsg("run: " + cmdline); @@ -113,7 +116,6 @@ void buildAgents(BuildContext ctx) throws IOException { if (runMixin.javaRuntimeOptions == null) { runMixin.javaRuntimeOptions = new ArrayList<>(); } - agents = handleRemoteFiles(agents); for (Map.Entry agentOption : agents.entrySet()) { String javaAgent = agentOption.getKey(); String javaAgentOptions = agentOption.getValue(); diff --git a/src/main/java/dev/jbang/source/CmdGeneratorBuilder.java b/src/main/java/dev/jbang/source/CmdGeneratorBuilder.java index 3c20c456a..2b37fa756 100644 --- a/src/main/java/dev/jbang/source/CmdGeneratorBuilder.java +++ b/src/main/java/dev/jbang/source/CmdGeneratorBuilder.java @@ -1,7 +1,6 @@ package dev.jbang.source; import java.util.*; -import java.util.stream.Collectors; import dev.jbang.catalog.Alias; import dev.jbang.source.generators.JarCmdGenerator; @@ -145,9 +144,9 @@ private NativeCmdGenerator createNativeCmdGenerator() { private void updateFromAlias(Alias alias) { if (arguments.isEmpty()) { - setArguments(handleRemoteFiles(alias.arguments)); + setArguments(Util.handleRemoteFiles(alias.arguments)); } else if (alias.arguments != null && !alias.arguments.isEmpty()) { - List args = new ArrayList<>(handleRemoteFiles(alias.arguments)); + List args = new ArrayList<>(Util.handleRemoteFiles(alias.arguments)); args.addAll(arguments); setArguments(args); } @@ -184,11 +183,4 @@ private void updateFromAlias(Alias alias) { } } - private static List handleRemoteFiles(List args) { - if (args != null) { - return args.stream().map(Util::substituteRemote).collect(Collectors.toList()); - } else { - return null; - } - } } diff --git a/src/main/java/dev/jbang/source/buildsteps/CompileBuildStep.java b/src/main/java/dev/jbang/source/buildsteps/CompileBuildStep.java index f18927d82..ad6cdf09c 100644 --- a/src/main/java/dev/jbang/source/buildsteps/CompileBuildStep.java +++ b/src/main/java/dev/jbang/source/buildsteps/CompileBuildStep.java @@ -74,7 +74,7 @@ protected Project compile() throws IOException { optionList.add("-source"); optionList.add("" + JavaUtil.javaVersion(requestedJavaVersion)); } - optionList.addAll(project.getMainSourceSet().getCompileOptions()); + optionList.addAll(Util.handleRemoteFiles(project.getMainSourceSet().getCompileOptions())); String path = ctx.resolveClassPath().getClassPath(); if (!Util.isBlankString(path)) { if (project.getModuleName().isPresent()) { diff --git a/src/main/java/dev/jbang/source/generators/JarCmdGenerator.java b/src/main/java/dev/jbang/source/generators/JarCmdGenerator.java index 7b3f16946..6530c9f64 100644 --- a/src/main/java/dev/jbang/source/generators/JarCmdGenerator.java +++ b/src/main/java/dev/jbang/source/generators/JarCmdGenerator.java @@ -159,8 +159,8 @@ protected List generateCommandLineList() throws IOException { fullArgs.add(javacmd); - fullArgs.addAll(project.getRuntimeOptions()); - fullArgs.addAll(runtimeOptions); + fullArgs.addAll(Util.handleRemoteFiles(project.getRuntimeOptions())); + fullArgs.addAll(Util.handleRemoteFiles(runtimeOptions)); fullArgs.addAll(ctx.resolveClassPath().getAutoDectectedModuleArguments(jdk)); fullArgs.addAll(optionalArgs); diff --git a/src/main/java/dev/jbang/util/Util.java b/src/main/java/dev/jbang/util/Util.java index c0de67a72..7a4fc4945 100644 --- a/src/main/java/dev/jbang/util/Util.java +++ b/src/main/java/dev/jbang/util/Util.java @@ -403,6 +403,14 @@ public static String unkebabify(String name) { return name; } + public static List handleRemoteFiles(List args) { + if (args != null) { + return args.stream().map(Util::substituteRemote).collect(Collectors.toList()); + } else { + return null; + } + } + public enum OS { linux, alpine_linux, mac, windows, aix, unknown } diff --git a/src/test/java/dev/jbang/cli/TestRun.java b/src/test/java/dev/jbang/cli/TestRun.java index 89df14043..694d6c15b 100644 --- a/src/test/java/dev/jbang/cli/TestRun.java +++ b/src/test/java/dev/jbang/cli/TestRun.java @@ -2404,6 +2404,68 @@ void testRemoteFileJavaagentComplex() throws Exception { assertThat(result.err, containsString("-javaagent:" + jar + "=test:" + file)); } + @Test + @SuppressWarnings("unchecked") + void testRemoteFileXXargs(@TempDir File output) throws Exception { + + wms.stubFor( + WireMock.get(urlEqualTo("/flags.json")) + .willReturn(aResponse() + .withHeader("Content-Type", "text/plain") + .withBodyFile("flags.json") + .withBody( + Util.readString( + examplesTestFolder.resolve("flags.json"))))); + + wms.start(); + + String script = examplesTestFolder.resolve("helloworld.java").toString(); + String arg = "http://localhost:" + wms.port() + "/flags.json"; + CaptureResult result = checkedRun(null, "run", "--verbose", + "-R=-XX:CompilerDirectivesFile=%{" + arg + "}", + script); + assertThat(result.err, containsString("Requesting HTTP GET " + arg)); + Path file = Util.downloadAndCacheFile(arg); + assertThat(result.err, containsString("-XX:CompilerDirectivesFile=" + file)); + } + + @Test + @SuppressWarnings("unchecked") + void testRemoteFileEmbeddedXXargs(@TempDir File output) throws Exception { + + wms.stubFor( + WireMock.get(urlEqualTo("/flags2.json")) + .willReturn(aResponse() + .withHeader("Content-Type", "text/plain") + .withBodyFile("flags2.json") + .withBody( + Util.readString( + examplesTestFolder.resolve("flags.json"))))); + + wms.start(); + String arg = "http://localhost:" + wms.port() + "/flags2.json"; + + String directives = "//RUNTIME_OPTIONS -XX:CompilerDirectivesFile=%{" + arg + "}\n" + + "class funky { static void main(String args[]) {} }"; + + Path f = output.toPath().resolve("funky.java"); + + Util.writeString(f, directives); + + String script = f.toAbsolutePath().toString(); + + CaptureResult result = checkedRun(null, "run", "--verbose", + script); + assertThat(result.err, containsString("Requesting HTTP GET " + arg)); + Path file = Util.downloadAndCacheFile(arg); + assertThat(result.err, containsString("-XX:CompilerDirectivesFile=" + file)); + + } + + void testRemoteFileEmbeddedInOptions() { + + } + @Test @SuppressWarnings("unchecked") void testRemoteFileArgSimpleEscaped() throws Exception {