fix(macos): create tunarr data directory on first run if not exists

This commit is contained in:
Christian Benincasa
2026-01-06 10:48:51 -05:00
parent 03469646c8
commit a5271b46be
2 changed files with 59 additions and 5 deletions

View File

@@ -11,18 +11,27 @@ import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
let bundle = Bundle.main 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() var task = Process()
func applicationDidFinishLaunching(_ notification: Notification) { func applicationDidFinishLaunching(_ notification: Notification) {
guard let executablePath = Bundle.main.url(forAuxiliaryExecutable: "tunarr-macos") guard
let executablePath = Bundle.main.url(
forAuxiliaryExecutable: "tunarr-macos"
)
else { else {
/// TODO: Do a popup here. /// TODO: Do a popup here.
logger.error("Error: Bundled executable 'tunarr-macos' not found.") logger.error("Error: Bundled executable 'tunarr-macos' not found.")
return return
} }
guard let meilisearchPath = Bundle.main.url(forAuxiliaryExecutable: "meilisearch") guard
let meilisearchPath = Bundle.main.url(
forAuxiliaryExecutable: "meilisearch"
)
else { else {
logger.error("Error: Bundled executable 'meilisearch' not found") logger.error("Error: Bundled executable 'meilisearch' not found")
return return
@@ -31,12 +40,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
task.executableURL = executablePath task.executableURL = executablePath
var env = ProcessInfo.processInfo.environment var env = ProcessInfo.processInfo.environment
env["TUNARR_MEILISEARCH_PATH"] = meilisearchPath.absoluteURL.path() env["TUNARR_MEILISEARCH_PATH"] = meilisearchPath.absoluteURL.path()
task.currentDirectoryURL = FileManager.default let tunarrDataDir = FileManager.default
.homeDirectoryForCurrentUser .homeDirectoryForCurrentUser
.appendingPathComponent( .appendingPathComponent(
"Library" "Library"
).appendingPathComponent("Preferences") ).appendingPathComponent("Preferences")
.appendingPathComponent("tunarr") .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 task.environment = env
logger.info("\(env, privacy: .public)") logger.info("\(env, privacy: .public)")
@@ -58,7 +75,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
logger.info("Successfully started Tunarr subprocess.") logger.info("Successfully started Tunarr subprocess.")
} catch { } 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.terminate()
task.waitUntilExit() 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
}
}
} }