Skip to content

Commit

Permalink
feat!: alias arguments are now appended (#1753)
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 #605
  • Loading branch information
quintesse authored Feb 22, 2024
1 parent 7380851 commit 8855f35
Show file tree
Hide file tree
Showing 2 changed files with 52 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 != null && !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 != null && !alias.runtimeOptions.isEmpty()) {
List<String> opts = new ArrayList<>(alias.runtimeOptions);
opts.addAll(runtimeOptions);
runtimeOptions(opts);
}
if (mainClass == null) {
mainClass(alias.mainClass);
Expand Down
44 changes: 44 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,46 @@ 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("echo foo bar baz"));
}

@Test
void testCatalogAliasArguments() throws IOException {
File f = examplesTestFolder.resolve("jbang-catalog.json").toFile();
CommandLine.ParseResult pr = JBang .getCommandLine()
.parseArgs("run", "--catalog", f.getAbsolutePath(), "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("echo baz"));
}
}

0 comments on commit 8855f35

Please sign in to comment.