-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
381 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
AerialUpdater/Assets.xcassets/Status48Attention.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "[email protected]", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+4.31 KB
AerialUpdater/Assets.xcassets/Status48Attention.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// | ||
// LaunchAgent.swift | ||
// AerialUpdater | ||
// | ||
// Created by Guillaume Louel on 09/08/2020. | ||
// | ||
|
||
import Cocoa | ||
|
||
struct LaunchAgent { | ||
// com.glouel.AerialUpdaterAgent.plist | ||
static let agentPath: String = { | ||
let path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] as String | ||
let url = NSURL(fileURLWithPath: path) | ||
if let pathComponent = url.appendingPathComponent("LaunchAgents/com.glouel.AerialUpdaterAgent.plist") { | ||
return pathComponent.path | ||
} else { | ||
return "" | ||
} | ||
}() | ||
|
||
/// Update the Launch Agent to the current settings | ||
/// | ||
/// It will remove the Launch Agent first if any exists then install the correct one depending on the mode. In manual mode, nothing is installed. | ||
static public func update() { | ||
// Let's start by cleaning up | ||
removeAgent() | ||
|
||
// If we need to set something, do it | ||
if Preferences.launchMode != .manual { | ||
installAgent() | ||
} | ||
} | ||
|
||
static private func removeAgent() { | ||
if FileManager.default.fileExists(atPath: agentPath) { | ||
do { | ||
debugLog("Removing LaunchAgent") | ||
try FileManager().removeItem(atPath: agentPath) | ||
} catch { | ||
// ERROR | ||
errorLog("Cannot remove LaunchAgent") | ||
Helpers.showErrorAlert(question: "Cannot remove launch agent", text: "Please report !") | ||
return | ||
} | ||
} | ||
} | ||
|
||
static private func installAgent() { | ||
var agent: String = "" | ||
switch Preferences.launchMode { | ||
case .manual: | ||
errorLog("Calling installAgent in manual mode, please report") | ||
return | ||
case .startup: | ||
agent = getStartupAgent() | ||
case .background: | ||
agent = getBackgroundAgent() | ||
} | ||
|
||
do { | ||
try agent.write(toFile: agentPath, atomically: true, encoding: .utf8) | ||
debugLog("LaunchAgent installed") | ||
} catch { | ||
Helpers.showErrorAlert(question: "Can't install LaunchAgent", text: "Please report the issue") | ||
} | ||
|
||
restart() | ||
if Preferences.launchMode == .background { | ||
debugLog("Quitting after installing background mode") | ||
NSApplication.shared.terminate(nil) | ||
} | ||
} | ||
|
||
// Restarts our agent via launchctl | ||
// This may not work on very old macOS versions? | ||
static private func restart() { | ||
debugLog("Restarting LaunchAgent") | ||
let out1 = Helpers.shell(launchPath: "/bin/launchctl", arguments: ["unload", agentPath]) | ||
debugLog(out1 ?? "(no output)") | ||
let out2 = Helpers.shell(launchPath: "/bin/launchctl", arguments: ["load", agentPath]) | ||
debugLog(out2 ?? "(no output)") | ||
} | ||
|
||
static private func getStartupAgent() -> String { | ||
let top = | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>Label</key> | ||
<string>com.glouel.AerialUpdater</string> | ||
<key>LimitLoadToSessionType</key> | ||
<string>Aqua</string> | ||
<key>ProgramArguments</key> | ||
<array> | ||
<string>/usr/bin/open</string> | ||
""" | ||
|
||
let bundleLine = "<string>" + Bundle.main.bundlePath + "</string>" | ||
|
||
let bottom = | ||
""" | ||
</array> | ||
<key>RunAtLoad</key> | ||
<true/> | ||
</dict> | ||
</plist> | ||
""" | ||
return top + bundleLine + bottom | ||
} | ||
|
||
|
||
static private func getBackgroundAgent() -> String { | ||
let top = | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>Label</key> | ||
<string>com.glouel.AerialUpdater</string> | ||
<key>LimitLoadToSessionType</key> | ||
<string>Aqua</string> | ||
<key>ProgramArguments</key> | ||
<array> | ||
<string>/usr/bin/open</string> | ||
""" | ||
|
||
let bundleLine = "<string>" + Bundle.main.bundlePath + "</string>" | ||
|
||
let bottom = | ||
""" | ||
<string>--args</string> | ||
<string>--silent</string> | ||
</array> | ||
<key>RunAtLoad</key> | ||
<true/> | ||
<key>StartInterval</key> | ||
<integer> | ||
""" | ||
let bottom2 = | ||
""" | ||
</integer> | ||
</dict> | ||
</plist> | ||
""" | ||
return top + bundleLine + bottom + String(BackgroundCheck.instance.getTimer()) + bottom2 | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.