|
20 | 20 | import java.util.HashSet; |
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Set; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
23 | 24 | import java.util.function.Consumer; |
24 | 25 |
|
25 | 26 | import org.tinylog.Logger; |
@@ -224,31 +225,29 @@ public static String getDefault(String directory) throws IOException { |
224 | 225 | pw.println("# https://appmap.io/docs/reference/appmap-java.html#configuration"); |
225 | 226 | pw.format("name: %s\n", CLI.projectName(new File(directory))); |
226 | 227 |
|
227 | | - // For now, this only works in this type of standardize repo structure. |
228 | | - Path javaDir = Paths.get(directory).resolve("src/main/java"); |
229 | | - if (Files.isDirectory(javaDir)) { |
230 | | - int pkgStart = javaDir.getNameCount(); |
231 | | - // Collect package names in src/main/java |
232 | | - Set<Path> packages = new HashSet<>(); |
233 | | - Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() { |
234 | | - @Override |
235 | | - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
236 | | - if (file.getFileName().toString().endsWith(".java")) { |
237 | | - int pkgEnd = file.getParent().getNameCount(); |
238 | | - if (pkgStart == pkgEnd) { |
239 | | - // We're in the the unnamed package, ignore |
240 | | - return FileVisitResult.CONTINUE; |
241 | | - } |
242 | | - |
243 | | - Path packagePath = file.getParent().subpath(pkgStart, pkgEnd); |
244 | | - if (packagePath.getNameCount() > 0) { |
245 | | - packages.add(packagePath); |
246 | | - } |
247 | | - } |
248 | | - return FileVisitResult.CONTINUE; |
| 228 | + // Set to collect packages from all relevant src/main/java directories |
| 229 | + Set<Path> packages = new HashSet<>(); |
| 230 | + AtomicBoolean srcMainJavaDirExists = new AtomicBoolean(false); |
| 231 | + |
| 232 | + // Traverse the root directory to find all src/main/java directories |
| 233 | + Files.walkFileTree(Paths.get(directory), new SimpleFileVisitor<Path>() { |
| 234 | + @Override |
| 235 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
| 236 | + if (dir.endsWith("src/main/java")) { |
| 237 | + srcMainJavaDirExists.set(true); |
| 238 | + collectPackages(dir, packages); |
249 | 239 | } |
250 | | - }); |
| 240 | + return FileVisitResult.CONTINUE; |
| 241 | + } |
251 | 242 |
|
| 243 | + @Override |
| 244 | + public FileVisitResult visitFileFailed(Path file, IOException io) |
| 245 | + { |
| 246 | + return FileVisitResult.SKIP_SUBTREE; |
| 247 | + } |
| 248 | + }); |
| 249 | + |
| 250 | + if (srcMainJavaDirExists.get()) { |
252 | 251 | pw.print("\n" |
253 | 252 | + "# Your project contains the directory src/main/java. AppMap has\n" |
254 | 253 | + "# auto-detected the following Java packages in this directory:\n" |
@@ -288,6 +287,32 @@ public void accept(Path packagePath) { |
288 | 287 | return sw.toString(); |
289 | 288 | } |
290 | 289 |
|
| 290 | + private static void collectPackages(Path javaDir, Set<Path> packages) { |
| 291 | + int pkgStart = javaDir.getNameCount(); |
| 292 | + try { |
| 293 | + Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() { |
| 294 | + @Override |
| 295 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 296 | + if (file.getFileName().toString().endsWith(".java")) { |
| 297 | + int pkgEnd = file.getParent().getNameCount(); |
| 298 | + if (pkgStart == pkgEnd) { |
| 299 | + // We're in the unnamed package, ignore |
| 300 | + return FileVisitResult.CONTINUE; |
| 301 | + } |
| 302 | + |
| 303 | + Path packagePath = file.getParent().subpath(pkgStart, pkgEnd); |
| 304 | + if (packagePath.getNameCount() > 0) { |
| 305 | + packages.add(packagePath); |
| 306 | + } |
| 307 | + } |
| 308 | + return FileVisitResult.CONTINUE; |
| 309 | + } |
| 310 | + }); |
| 311 | + } catch (IOException e) { |
| 312 | + e.printStackTrace(); |
| 313 | + } |
| 314 | +} |
| 315 | + |
291 | 316 | public static TaggedLogger configureLogging() { |
292 | 317 | // tinylog freezes its configuration after the first call to any of its |
293 | 318 | // methods other than those in Configuration. So, get everything ready |
|
0 commit comments