Skip to content

Commit

Permalink
refactor: improve writing launch-agent and its logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jan 24, 2025
1 parent dcea005 commit 69edbbd
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/ui/preferences-window/tabs/GeneralTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,29 @@ class GeneralTab {
sender.state = .on
LabelAndControl.controlWasChanged(sender, sender.identifier!.rawValue)
}
var launchAgentsPath = (try? FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)) ?? URL.init(fileURLWithPath: "~/Library", isDirectory: true)
do {
try writePlistToDisk(sender)
} catch let error {
Logger.error("Failed to write plist file to disk", error)
}
}

private static func writePlistToDisk(_ sender: Switch) throws {
var launchAgentsPath = (try? FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)) ?? URL(fileURLWithPath: "~/Library", isDirectory: true)
launchAgentsPath.appendPathComponent("LaunchAgents", isDirectory: true)
if !FileManager.default.fileExists(atPath: launchAgentsPath.path) {
do {
try FileManager.default.createDirectory(at: launchAgentsPath, withIntermediateDirectories: false)
} catch let error {
Logger.error("Failed to create LaunchAgent directory at '\(launchAgentsPath.path)'", error)
}
try FileManager.default.createDirectory(at: launchAgentsPath, withIntermediateDirectories: false)
Logger.debug(launchAgentsPath.absoluteString + " created")
}
launchAgentsPath.appendPathComponent("com.lwouis.alt-tab-macos.plist", isDirectory: false)
if sender.state == .on {
launchAgentPlist.write(to: launchAgentsPath, atomically: true)
let data = try PropertyListSerialization.data(fromPropertyList: launchAgentPlist, format: .xml, options: 0)
try data.write(to: launchAgentsPath, options: [.atomic])
Logger.debug(launchAgentsPath.absoluteString + " written")
} else {
if FileManager.default.fileExists(atPath: launchAgentsPath.path) {
do {
try FileManager.default.removeItem(at: launchAgentsPath)
Logger.debug(launchAgentsPath.absoluteString + " deleted")
} catch let error {
Logger.error("Failed to remove LaunchAgent", error)
}
try FileManager.default.removeItem(at: launchAgentsPath)
Logger.debug(launchAgentsPath.absoluteString + " removed")
}
}
}
Expand Down

0 comments on commit 69edbbd

Please sign in to comment.