Skip to content

Commit 164a09e

Browse files
committed
[Basic] Add dumpTree method to FileSystem
This method is similar to the unix program `tree`. Useful for debugging in memory filesystem.
1 parent d5e3361 commit 164a09e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Sources/Basic/FileSystem.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,3 +755,34 @@ public struct RerootedFileSystemView: FileSystem {
755755

756756
/// Public access to the local FS proxy.
757757
public var localFileSystem: FileSystem = LocalFileSystem()
758+
759+
extension FileSystem {
760+
/// Print the filesystem tree of the given path.
761+
///
762+
/// For debugging only.
763+
public func dumpTree(at path: AbsolutePath = .root) {
764+
print(".")
765+
do {
766+
try recurse(fs: self, path: path)
767+
} catch {
768+
print("\(error)")
769+
}
770+
}
771+
772+
/// Helper method to recurse and print the tree.
773+
private func recurse(fs: FileSystem, path: AbsolutePath, prefix: String = "") throws {
774+
let contents = try fs.getDirectoryContents(path)
775+
776+
for (idx, entry) in contents.enumerated() {
777+
let isLast = idx == contents.count - 1
778+
let line = prefix + (isLast ? "└── " : "├── ") + entry
779+
print(line)
780+
781+
let entryPath = path.appending(component: entry)
782+
if fs.isDirectory(entryPath) {
783+
let childPrefix = prefix + (isLast ? " " : "")
784+
try recurse(fs: fs, path: entryPath, prefix: String(childPrefix))
785+
}
786+
}
787+
}
788+
}

0 commit comments

Comments
 (0)