Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: merge alias attributes #1913

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions src/main/java/dev/jbang/catalog/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import static dev.jbang.cli.BaseCommand.EXIT_INVALID_INPUT;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -205,40 +202,30 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
names.add(name);
a2 = merge(a2, a2.scriptRef, findUnqualifiedAlias, names);
String desc = a1.description != null ? a1.description : a2.description;
List<String> args = a1.arguments != null && !a1.arguments.isEmpty() ? a1.arguments : a2.arguments;
List<String> jopts = a1.runtimeOptions != null && !a1.runtimeOptions.isEmpty() ? a1.runtimeOptions
: a2.runtimeOptions;
List<String> srcs = a1.sources != null && !a1.sources.isEmpty() ? a1.sources
: a2.sources;
List<String> ress = a1.resources != null && !a1.resources.isEmpty() ? a1.resources
: a2.resources;
List<String> deps = a1.dependencies != null && !a1.dependencies.isEmpty() ? a1.dependencies
: a2.dependencies;
List<String> repos = a1.repositories != null && !a1.repositories.isEmpty() ? a1.repositories
: a2.repositories;
List<String> cpaths = a1.classpaths != null && !a1.classpaths.isEmpty() ? a1.classpaths
: a2.classpaths;
Map<String, String> props = a1.properties != null && !a1.properties.isEmpty() ? a1.properties
: a2.properties;
List<String> args = join(a1.arguments, a2.arguments);
List<String> jopts = join(a1.runtimeOptions, a2.runtimeOptions);
List<String> srcs = join(a1.sources, a2.sources);
List<String> ress = join(a1.resources, a2.resources);
List<String> deps = join(a1.dependencies, a2.dependencies);
List<String> repos = join(a1.repositories, a2.repositories);
List<String> cpaths = join(a1.classpaths, a2.classpaths);
Map<String, String> props = join(a1.properties, a2.properties);
String javaVersion = a1.javaVersion != null ? a1.javaVersion : a2.javaVersion;
String mainClass = a1.mainClass != null ? a1.mainClass : a2.mainClass;
String moduleName = a1.moduleName != null ? a1.moduleName : a2.moduleName;
List<String> copts = a1.compileOptions != null && !a1.compileOptions.isEmpty() ? a1.compileOptions
: a2.compileOptions;
List<String> nopts = a1.nativeOptions != null && !a1.nativeOptions.isEmpty() ? a1.nativeOptions
: a2.nativeOptions;
List<String> copts = join(a1.compileOptions, a2.compileOptions);
List<String> nopts = join(a1.nativeOptions, a2.nativeOptions);
Boolean nimg = a1.nativeImage != null ? a1.nativeImage : a2.nativeImage;
Boolean ints = a1.integrations != null ? a1.integrations : a2.integrations;
String jfr = a1.jfr != null ? a1.jfr : a2.jfr;
Map<String, String> debug = a1.debug != null ? a1.debug : a2.debug;
Map<String, String> debug = join(a1.debug, a2.debug);
Boolean cds = a1.cds != null ? a1.cds : a2.cds;
Boolean inter = a1.interactive != null ? a1.interactive : a2.interactive;
Boolean ep = a1.enablePreview != null ? a1.enablePreview : a2.enablePreview;
Boolean ea = a1.enableAssertions != null ? a1.enableAssertions : a2.enableAssertions;
Boolean esa = a1.enableSystemAssertions != null ? a1.enableSystemAssertions : a2.enableSystemAssertions;
Map<String, String> mopts = a1.manifestOptions != null && !a1.manifestOptions.isEmpty() ? a1.manifestOptions
: a2.manifestOptions;
List<JavaAgent> jags = a1.javaAgents != null && !a1.javaAgents.isEmpty() ? a1.javaAgents : a2.javaAgents;
Map<String, String> mopts = join(a1.manifestOptions, a2.manifestOptions);
List<JavaAgent> jags = join(a1.javaAgents, a2.javaAgents);
Catalog catalog = a2.catalog != null ? a2.catalog : a1.catalog;
return new Alias(a2.scriptRef, desc, args, jopts, srcs, ress, deps, repos, cpaths, props, javaVersion,
mainClass, moduleName, copts, nimg, nopts, ints, jfr, debug, cds, inter, ep, ea, esa, mopts, jags,
Expand All @@ -248,6 +235,36 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
}
}

