Skip to content
31 changes: 8 additions & 23 deletions src/org/rascalmpl/library/lang/rascal/tests/basic/Locations.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ private MavenLocalRepositoryPath parseMavenLocalRepositoryPath(loc jar) {
return error("jar should have jar extension");
}

jar = relativize(|home:///.m2/repository|, jar);

groupId = replaceAll(jar.parent.parent.parent.path[1..], "/", ".");
artifactId = jar.parent.parent.file;
version = jar.parent.file;
Expand All @@ -583,33 +585,16 @@ private MavenLocalRepositoryPath parseMavenLocalRepositoryPath(loc jar) {

test bool mvnSchemeTest() {
debug = false;
jarFiles = find(|mvn:///|, "jar");
jarFiles = find(|home:///.m2/repository|, "jar");

// check whether the implementation of the scheme holds the contract specified in the assert
for (jar <- jarFiles, path(groupId, artifactId, version) := parseMavenLocalRepositoryPath(jar)) {
// this is the contract:
mvnLoc = |mvn://<groupId>--<artifactId>--<version>|;

assert resolveLocation(mvnLoc) == resolveLocation(jar) : "<resolveLocation(mvnLoc)> != <resolveLocation(jar)>
' jar: <jar>
' mvnLoc: <mvnLoc>";

assert exists(mvnLoc) : "<mvnLoc> should exist because <jar> exists.";

assert exists(mvnLoc + "!") : "<mvnLoc + "!"> should resolve to the jarified root and exist";

// not all jars contain a META-INF folder
if (exists(mvnLoc + "!/META-INF")) {
// but if they do then this relation holds

assert exists(mvnLoc + "META-INF")
: "<mvnLoc + "META-INF"> should exist and resolved to the jarified location inside.";

assert resolveLocation(mvnLoc + "!/META-INF") == resolveLocation(mvnLoc + "META-INF")
: "Two different ways of resolving inside the jar (with and without !) should be equivalent";

assert (mvnLoc + "META-INF").ls == [e[path=e.path[2..]] | e <- (mvnLoc + "!/META-INF").ls]
: "listings should be equal mod ! for <mvnLoc + "META-INF">";
loc mvnLoc = |mvn://<groupId>--<artifactId>--<version>|;

if (!exists(mvnLoc)) {
println("<mvnLoc> does not exist.");
return false;
}
}

Expand Down
33 changes: 23 additions & 10 deletions src/org/rascalmpl/library/util/Maven.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.cli.CliRequest;
import org.apache.maven.cli.MavenCli;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
Expand All @@ -37,17 +38,17 @@
* to a file. For instance, the output of `mvn dependency:build-classpath` can be redicted to a file
* by providing an additional argument `-Dmdep.outputFile=/path/to/file`.
*/
public static List<String> runCommand(List<String> args, ISourceLocation manifestRoot, Path outputFile) {
public static List<String> runCommand(List<String> args, @Nullable ISourceLocation manifestRoot, Path outputFile) {
try {
ISourceLocation pomxml = URIUtil.getChildLocation(manifestRoot, "pom.xml");
pomxml = URIResolverRegistry.getInstance().logicalToPhysical(pomxml);
manifestRoot = URIResolverRegistry.getInstance().logicalToPhysical(manifestRoot);
ISourceLocation pomxml = manifestRoot != null ? URIUtil.getChildLocation(manifestRoot, "pom.xml") : null;
pomxml = pomxml != null ? URIResolverRegistry.getInstance().logicalToPhysical(pomxml) : null;
manifestRoot = manifestRoot != null ? URIResolverRegistry.getInstance().logicalToPhysical(manifestRoot) : null;

if (!"file".equals(manifestRoot.getScheme())) {
if (manifestRoot != null && !"file".equals(manifestRoot.getScheme())) {
throw new IllegalArgumentException("`manifestRoot` could not be resolved");
}

if (!URIResolverRegistry.getInstance().exists(pomxml)) {
if (pomxml != null && !URIResolverRegistry.getInstance().exists(pomxml)) {
throw new IllegalArgumentException("`manifestRoot` does not contain pom.xml");
}

Expand Down Expand Up @@ -78,15 +79,27 @@
field.set(req, value);
}

private static CliRequest buildRequest(String[] args, ISourceLocation manifestRoot) throws ReflectiveOperationException {
private static CliRequest buildRequest(String[] args, @Nullable ISourceLocation manifestRoot) throws ReflectiveOperationException {
// we need to set a field that the default class doesn't set
// it's a work around around a bug in the MavenCli code
var cons = CliRequest.class.getDeclaredConstructor(String[].class, ClassWorld.class);
cons.setAccessible(true);
var result = cons.newInstance(args, null);
var manifestRootFile = new File(manifestRoot.getPath());
setField(result, "workingDirectory", manifestRootFile.getPath());
setField(result, "multiModuleProjectDirectory", manifestRootFile);

if (manifestRoot != null) {
var manifestRootFile = new File(manifestRoot.getPath());
setField(result, "workingDirectory", manifestRootFile.getPath());
setField(result, "multiModuleProjectDirectory", manifestRootFile);
}
else {
try {
setField(result, "multiModuleProjectDirectory", File.createTempFile("dummy", ""));

Check warning on line 96 in src/org/rascalmpl/library/util/Maven.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/Maven.java#L96

Added line #L96 was not covered by tests
}
catch (ReflectiveOperationException | IOException e) {

Check warning on line 98 in src/org/rascalmpl/library/util/Maven.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/Maven.java#L98

Added line #L98 was not covered by tests
// ignore for robustness sake, since we don't have a manifestRoot
}

Check warning on line 100 in src/org/rascalmpl/library/util/Maven.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/Maven.java#L100

Added line #L100 was not covered by tests
}

return result;
}

Expand Down
3 changes: 1 addition & 2 deletions src/org/rascalmpl/uri/URIResolverRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,7 @@ private boolean isRootLogical(ISourceLocation uri) {
&& logicalResolvers.containsKey(uri.getScheme());
}

public String[] listEntries(ISourceLocation uri) throws IOException {
uri = safeResolve(uri);
public String[] listEntries(ISourceLocation uri) throws IOException { uri = safeResolve(uri);
if (isRootLogical(uri)) {
// if it's a location without any path and authority
// we want to list possible authorities if it's a logical one
Expand Down
5 changes: 5 additions & 0 deletions src/org/rascalmpl/uri/URIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import org.rascalmpl.values.ValueFactoryFactory;

Expand Down Expand Up @@ -96,6 +97,10 @@ private static String decodeURLPart(String part) throws UnsupportedEncodingExcep
public static ISourceLocation createFileLocation(String path) throws URISyntaxException {
return vf.sourceLocation(createFile(path));
}

public static ISourceLocation createFileLocation(Path path) throws URISyntaxException {
return vf.sourceLocation(createFile(path.toString()));
}

private static String fixWindowsPath(String path) {
if (!path.startsWith(URI_PATH_SEPARATOR)) {
Expand Down
Loading
Loading