Skip to content

Commit aa419ff

Browse files
committed
fix: resource references with property resolving
1 parent 4497615 commit aa419ff

File tree

6 files changed

+105
-3
lines changed

6 files changed

+105
-3
lines changed

itests/linuxxyz.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
import static java.lang.System.*;
4+
5+
public class linux {
6+
7+
public static void main(String... args) {
8+
out.println("Hello linux");
9+
}
10+
}

itests/osxxyz.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
import static java.lang.System.*;
4+
5+
public class osx {
6+
7+
public static void main(String... args) {
8+
out.println("Hello osx");
9+
}
10+
}

itests/winxyz.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
import static java.lang.System.*;
4+
5+
public class win {
6+
7+
public static void main(String... args) {
8+
out.println("Hello Windows");
9+
}
10+
}

src/main/java/dev/jbang/dependencies/Detector.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.List;
44
import java.util.Properties;
55

6+
import dev.jbang.util.Util;
7+
68
public class Detector extends kr.motd.maven.os.Detector {
79

810
public Detector() {
@@ -16,13 +18,19 @@ protected void log(String message) {
1618

1719
@Override
1820
protected void logProperty(String name, String value) {
19-
21+
Util.verboseMsg("Auto-detected: " + name + "=" + value);
2022
}
2123

2224
public void detect(Properties properties, List<String> classiferWithLikes) {
2325
super.detect(properties, classiferWithLikes);
2426

2527
// "hack" to expose a property that works with javafx mac classifers
28+
// https://repo1.maven.org/maven2/org/openjfx/javafx-controls/26-ea+3/
29+
// linux-aarch64
30+
// linux
31+
// mac-aarch64
32+
// mac
33+
// win
2634
String os = properties.getProperty("os.detected.name");
2735
if (os.equals("osx")) {
2836
os = "mac";
@@ -31,6 +39,11 @@ public void detect(Properties properties, List<String> classiferWithLikes) {
3139
}
3240
} else if (os.equals("windows")) {
3341
os = "win";
42+
} else if (os.equals("linux")) {
43+
os = "linux";
44+
if ("aarch64".equals(System.getProperty("os.arch"))) {
45+
os = "linux-aarch64";
46+
}
3447
}
3548
properties.setProperty("os.detected.jfxname", os);
3649
}

src/main/java/dev/jbang/source/ProjectBuilder.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,41 @@ private List<MavenRepo> allToMavenRepo(List<String> repos) {
258258
}
259259

260260
public Project build(String resource) {
261-
ResourceRef resourceRef = resolveChecked(getResourceResolver(), resource);
261+
ResourceRef resourceRef = resolveChecked(getResourceResolver(), resource, true);
262262
return build(resourceRef);
263263
}
264264

265265
private ResourceRef resolveChecked(ResourceResolver resolver, String resource) {
266-
Util.verboseMsg("Resolving resource ref: " + resource);
266+
return resolveChecked(resolver, resource, false);
267+
}
268+
269+
/**
270+
* Resolves the given resource, with property resolution from project context
271+
* and handles retry on caching.
272+
*
273+
* Note: only enable property replacement if you know the resource is not
274+
* something that can be embedded/included directly in the project from users
275+
* data.
276+
*
277+
* i.e. jbang run someref${os.detected.os}.jar is ok but //FILES with
278+
* ${user.home} is definitely not.
279+
*
280+
* @param resolver
281+
* @param resource
282+
* @return
283+
*/
284+
private ResourceRef resolveChecked(ResourceResolver resolver, String orginal_resource,
285+
boolean propertyReplacement) {
286+
final String resource = propertyReplacement
287+
? PropertiesValueResolver.replaceProperties(orginal_resource, getContextProperties())
288+
: orginal_resource;
289+
290+
if (!resource.equals(orginal_resource)) {
291+
Util.verboseMsg("Resolving resource ref: " + orginal_resource + " -> " + resource);
292+
} else {
293+
Util.verboseMsg("Resolving resource ref:" + resource);
294+
}
295+
267296
boolean retryCandidate = catalogFile == null && !Util.isFresh() && Settings.getCacheEvict() > 0
268297
&& (Catalog.isValidName(resource) || Catalog.isValidCatalogReference(resource)
269298
|| Util.isRemoteRef(resource));
@@ -618,6 +647,7 @@ private ResourceResolver getAliasResourceResolver(Alias alias) {
618647
if (alias != null) {
619648
updateFromAlias(alias);
620649
}
650+
621651
return new CombinedResourceResolver(
622652
new RenamingScriptResourceResolver(forceType),
623653
new LiteralScriptResourceResolver(forceType),

src/test/java/dev/jbang/cli/TestRun.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.Properties;
3738
import java.util.jar.Attributes;
3839
import java.util.jar.JarFile;
3940
import java.util.regex.Pattern;
@@ -65,6 +66,7 @@
6566
import dev.jbang.catalog.Alias;
6667
import dev.jbang.catalog.Catalog;
6768
import dev.jbang.catalog.CatalogUtil;
69+
import dev.jbang.dependencies.Detector;
6870
import dev.jbang.net.TrustedSources;
6971
import dev.jbang.source.BuildContext;
7072
import dev.jbang.source.Builder;
@@ -80,6 +82,7 @@
8082
import dev.jbang.source.sources.JavaSource;
8183
import dev.jbang.util.CommandBuffer;
8284
import dev.jbang.util.JavaUtil;
85+
import dev.jbang.util.PropertiesValueResolver;
8386
import dev.jbang.util.Util;
8487

8588
import picocli.CommandLine;
@@ -2101,6 +2104,32 @@ void testJavaFXMagicPropertyViaCommandline() throws IOException {
21012104

21022105
}
21032106

2107+
@Test
2108+
void testMagicPropertyViaRun(@TempDir Path tdir) throws IOException {
2109+
2110+
String fileref = examplesTestFolder.resolve("${os.detected.name}${customvar}.java").toString();
2111+
// todo fix so --deps can use system properties
2112+
CommandLine.ParseResult pr = JBang.getCommandLine()
2113+
.parseArgs("run",
2114+
"-Dcustomvar=xyz",
2115+
fileref.toString());
2116+
2117+
Run run = (Run) pr.subcommand().commandSpec().userObject();
2118+
2119+
ProjectBuilder pb = run.createProjectBuilderForRun();
2120+
pb.mainClass("fakemain");
2121+
2122+
Project prj = pb.build(fileref.toString());
2123+
String line = run.updateGeneratorForRun(CmdGenerator.builder(prj)).build().generate();
2124+
2125+
Properties props = new Properties();
2126+
new Detector().detect(props, Collections.emptyList());
2127+
String expectedJar = PropertiesValueResolver.replaceProperties("${os.detected.name}xyz.jar", props);
2128+
2129+
assertThat(line, containsString(expectedJar));
2130+
2131+
}
2132+
21042133
@Test
21052134
void testScriptCliReposAndDeps(@TempDir File output) throws IOException {
21062135
String base = "" +

0 commit comments

Comments
 (0)