Created
December 19, 2025 05:47
-
-
Save anjoismysign/3f65616d37a98a79a58706b400e83592 to your computer and use it in GitHub Desktop.
ProjectContentGatherer (MIT License)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class ProjectContentGatherer { | |
| public static String gather(String rootPathString, int maxDepth) { | |
| Path rootPath = Paths.get(rootPathString).toAbsolutePath().normalize(); | |
| // Same exclusion logic as the Tree Generator | |
| Predicate<Path> isExcluded = path -> { | |
| String name = path.getFileName().toString(); | |
| return name.equals("build") || | |
| name.equals("target") || | |
| name.equals(".git") || | |
| name.equals(".gradle") || | |
| name.equals(".idea") || | |
| name.endsWith(".iml") || | |
| name.endsWith(".DS_Store") || | |
| name.endsWith(".vscode"); | |
| }; | |
| StringBuilder builder = new StringBuilder(); | |
| gatherRecursive(rootPath, isExcluded, maxDepth, 0, rootPath, builder); | |
| String text = builder.toString(); | |
| return text.substring(0, text.length()-2); | |
| } | |
| private static void gatherRecursive( | |
| Path currentPath, | |
| Predicate<Path> isExcluded, | |
| int maxDepth, | |
| int currentDepth, | |
| Path rootPath, | |
| StringBuilder result | |
| ) { | |
| if (currentDepth >= maxDepth) { | |
| return; | |
| } | |
| try (Stream<Path> stream = Files.list(currentPath)) { | |
| // Sort entries so that contents are gathered in a predictable order (alphabetical) | |
| List<Path> entries = stream | |
| .filter(path -> !isExcluded.test(path)) | |
| .sorted(Comparator.comparing(Path::getFileName)) | |
| .collect(Collectors.toList()); | |
| for (Path entry : entries) { | |
| if (Files.isDirectory(entry)) { | |
| gatherRecursive(entry, isExcluded, maxDepth, currentDepth + 1, rootPath, result); | |
| } else if (Files.isRegularFile(entry)) { | |
| appendFileContent(entry, rootPath, result); | |
| } | |
| } | |
| } catch (IOException e) { | |
| // Skip unreadable directories | |
| } | |
| } | |
| private static void appendFileContent(Path file, Path rootPath, StringBuilder result) { | |
| try { | |
| // Get path relative to the root for the header comment | |
| String relativePath = rootPath.relativize(file).toString(); | |
| // Read file content | |
| String content = Files.readString(file); | |
| result.append("// file: ").append(relativePath).append("\n"); | |
| result.append("```\n").append(content).append("\n```\n\n"); | |
| } catch (IOException e) { | |
| // Skip files that cannot be read (e.g., binary files or permission issues) | |
| } | |
| } | |
| public static void main(String[] args) { | |
| // Gather all source code from the current directory | |
| String allContents = ProjectContentGatherer.gather(".", 15); | |
| System.out.println(allContents); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment