Skip to content

Instantly share code, notes, and snippets.

@runningcode
Last active February 16, 2022 12:04
Show Gist options
  • Save runningcode/7b6050c6d3e639a89f61703f7fac34c3 to your computer and use it in GitHub Desktop.
Save runningcode/7b6050c6d3e639a89f61703f7fac34c3 to your computer and use it in GitHub Desktop.
Print build directory structure
tasks.withType(Jar).configureEach { task ->
doFirst {
buildScan.value("Jar doFirst ${task.name}", printDirectoryTree(buildDir.path))
}
}
/**
* Pretty print the directory tree and its file names.
*
* @param path
* @return
*/
static String printDirectoryTree(String path) {
File folder = new File(path);
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
int indent = 0;
StringBuilder sb = new StringBuilder();
printDirectoryTreeRecurse(folder, indent, sb);
return sb.toString();
}
private static void printDirectoryTreeRecurse(File folder, int indent, StringBuilder sb) {
if (folder != null && folder.isDirectory() && folder.listFiles() != null) {
sb.append(getIndentString(indent));
sb.append("+--");
sb.append(folder.getName());
sb.append("/");
sb.append("\n");
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
printDirectoryTreeRecurse(file, indent + 1, sb);
} else {
printFile(file, indent + 1, sb);
}
}
}
}
private static void printFile(File file, int indent, StringBuilder sb) {
sb.append(getIndentString(indent));
sb.append("+--");
if (file != null) {
sb.append(file.getName());
} else {
sb.append("null file name");
}
sb.append("\n");
}
private static String getIndentString(int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append("| ");
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment