Skip to content

Commit

Permalink
Merge pull request #1962 from usethesource/mvn-scheme
Browse files Browse the repository at this point in the history
Introduced a new resolver for the `mvn:///` scheme.
  • Loading branch information
jurgenvinju authored Jun 10, 2024
2 parents fb13890 + 8030a77 commit 7a28561
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/org/rascalmpl/interpreter/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import org.rascalmpl.debug.IRascalSuspendTrigger;
import org.rascalmpl.debug.IRascalSuspendTriggerListener;
import org.rascalmpl.exceptions.ImplementationError;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.exceptions.StackTrace;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.interpreter.asserts.NotYetImplemented;
Expand Down
20 changes: 2 additions & 18 deletions src/org/rascalmpl/interpreter/utils/RascalManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -22,6 +21,7 @@
import org.rascalmpl.library.util.PathConfig;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.uri.jar.JarURIResolver;

import io.usethesource.vallang.ISourceLocation;

Expand Down Expand Up @@ -256,7 +256,7 @@ public InputStream manifest(Class<?> clazz) {

public InputStream manifest(ISourceLocation root) {
try {
return URIResolverRegistry.getInstance().getInputStream(URIUtil.getChildLocation(jarify(root), META_INF_RASCAL_MF));
return URIResolverRegistry.getInstance().getInputStream(URIUtil.getChildLocation(JarURIResolver.jarify(root), META_INF_RASCAL_MF));
} catch (IOException e) {
return null;
}
Expand Down Expand Up @@ -289,22 +289,6 @@ public PathConfig makePathConfig(ISourceLocation root) throws IOException {
return new PathConfig(srcs, libs, binFolder);
}

public static ISourceLocation jarify(ISourceLocation loc) {
if (!loc.getPath().endsWith(".jar")) {
return loc;
}

try {
loc = URIUtil.changeScheme(loc, "jar+" + loc.getScheme());
loc = URIUtil.changePath(loc, loc.getPath() + "!/");
return loc;
}
catch (URISyntaxException e) {
assert false; // this can never happen;
return loc;
}
}

public InputStream manifest(JarInputStream stream) {
JarEntry next = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import org.objectweb.asm.tree.ModuleNode;
import org.objectweb.asm.tree.ModuleOpenNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.uri.jar.JarURIResolver;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
Expand Down Expand Up @@ -141,7 +141,7 @@ private void createM3() {
try (JarInputStream jarStream = new JarInputStream(registry.getInputStream(loc))) {
JarEntry entry = jarStream.getNextJarEntry();
while (entry != null) {
compUnitPhysical = URIUtil.getChildLocation(RascalManifest.jarify(loc), entry.getName());
compUnitPhysical = URIUtil.getChildLocation(JarURIResolver.jarify(loc), entry.getName());

if (entry.getName().endsWith(".class")) {
String compUnit = getCompilationUnitRelativePath();
Expand Down Expand Up @@ -174,7 +174,7 @@ private void createSingleClassM3(String className, IList classpath) {
String compUnit = className.replaceAll("\\.", "/");
ClassReader classReader = resolver.buildClassReader(className);

this.compUnitPhysical = URIUtil.getChildLocation(RascalManifest.jarify(loc), compUnit + ".class");
this.compUnitPhysical = URIUtil.getChildLocation(JarURIResolver.jarify(loc), compUnit + ".class");
setCompilationUnitRelations(compUnit);
setPackagesRelations(compUnit);
setClassRelations(classReader, compUnit);
Expand Down
70 changes: 69 additions & 1 deletion src/org/rascalmpl/library/lang/rascal/tests/basic/Locations.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ListRelation;
import IO;
import util::Math;
import Location;
import util::FileSystem;

int singleChar(str s) = charAt(s,0);

Expand Down Expand Up @@ -518,4 +519,71 @@ test bool extensionSetSimple()


// we don't want backslashes in windows
test bool correctTempPathResolverOnWindows() = /\\/ !:= resolveLocation(|tmp:///|).path;
test bool correctTempPathResolverOnWindows() = /\\/ !:= resolveLocation(|tmp:///|).path;

private data MavenLocalRepositoryPath
= path(str groupId, str artifactId, str version)
| error(str cause)
;

private MavenLocalRepositoryPath parseMavenLocalRepositoryPath(loc jar) {
if (jar.extension != "jar") {
return error("jar should have jar extension");
}

groupId = replaceAll(jar.parent.parent.parent.path[1..], "/", ".");
artifactId = jar.parent.parent.file;
version = jar.parent.file;
file = jar.file;

if (file != "<artifactId>-<version>.jar") {
return error("This is not a repository release jar; filename should be ArtifactId-Version.jar: <jar.file>");
}

if (/!/ := artifactId) {
return error("ArtifactId contains exclamation mark: <artifactId>");
}

return path(groupId, artifactId, version);
}

test bool mvnSchemeTest() {
debug = false;
jarFiles = find(|mvn:///|, "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">";
}
}

// report on all the failed attempts
for (debug, jar <- jarFiles, error(msg) := parseMavenLocalRepositoryPath(jar)) {
println(msg);
}


return true;
}
3 changes: 2 additions & 1 deletion src/org/rascalmpl/library/util/PathConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.rascalmpl.uri.ILogicalSourceLocationResolver;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.uri.jar.JarURIResolver;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.ValueFactoryFactory;

Expand Down Expand Up @@ -485,7 +486,7 @@ public static PathConfig fromSourceProjectRascalManifest(ISourceLocation manifes
String libProjectName = manifest.getManifestProjectName(manifest.manifest(dep));

if (libProjectName != null) {
mavenLibs.put(libProjectName, RascalManifest.jarify(dep));
mavenLibs.put(libProjectName, JarURIResolver.jarify(dep));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/rascalmpl/uri/file/AliasedFileResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public abstract class AliasedFileResolver implements ILogicalSourceLocationResolver {

private final String scheme;
private final ISourceLocation root;
protected final ISourceLocation root;

AliasedFileResolver(String scheme, String rootPath) {
this.scheme = scheme;
Expand Down
Loading

0 comments on commit 7a28561

Please sign in to comment.