Skip to content

Commit

Permalink
fix: detect packages in multi-project repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
zermelo-wisen committed Aug 28, 2024
1 parent 155da18 commit 26a8b02
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
71 changes: 48 additions & 23 deletions agent/src/main/java/com/appland/appmap/config/AppMapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import org.tinylog.Logger;
Expand Down Expand Up @@ -224,31 +225,29 @@ public static String getDefault(String directory) throws IOException {
pw.println("# https://appmap.io/docs/reference/appmap-java.html#configuration");
pw.format("name: %s\n", CLI.projectName(new File(directory)));

// For now, this only works in this type of standardize repo structure.
Path javaDir = Paths.get(directory).resolve("src/main/java");
if (Files.isDirectory(javaDir)) {
int pkgStart = javaDir.getNameCount();
// Collect package names in src/main/java
Set<Path> packages = new HashSet<>();
Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(".java")) {
int pkgEnd = file.getParent().getNameCount();
if (pkgStart == pkgEnd) {
// We're in the the unnamed package, ignore
return FileVisitResult.CONTINUE;
}

Path packagePath = file.getParent().subpath(pkgStart, pkgEnd);
if (packagePath.getNameCount() > 0) {
packages.add(packagePath);
}
}
return FileVisitResult.CONTINUE;
// Set to collect packages from all relevant src/main/java directories
Set<Path> packages = new HashSet<>();
AtomicBoolean srcMainJavaDirExists = new AtomicBoolean(false);

// Traverse the root directory to find all src/main/java directories
Files.walkFileTree(Paths.get(directory), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (dir.endsWith("src/main/java")) {
srcMainJavaDirExists.set(true);
collectPackages(dir, packages);
}
});
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException io)
{
return FileVisitResult.SKIP_SUBTREE;
}
});

if (srcMainJavaDirExists.get()) {
pw.print("\n"
+ "# Your project contains the directory src/main/java. AppMap has\n"
+ "# auto-detected the following Java packages in this directory:\n"
Expand Down Expand Up @@ -288,6 +287,32 @@ public void accept(Path packagePath) {
return sw.toString();
}

private static void collectPackages(Path javaDir, Set<Path> packages) {
int pkgStart = javaDir.getNameCount();
try {
Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(".java")) {
int pkgEnd = file.getParent().getNameCount();
if (pkgStart == pkgEnd) {
// We're in the unnamed package, ignore
return FileVisitResult.CONTINUE;
}

Path packagePath = file.getParent().subpath(pkgStart, pkgEnd);
if (packagePath.getNameCount() > 0) {
packages.add(packagePath);
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}

public static TaggedLogger configureLogging() {
// tinylog freezes its configuration after the first call to any of its
// methods other than those in Configuration. So, get everything ready
Expand Down
1 change: 1 addition & 0 deletions agent/test/agent_cli/agent_cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ setup_file() {
assert_success
assert_json_contains '.configuration.contents' 'path: pkg1'
assert_json_contains '.configuration.contents' 'path: pkg2'
assert_json_contains '.configuration.contents' 'path: subproj'
}

@test "appmap agent init empty project" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// intentionally blank

0 comments on commit 26a8b02

Please sign in to comment.