private static <T> List<T> join(List<T> l1, List<T> l2) {
if (l1 != null && !l1.isEmpty()) {
if (l2 != null && !l2.isEmpty()) {
List<T> merged = new ArrayList<>(l1.size() + l2.size());
merged.addAll(l2);
merged.addAll(l1);
return merged;
} else {
return l1;
}
} else {
return l2;
}
}

private static <K, V> Map<K, V> join(Map<K, V> m1, Map<K, V> m2) {
if (m1 != null && !m1.isEmpty()) {
if (m2 != null && !m2.isEmpty()) {
Map<K, V> merged = new HashMap<>();
merged.putAll(m2);
merged.putAll(m1);
return merged;
} else {
return m1;
}
} else {
return m2;
}
}

/**
* Returns the given Alias from the local file system
*
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/dev/jbang/cli/TestAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,10 @@ void testGetAliasFour() throws IOException {
assertThat(alias.scriptRef, equalTo("http://dummy"));
assertThat(alias.resolve(), equalTo("http://dummy"));
assertThat(alias.description, equalTo("fourdesc"));
assertThat(alias.arguments, iterableWithSize(1));
assertThat(alias.arguments, contains("4"));
assertThat(alias.runtimeOptions, iterableWithSize(1));
assertThat(alias.runtimeOptions, contains("--four"));
assertThat(alias.arguments, iterableWithSize(2));
assertThat(alias.arguments, contains("3", "4"));
assertThat(alias.runtimeOptions, iterableWithSize(2));
assertThat(alias.runtimeOptions, contains("--three", "--four"));
assertThat(alias.sources, iterableWithSize(1));
assertThat(alias.sources, contains("foursrc"));
assertThat(alias.resources, iterableWithSize(1));
Expand All @@ -472,16 +472,16 @@ void testGetAliasFour() throws IOException {
assertThat(alias.repositories, contains("fourrepo"));
assertThat(alias.classpaths, iterableWithSize(1));
assertThat(alias.classpaths, contains("fourcp"));
assertThat(alias.properties, aMapWithSize(1));
assertThat(alias.properties, aMapWithSize(2));
assertThat(alias.properties, hasEntry("four", "4"));
assertThat(alias.javaVersion, equalTo("fourjava"));
assertThat(alias.mainClass, equalTo("fourmain"));
assertThat(alias.moduleName, equalTo("fourmodule"));
assertThat(alias.compileOptions, iterableWithSize(1));
assertThat(alias.compileOptions, contains("--cfour"));
assertThat(alias.compileOptions, iterableWithSize(2));
assertThat(alias.compileOptions, contains("--cthree", "--cfour"));
assertThat(alias.nativeImage, is(Boolean.FALSE));
assertThat(alias.nativeOptions, iterableWithSize(1));
assertThat(alias.nativeOptions, contains("--nfour"));
assertThat(alias.nativeOptions, iterableWithSize(2));
assertThat(alias.nativeOptions, contains("--nthree", "--nfour"));
assertThat(alias.jfr, equalTo("fourjfr"));
assertThat(alias.debug, aMapWithSize(1));
assertThat(alias.debug, hasEntry("fourd", "4"));
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,34 @@ void testAliasArguments() throws IOException {
assertThat(cmdline, endsWith("echo foo bar baz"));
}

@Test
void testAliasAliasArguments() throws IOException {
File f = examplesTestFolder.resolve("echo.java").toFile();
List<String> args = Arrays.asList("foo");
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, null);
CatalogUtil.addNearestAlias("foo", alias);

List<String> args2 = Arrays.asList("bar");
Alias alias2 = new Alias("foo", null, args2, null, 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("bar", alias2);

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

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

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();
Expand Down
Loading