Skip to content

Commit

Permalink
refactor: added manifest attributes to SourceSet
Browse files Browse the repository at this point in the history
Instead of having specific attributes on `RunContext` we now have a
generic `manifestAttributes` on `SourceSet` to store any attributes.
  • Loading branch information
quintesse committed Jun 15, 2022
1 parent c1fb77e commit aadbb34
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
18 changes: 0 additions & 18 deletions src/main/java/dev/jbang/source/RunContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public class RunContext {
**/
private String javaAgentOption;
private List<AgentSourceContext> javaAgents;
private String preMainClass;
private List<String> integrationOptions;
private String agentMainClass;
private File catalogFile;

private Alias alias;
Expand Down Expand Up @@ -289,22 +287,6 @@ public void setJavaAgentOption(String option) {
this.javaAgentOption = option;
}

public String getAgentMainClass() {
return agentMainClass;
}

public void setAgentMainClass(String b) {
agentMainClass = b;
}

public String getPreMainClass() {
return preMainClass;
}

public void setPreMainClass(String name) {
preMainClass = name;
}

public boolean isNativeImage() {
return nativeImage;
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/dev/jbang/source/SourceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SourceSet implements Code {
private final List<String> classPaths = new ArrayList<>();
private final List<String> compileOptions = new ArrayList<>();
private final List<String> runtimeOptions = new ArrayList<>();
private final List<KeyValue> agentOptions = new ArrayList<>();
private final Map<String, String> manifestAttributes = new LinkedHashMap<>();
private String javaVersion;
private String description;
private String gav;
Expand All @@ -38,6 +38,9 @@ public class SourceSet implements Code {
private File jarFile;
private Jar jar;

public static final String ATTR_PREMAIN_CLASS = "Premain-Class";
public static final String ATTR_AGENT_CLASS = "Agent-Class";

public static SourceSet forSource(Source mainSource) {
return new SourceSet(mainSource, ResourceResolver.forResources());
}
Expand Down Expand Up @@ -85,7 +88,11 @@ private void addSource(Source source, String javaVersion, Set<ResourceRef> refs)
repositories.addAll(source.collectRepositories());
compileOptions.addAll(source.getCompileOptions());
runtimeOptions.addAll(source.getRuntimeOptions());
agentOptions.addAll(source.collectAgentOptions());
source.collectAgentOptions().forEach(kv -> {
if (!kv.getKey().isEmpty()) {
getManifestAttributes().put(kv.getKey(), kv.getValue() != null ? kv.getValue() : "true");
}
});
String version = source.getJavaVersion();
if (version != null && JavaUtil.checkRequestedVersion(version)) {
if (new JavaUtil.RequestedVersionComparator().compare(javaVersion, version) > 0) {
Expand Down Expand Up @@ -207,20 +214,16 @@ public SourceSet addRuntimeOptions(Collection<String> options) {
}

@Nonnull
public List<KeyValue> getAgentOptions() {
return Collections.unmodifiableList(agentOptions);
public Map<String, String> getManifestAttributes() {
return manifestAttributes;
}

@Nonnull
public SourceSet addAgentOption(KeyValue option) {
agentOptions.add(option);
return this;
public void setAgentMainClass(String agentMainClass) {
manifestAttributes.put(ATTR_AGENT_CLASS, agentMainClass);
}

@Nonnull
public SourceSet addAgentOptions(Collection<KeyValue> options) {
agentOptions.addAll(options);
return this;
public void setPreMainClass(String preMainClass) {
manifestAttributes.put(ATTR_PREMAIN_CLASS, preMainClass);
}

@Nullable
Expand Down
24 changes: 4 additions & 20 deletions src/main/java/dev/jbang/source/builders/BaseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public abstract class BaseBuilder implements Builder {
public static final String ATTR_BUILD_JDK = "Build-Jdk";
public static final String ATTR_JBANG_JAVA_OPTIONS = "JBang-Java-Options";
public static final String ATTR_BOOT_CLASS_PATH = "Boot-Class-Path";
public static final String ATTR_PREMAIN_CLASS = "Premain-Class";
public static final String ATTR_AGENT_CLASS = "Agent-Class";

public static final Type STRINGARRAYTYPE = Type.create(DotName.createSimple("[Ljava.lang.String;"),
Type.Kind.ARRAY);
Expand Down Expand Up @@ -205,23 +203,9 @@ public static void createJar(SourceSet ss, RunContext ctx, File compileDir, File
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainclass);
}

if (ss.getMainSource().isAgent()) {
if (ctx.getPreMainClass() != null) {
manifest.getMainAttributes().put(new Attributes.Name(ATTR_PREMAIN_CLASS), ctx.getPreMainClass());
}
if (ctx.getAgentMainClass() != null) {
manifest.getMainAttributes().put(new Attributes.Name(ATTR_AGENT_CLASS), ctx.getAgentMainClass());
}

for (KeyValue kv : ss.getAgentOptions()) {
if (Util.isBlankString(kv.getKey())) {
continue;
}
Attributes.Name k = new Attributes.Name(kv.getKey());
String v = kv.getValue() == null ? "true" : kv.getValue();
manifest.getMainAttributes().put(k, v);
}
ss.getManifestAttributes().forEach((k, v) -> manifest.getMainAttributes().putValue(k, v));

if (ss.getMainSource().isAgent()) {
String bootClasspath = ss.getClassPath().getManifestPath();
if (!bootClasspath.isEmpty()) {
manifest.getMainAttributes().put(new Attributes.Name(ATTR_BOOT_CLASS_PATH), bootClasspath);
Expand Down Expand Up @@ -446,7 +430,7 @@ protected void searchForMain(File tmpJarDir) {
.findFirst();

if (agentmain.isPresent()) {
ctx.setAgentMainClass(agentmain.get().name().toString());
ss.setAgentMainClass(agentmain.get().name().toString());
}

Optional<ClassInfo> premain = classes .stream()
Expand All @@ -459,7 +443,7 @@ protected void searchForMain(File tmpJarDir) {
.findFirst();

if (premain.isPresent()) {
ctx.setPreMainClass(premain.get().name().toString());
ss.setPreMainClass(premain.get().name().toString());
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static dev.jbang.source.SourceSet.ATTR_AGENT_CLASS;
import static dev.jbang.source.SourceSet.ATTR_PREMAIN_CLASS;
import static dev.jbang.util.Util.writeString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down Expand Up @@ -1161,8 +1163,8 @@ void testAgent(@TempDir Path output) throws IOException {

assertThat(ss.getMainSource().isAgent(), is(true));

assertThat(ctx.getAgentMainClass(), is("Agent"));
assertThat(ctx.getPreMainClass(), is("Agent"));
assertThat(ss.getManifestAttributes().get(ATTR_AGENT_CLASS), is("Agent"));
assertThat(ss.getManifestAttributes().get(ATTR_PREMAIN_CLASS), is("Agent"));

try (JarFile jf = new JarFile(ss.getJarFile())) {
Attributes attrs = jf.getManifest().getMainAttributes();
Expand Down Expand Up @@ -1190,8 +1192,8 @@ void testpreAgent(@TempDir Path output) throws IOException {

assertThat(ss.getMainSource().isAgent(), is(true));

assertThat(ctx.getAgentMainClass(), is(nullValue()));
assertThat(ctx.getPreMainClass(), is("Agent"));
assertThat(ss.getManifestAttributes().get(ATTR_AGENT_CLASS), is(nullValue()));
assertThat(ss.getManifestAttributes().get(ATTR_PREMAIN_CLASS), is("Agent"));

}

Expand Down

0 comments on commit aadbb34

Please sign in to comment.