Skip to content

Commit

Permalink
Support links in classpath files
Browse files Browse the repository at this point in the history
Fix #3803
  • Loading branch information
laeubi committed May 3, 2024
1 parent 6428167 commit 93b53da
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ public ArtifactKey getArtifactKey(Artifact artifact) {
}

public Optional<EclipseProject> getEclipseProject(MavenProject project) {
File projectFile = new File(project.getBasedir(), ".project");
return getEclipseProject(project.getBasedir());
}

public Optional<EclipseProject> getEclipseProject(File baseDir) {
File projectFile = new File(baseDir, ".project");
return eclipseProjectCache.computeIfAbsent(projectFile, file -> {
if (file.isFile()) {
try {
Expand Down Expand Up @@ -273,7 +277,7 @@ public Optional<Processor> getBndTychoProject(MavenProject project) {
* Determine the list of dependencies for a given project as a collection of path items
*
* @param project
* the project to use to determine the dependencies
* the project to use to determine the dependencies
* @return a Collection of pathes describing the project dependencies
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -25,6 +26,7 @@
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.MavenArtifactKey;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.model.classpath.ClasspathParser;
import org.eclipse.tycho.model.classpath.JUnitBundle;
import org.eclipse.tycho.model.classpath.ProjectClasspathEntry;
Expand All @@ -37,15 +39,23 @@ public class ClasspathReader implements Disposable {
@Requirement
Logger logger;

@Requirement
TychoProjectManager projectManager;

@Override
public void dispose() {
cache.clear();
}

public Collection<ProjectClasspathEntry> parse(File basedir) throws IOException {
return cache.computeIfAbsent(basedir.getCanonicalPath(), f -> {

Path resolvedClasspath = projectManager.getEclipseProject(basedir)
.map(project -> project.getFile(ClasspathParser.CLASSPATH_FILENAME))
.orElse(basedir.toPath().resolve(ClasspathParser.CLASSPATH_FILENAME));

return cache.computeIfAbsent(resolvedClasspath.normalize().toString(), f -> {
try {
return ClasspathParser.parse(basedir);
return ClasspathParser.parse(resolvedClasspath.toFile());
} catch (IOException e) {
logger.warn("Can't read classpath from " + basedir);
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ protected void initModel(Model model, Reader artifactReader, Path artifactFile)
}
List<SourceFolderClasspathEntry> sourceFolders = new ArrayList<SourceFolderClasspathEntry>(1);
List<SourceFolderClasspathEntry> testSourceFolders = new ArrayList<SourceFolderClasspathEntry>(1);
for (ProjectClasspathEntry entry : ClasspathParser.parse(bundleRoot.toFile())) {
for (ProjectClasspathEntry entry : ClasspathParser
.parse(bundleRoot.resolve(ClasspathParser.CLASSPATH_FILENAME).toFile())) {
if (entry instanceof SourceFolderClasspathEntry source) {
if (source.isTest()) {
testSourceFolders.add(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.xml.sax.SAXException;

public class ClasspathParser {
public static final String CLASSPATH_FILENAME = ".classpath";

public static Collection<ProjectClasspathEntry> parse(File basedir) throws IOException {
File file = new File(basedir, ".classpath");
public static Collection<ProjectClasspathEntry> parse(File file) throws IOException {
if (!file.isFile()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ public Path getFile(Path path) {
if (link.type() == LinkDescription.FILE) {
//the path must actually match each others as it is a file!
if (linkPath.startsWith(relative)) {
Path resolvedPath = resolvePath(link.locationURI());
Path resolvedPath = resolvePath(link.locationURI(), this);
return location.resolve(resolvedPath).normalize();
}
} else if (link.type() == LinkDescription.FOLDER) {
Path linkRelative = linkPath.relativize(relative);
Path resolvedPath = resolvePath(link.locationURI());
Path resolvedPath = resolvePath(link.locationURI(), this);
if (resolvedPath != null) {
return location.resolve(resolvedPath).resolve(linkRelative).normalize();
}
Expand All @@ -173,7 +173,7 @@ public Collection<ProjectVariable> getVariables() {
}
}

private static Path resolvePath(URI uri) {
private static Path resolvePath(URI uri, EclipseProject project) {
String schemeSpecificPart = uri.getSchemeSpecificPart();
if (schemeSpecificPart != null) {
Path path = Path.of(schemeSpecificPart);
Expand All @@ -182,24 +182,31 @@ private static Path resolvePath(URI uri) {
//only the first path is allowed to be a variable...
Path first = path.getName(0);
String name = first.toString();
if ("PROJECT_LOC".equals(name)) {
return appendRemaining(path, count, project.getLocation());
}
Matcher parentMatcher = PARENT_PROJECT_PATTERN.matcher(name);
if (parentMatcher.matches()) {
Path resolvedPath = Path.of("..");
int p = Integer.parseInt(parentMatcher.group(1));
for (int i = 1; i < p; i++) {
resolvedPath = resolvedPath.resolve("..");
}
for (int i = 1; i < count; i++) {
resolvedPath = resolvedPath.resolve(path.getName(i));
}
return resolvedPath;
return appendRemaining(path, count, resolvedPath);
}
return path;
}
}
return null;
}

private static Path appendRemaining(Path path, int count, Path resolvedPath) {
for (int i = 1; i < count; i++) {
resolvedPath = resolvedPath.resolve(path.getName(i));
}
return resolvedPath;
}

private static ProjectVariable parseVariable(Element element) {
try {
String name = element.getElementsByTagName("name").item(0).getTextContent();
Expand Down

0 comments on commit 93b53da

Please sign in to comment.