Skip to content

Commit

Permalink
Merge pull request #130 from entunidotdeb/feature/DirectoryModeled
Browse files Browse the repository at this point in the history
directory modelling suggestion
  • Loading branch information
AvdLee authored Nov 3, 2022
2 parents ee7d32a + 105b104 commit 8861bd6
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions Sources/Reporters/DirectoryTree/DirectoryTreeReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,55 @@

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`.
/// - 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`.
@available(*, deprecated, message: "Use the new init(trunks: [Directory])")
public init(
directories: [URL],
maxDepth: Int = .max,
Expand All @@ -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("<h3>Directory Tree for: \(directoryName)</h3>")
let trunkName = trunk.customisedName ?? trunk.url.lastPathComponent
diagnostics.append("<h3>Directory Tree for: \(trunkName)</h3>")

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("""
Expand All @@ -65,7 +104,7 @@ public struct DirectoryTreesReporter: DiagnosticsReporting {
""")
diagnostics.append("""
<pre>
\(tree.prettyString(printFullPath: printFullPath))
\(tree.prettyString(printFullPath: trunk.printFullPath))
</pre>
""")
} catch {
Expand Down

0 comments on commit 8861bd6

Please sign in to comment.