diff --git a/aftermath.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/aftermath.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index b5550be..f45d205 100644 --- a/aftermath.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/aftermath.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,7 +5,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/weichsel/ZIPFoundation", "state" : { - "revision" : "1b662e2e7a091710ad8a963263939984e2ebf527", + "revision" : "7254c74b49cec2cb81520523ba993c671f71b066", "version" : "0.9.14" } } diff --git a/aftermath/CaseFiles.swift b/aftermath/CaseFiles.swift index 0958bf5..fe16b88 100644 --- a/aftermath/CaseFiles.swift +++ b/aftermath/CaseFiles.swift @@ -54,20 +54,34 @@ struct CaseFiles { } } - static func MoveTemporaryCaseDir(outputDir: String, isAnalysis: Bool) { + static func MoveTemporaryCaseDir(outputLocation: String, isAnalysis: Bool) { + print("Checking for existence of output location") + + let fm = FileManager.default + let isDir = fm.isDirectoryThatExists(path: outputLocation) + guard isDir || fm.fileExists(atPath: outputLocation) else { + print("Output path is not a valid file or directory that exists") + return + } + print("Moving the aftermath directory from its temporary location. This may take some time. Please wait...") - - var localCaseDir: URL - - if isAnalysis { - localCaseDir = analysisCaseDir + + // Determine if we should look in /tmp or in the Aftermath case directory within /tmp + let localCaseDir = isAnalysis ? analysisCaseDir : caseDir + + let endPath: String + if isDir { + endPath = "\(outputLocation)/\(localCaseDir.lastPathComponent)" } else { - localCaseDir = caseDir + // Ensure that we end up with the correct (.zip) path extension + endPath = fm.deletingPathExtension(path: outputLocation) } + + // The zipped case directory should end up in the specified output location + let endURL = URL(fileURLWithPath: endPath) + let zippedURL = endURL.appendingPathExtension("zip") + do { - let endURL = URL(fileURLWithPath: "\(outputDir)/\(localCaseDir.lastPathComponent)") - let zippedURL = endURL.appendingPathExtension("zip") - try fm.zipItem(at: localCaseDir, to: endURL, shouldKeepParent: true, compressionMethod: .deflate) try fm.moveItem(at: endURL, to: zippedURL) print("Aftermath archive moved to \(zippedURL.path)") diff --git a/aftermath/Command.swift b/aftermath/Command.swift index de3508c..f75dab6 100644 --- a/aftermath/Command.swift +++ b/aftermath/Command.swift @@ -22,7 +22,7 @@ class Command { static var options: Options = [] static var analysisDir: String? = nil - static var outputDir: String = "/tmp" + static var outputLocation: String = "/tmp" static var collectDirs: [String] = [] static let version: String = "1.2.0" @@ -50,7 +50,7 @@ class Command { case "-o", "--output": if let index = args.firstIndex(of: arg) { Self.options.insert(.output) - Self.outputDir = args[index + 1] + Self.outputLocation = args[index + 1] } case "--analyze": if let index = args.firstIndex(of: arg) { @@ -97,7 +97,7 @@ class Command { mainModule.log("Analysis directory not provided") return } - guard isFileThatExists(path: dir) else { + guard FileManager.default.isFileThatExists(path: dir) else { mainModule.log("Analysis directory is not a valid directory that exists") return } @@ -113,14 +113,9 @@ class Command { } mainModule.log("Finished analysis module") - - guard isDirectoryThatExists(path: Self.outputDir) else { - mainModule.log("Output directory is not a valid directory that exists") - return - } // Move analysis directory to output direcotry - CaseFiles.MoveTemporaryCaseDir(outputDir: self.outputDir, isAnalysis: true) + CaseFiles.MoveTemporaryCaseDir(outputLocation: self.outputLocation, isAnalysis: true) // End Aftermath mainModule.log("Aftermath Finished") @@ -182,15 +177,9 @@ class Command { mainModule.log("Finished logging unified logs") mainModule.log("Finished running Aftermath collection") - - - guard isDirectoryThatExists(path: Self.outputDir) else { - mainModule.log("Output directory is not a valid directory that exists") - return - } // Copy from cache to output - CaseFiles.MoveTemporaryCaseDir(outputDir: self.outputDir, isAnalysis: false) + CaseFiles.MoveTemporaryCaseDir(outputLocation: self.outputLocation, isAnalysis: false) // End Aftermath mainModule.log("Aftermath Finished") @@ -218,20 +207,10 @@ class Command { exit(1) } - static func isDirectoryThatExists(path: String) -> Bool { - var isDir : ObjCBool = false - let pathExists = FileManager.default.fileExists(atPath: path, isDirectory:&isDir) - return pathExists && isDir.boolValue - } - - static func isFileThatExists(path: String) -> Bool { - let fileExists = FileManager.default.fileExists(atPath: path) - return fileExists - } - static func printHelp() { print("-o -> specify an output location for Aftermath results (defaults to /tmp)") - print(" usage: -o Users/user/Desktop") + print(" usage: -o Users/user/Desktop ") + print(" -o Users/user/Desktop/outputFile.zip ") print("--analyze -> Analyze the results of the Aftermath results") print(" usage: --analyze ") print("--collect-dirs -> specify locations of (space-separated) directories to dump those raw files") diff --git a/extensions/FileManager.swift b/extensions/FileManager.swift index d27724f..88f14b8 100644 --- a/extensions/FileManager.swift +++ b/extensions/FileManager.swift @@ -9,6 +9,24 @@ import Foundation public extension FileManager { + func isDirectoryThatExists(path: String) -> Bool { + var isDir : ObjCBool = false + let pathExists = self.fileExists(atPath: path, isDirectory:&isDir) + return pathExists && isDir.boolValue + } + + func isFileThatExists(path: String) -> Bool { + self.fileExists(atPath: path) + } + + func deletingPathExtension(path: String) -> String { + if #available(macOS 13.0, *) { + return URL(filePath: path).deletingPathExtension().absoluteString + } else { + return URL(fileURLWithPath: path).deletingPathExtension().absoluteString + } + } + @discardableResult class func delete(path: String) -> Error? { if (FileManager.default.fileExists(atPath: path)) {