Skip to content

Commit

Permalink
feat!: alias arguments are now appended
Browse files Browse the repository at this point in the history
Arguments passed to an alias are now appended to any arguments that are
already defined in the alias instead of replacing them.

Fixes jbangdev#605
  • Loading branch information
quintesse committed Feb 21, 2024
1 parent 1657c47 commit f0e7c87
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/dev/jbang/source/CmdGeneratorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ private NativeCmdGenerator createNativeCmdGenerator() {
private void updateFromAlias(Alias alias) {
if (arguments.isEmpty()) {
setArguments(handleRemoteFiles(alias.arguments));
} else if (!alias.arguments.isEmpty()) {
List<String> args = new ArrayList<>(handleRemoteFiles(alias.arguments));
args.addAll(arguments);
setArguments(args);
}
if (runtimeOptions.isEmpty()) {
runtimeOptions(alias.runtimeOptions);
} else if (!alias.runtimeOptions.isEmpty()) {
List<String> opts = new ArrayList<>(alias.runtimeOptions);
opts.addAll(runtimeOptions);
runtimeOptions(opts);
}
if (mainClass == null) {
mainClass(alias.mainClass);
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
import dev.jbang.BaseTest;
import dev.jbang.Cache;
import dev.jbang.Settings;
import dev.jbang.catalog.Alias;
import dev.jbang.catalog.Catalog;
import dev.jbang.catalog.CatalogUtil;
import dev.jbang.net.JdkManager;
import dev.jbang.net.TrustedSources;
import dev.jbang.source.BuildContext;
Expand Down Expand Up @@ -2472,4 +2474,28 @@ void testHelloWorldGAVWithModulesButNoManifest() throws IOException {

assertThat(e.getMessage(), startsWith("no main class"));
}

@Test
void testAliasArguments() throws IOException {
File f = examplesTestFolder.resolve("echo.java").toFile();
List<String> args = Arrays.asList("foo", "bar");
Alias alias = new Alias(f.toString(), null, args, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null,
null, null, null, null, null, null, null, null);
CatalogUtil.addNearestAlias("echo", alias);

CommandLine.ParseResult pr = JBang .getCommandLine()
.parseArgs("run", "echo", "baz");
Run run = (Run) pr.subcommand().commandSpec().userObject();

ProjectBuilder pb = run.createProjectBuilderForRun();
Project prj = pb.build("echo");

BuildContext ctx = BuildContext.forProject(prj, null);
CmdGeneratorBuilder genb = Project.codeBuilder(ctx).build();

String cmdline = run.updateGeneratorForRun(genb).build().generate();

assertThat(cmdline, endsWith(" foo bar baz"));
}
}

0 comments on commit f0e7c87

Please sign in to comment.