From 105b10469e4d727460c5df9b9588ea3b09ace26b Mon Sep 17 00:00:00 2001 From: Raghav Kundra Date: Sat, 13 Aug 2022 22:37:37 +0530 Subject: [PATCH] feature: proposed directory modelling with custom name & properties --- .../DirectoryTree/DirectoryTreeReporter.swift | 77 ++++++++++++++----- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/Sources/Reporters/DirectoryTree/DirectoryTreeReporter.swift b/Sources/Reporters/DirectoryTree/DirectoryTreeReporter.swift index b4a92f1..2b87d63 100644 --- a/Sources/Reporters/DirectoryTree/DirectoryTreeReporter.swift +++ b/Sources/Reporters/DirectoryTree/DirectoryTreeReporter.swift @@ -7,17 +7,47 @@ import Foundation -/// Reports a directory tree structure for the given directory urls. -public struct DirectoryTreesReporter: DiagnosticsReporting { - let title: String = "Directory Storage Trees" - let directories: [URL] +public struct Directory { + let url: URL + let customisedName: String? let maxDepth: Int let maxLength: Int let includeHiddenFiles: Bool let includeSymbolicLinks: Bool let printFullPath: Bool + + /// Directory/Group to be diagnosed + /// - Parameters: + /// - url: location of resource + /// - customisedName: name to use in report instead of unique string url. + /// - maxDepth: The max depth of directories to visit. Defaults to `.max`. + /// - maxLength: The max length of nodes in a directory to handle. Defaults to `10`. + /// - includeHiddenFiles: Whether hidden files should be captured. Defaults to `false`. + /// - includeSymbolicLinks: Whether symbolic links should be captured. Defaults to `false`. + /// - printFullPath: Whether the full path of the node should be printed or just the name. Defaults to `false`. + public init(url: URL, + customisedName: String? = nil, + maxDepth: Int = .max, + maxLength: Int = 10, + includeHiddenFiles: Bool = false, + includeSymbolicLinks: Bool = false, + printFullPath: Bool = false) { + self.url = url + self.customisedName = customisedName + self.maxDepth = maxDepth + self.maxLength = maxLength + self.includeHiddenFiles = includeHiddenFiles + self.includeSymbolicLinks = includeSymbolicLinks + self.printFullPath = printFullPath + } +} + +/// Reports a directory tree structure for the given directory urls. +public struct DirectoryTreesReporter: DiagnosticsReporting { + let trunks: [Directory] /// Creates a new reporter for directory trees. + /// eventually uses the new init(trunk: [Directory]) /// - Parameters: /// - directories: The directories to create trees for. /// - maxDepth: The max depth of directories to visit. Defaults to `.max`. @@ -25,6 +55,7 @@ public struct DirectoryTreesReporter: DiagnosticsReporting { /// - includeHiddenFiles: Whether hidden files should be captured. Defaults to `false`. /// - includeSymbolicLinks: Whether symbolic links should be captured. Defaults to `false`. /// - printFullPath: Whether the full path of the node should be printed or just the name. Defaults to `false`. + @available(*, deprecated, message: "Use the new init(trunks: [Directory])") public init( directories: [URL], maxDepth: Int = .max, @@ -33,28 +64,36 @@ public struct DirectoryTreesReporter: DiagnosticsReporting { includeSymbolicLinks: Bool = false, printFullPath: Bool = false ) { - self.directories = directories - self.maxDepth = maxDepth - self.maxLength = maxLength - self.includeHiddenFiles = includeHiddenFiles - self.includeSymbolicLinks = includeSymbolicLinks - self.printFullPath = printFullPath + var newTrunks: [Directory] = [] + directories.forEach { directoryURL in + newTrunks.append(Directory(url: directoryURL, + maxDepth: maxDepth, + maxLength: maxLength, + includeHiddenFiles: includeHiddenFiles, + includeSymbolicLinks: includeSymbolicLinks, + printFullPath: printFullPath)) + } + self.init(trunks: newTrunks) + } + + public init(trunks: [Directory]) { + self.trunks = trunks } public func report() -> DiagnosticsChapter { var diagnostics = "" - for directory in directories { + for trunk in trunks { do { - let directoryName = directory.lastPathComponent - diagnostics.append("

Directory Tree for: \(directoryName)

") + let trunkName = trunk.customisedName ?? trunk.url.lastPathComponent + diagnostics.append("

Directory Tree for: \(trunkName)

") let tree = try DirectoryTreeFactory( - path: directory.path, - maxDepth: maxDepth, - maxLength: maxLength, - includeHiddenFiles: includeHiddenFiles, - includeSymbolicLinks: includeSymbolicLinks + path: trunk.url.path, + maxDepth: trunk.maxDepth, + maxLength: trunk.maxLength, + includeHiddenFiles: trunk.includeHiddenFiles, + includeSymbolicLinks: trunk.includeSymbolicLinks ).make() diagnostics.append(""" @@ -65,7 +104,7 @@ public struct DirectoryTreesReporter: DiagnosticsReporting { """) diagnostics.append("""
-                \(tree.prettyString(printFullPath: printFullPath))
+                \(tree.prettyString(printFullPath: trunk.printFullPath))
                 
""") } catch {