Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect packages in multi-project repositories #290

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void loadBadDirectory() throws Exception {
public void createDefault() throws IOException {
// Just verify that the file gets created when it should. The contents
// get verified elsewhere.
Path configFile = Paths.get(System.getProperty("java.io.tmpdir"), "appmap.yml");
Path configFile = tmpdir.resolve("appmap.yml");
Files.deleteIfExists(configFile);
AppMapConfig.load(configFile, false);
assertTrue(Files.exists(configFile));
Expand All @@ -55,7 +55,7 @@ public void preservesExisting() throws IOException {

@Test
public void requiresExisting() throws Exception {
Path configFile = Paths.get(System.getProperty("java.io.tmpdir"), "appmap.yml");
Path configFile = tmpdir.resolve("appmap.yml");
Files.deleteIfExists(configFile);

String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, true));
Expand Down
2 changes: 2 additions & 0 deletions agent/test/agent_cli/agent_cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ setup_file() {
assert_success
assert_json_contains '.configuration.contents' 'path: pkg1'
assert_json_contains '.configuration.contents' 'path: pkg2'
assert_json_contains '.configuration.contents' 'path: com.example.subprojpkg1'
assert_json_contains '.configuration.contents' 'path: com.example.subprojpkg2'
}

@test "appmap agent init empty project" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// intentionally blank
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// intentionally blank
1 change: 1 addition & 0 deletions agent/test/intellij/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appmap-intellij-plugin
Loading