diff --git a/macos/Tunarr/Tunarr.xcodeproj/project.xcworkspace/xcuserdata/cbeni.xcuserdatad/UserInterfaceState.xcuserstate b/macos/Tunarr/Tunarr.xcodeproj/project.xcworkspace/xcuserdata/cbeni.xcuserdatad/UserInterfaceState.xcuserstate index 68fc75cf..6421e2c8 100644 Binary files a/macos/Tunarr/Tunarr.xcodeproj/project.xcworkspace/xcuserdata/cbeni.xcuserdatad/UserInterfaceState.xcuserstate and b/macos/Tunarr/Tunarr.xcodeproj/project.xcworkspace/xcuserdata/cbeni.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/macos/Tunarr/Tunarr/AppDelegate.swift b/macos/Tunarr/Tunarr/AppDelegate.swift index daf4f61a..97203c07 100644 --- a/macos/Tunarr/Tunarr/AppDelegate.swift +++ b/macos/Tunarr/Tunarr/AppDelegate.swift @@ -11,18 +11,27 @@ import SwiftUI class AppDelegate: NSObject, NSApplicationDelegate { let bundle = Bundle.main - let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "Tunarr Subprocess") + let logger = Logger( + subsystem: Bundle.main.bundleIdentifier!, + category: "Tunarr Subprocess" + ) var task = Process() func applicationDidFinishLaunching(_ notification: Notification) { - guard let executablePath = Bundle.main.url(forAuxiliaryExecutable: "tunarr-macos") + guard + let executablePath = Bundle.main.url( + forAuxiliaryExecutable: "tunarr-macos" + ) else { /// TODO: Do a popup here. logger.error("Error: Bundled executable 'tunarr-macos' not found.") return } - guard let meilisearchPath = Bundle.main.url(forAuxiliaryExecutable: "meilisearch") + guard + let meilisearchPath = Bundle.main.url( + forAuxiliaryExecutable: "meilisearch" + ) else { logger.error("Error: Bundled executable 'meilisearch' not found") return @@ -31,12 +40,20 @@ class AppDelegate: NSObject, NSApplicationDelegate { task.executableURL = executablePath var env = ProcessInfo.processInfo.environment env["TUNARR_MEILISEARCH_PATH"] = meilisearchPath.absoluteURL.path() - task.currentDirectoryURL = FileManager.default + let tunarrDataDir = FileManager.default .homeDirectoryForCurrentUser .appendingPathComponent( "Library" ).appendingPathComponent("Preferences") .appendingPathComponent("tunarr") + + let dirCreateResult = createDirectoryIfNeeded(path: tunarrDataDir) + if !dirCreateResult { + logger.error("Failure creating or location Tunarr data directory.") + return + } + + task.currentDirectoryURL = tunarrDataDir task.environment = env logger.info("\(env, privacy: .public)") @@ -58,7 +75,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { logger.info("Successfully started Tunarr subprocess.") } catch { - logger.error("Could not launch Tunarr. Reason: \(error, privacy: .public)") + logger.error( + "Could not launch Tunarr. Reason: \(error, privacy: .public)" + ) } } @@ -67,4 +86,39 @@ class AppDelegate: NSObject, NSApplicationDelegate { task.terminate() task.waitUntilExit() } + + func createDirectoryIfNeeded(path: URL) -> Bool { + let fileManager = FileManager.default + var isDirectory: ObjCBool = false + if fileManager.fileExists( + atPath: path.path(), + isDirectory: &isDirectory + ) { + if isDirectory.boolValue { + return true + } else { + logger.error( + "Expected a directory at \(path) but found a file. Delete this file and create a directory with the same name" + ) + return false + } + } + + do { + try fileManager.createDirectory( + atPath: path.path(), + withIntermediateDirectories: true, + attributes: nil + ) + logger.debug( + "Successfully created Tunarr data directory at \(path.path())" + ) + return true + } catch { + logger.error( + "Error creating Tunarr data directory at \(path.path()): \(error.localizedDescription)" + ) + return false + } + } }