-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
148 additions
and
15 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
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 |
---|---|---|
@@ -1,12 +1,5 @@ | ||
<?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>com.apple.developer.icloud-container-identifiers</key> | ||
<array/> | ||
<key>com.apple.security.app-sandbox</key> | ||
<true/> | ||
<key>com.apple.security.files.user-selected.read-write</key> | ||
<true/> | ||
</dict> | ||
<dict/> | ||
</plist> |
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
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,107 @@ | ||
// | ||
// Loader.swift | ||
// Clippr | ||
// | ||
// Created by Viktor Radulov on 1/15/21. | ||
// Copyright © 2021 Viktor Radulov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LaunchctlRegistry: Codable { | ||
|
||
enum ProcessType: String, Codable { | ||
case background = "Background" | ||
case standard = "Standard" | ||
case adaptive = "Adaptive" | ||
case interactive = "Interactive" | ||
} | ||
|
||
enum SessionType: String, Codable { | ||
case background = "Background" | ||
case aqua = "Aqua" | ||
case loginWindow = "LoginWindow" | ||
} | ||
|
||
static let agentsPath = "/Library/LaunchAgents/" | ||
|
||
var disabled: Bool? | ||
var launchOnlyOnce: Bool? | ||
var keepAlive = true | ||
var sessionType: SessionType? | ||
let label: String | ||
let program: String | ||
var processType: ProcessType? | ||
var machServices: [String: Bool]? | ||
var runAtLoad: Bool? | ||
var arguments: [String]? | ||
|
||
var defaultPlistName: String { | ||
return label + ".plist" | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
var rawValue: String { | ||
switch self { | ||
case .disabled: return LAUNCH_JOBKEY_DISABLED | ||
case .keepAlive: return LAUNCH_JOBKEY_KEEPALIVE | ||
case .sessionType: return LAUNCH_JOBKEY_LIMITLOADTOSESSIONTYPE | ||
case .label: return LAUNCH_JOBKEY_LABEL | ||
case .program: return LAUNCH_JOBKEY_PROGRAM | ||
case .processType: return LAUNCH_JOBKEY_PROCESSTYPE | ||
case .machServices: return LAUNCH_JOBKEY_MACHSERVICES | ||
case .runAtLoad: return LAUNCH_JOBKEY_RUNATLOAD | ||
case .arguments: return LAUNCH_JOBKEY_PROGRAMARGUMENTS | ||
case .launchOnlyOnce: return LAUNCH_JOBKEY_LAUNCHONLYONCE | ||
} | ||
} | ||
|
||
case disabled | ||
case keepAlive | ||
case sessionType | ||
case label | ||
case program | ||
case processType | ||
case machServices | ||
case runAtLoad | ||
case arguments | ||
case launchOnlyOnce | ||
} | ||
|
||
init?(bundle: Bundle) { | ||
guard let bundleIdentifier = bundle.bundleIdentifier, | ||
let executablePath = bundle.executablePath else { return nil } | ||
self.init(label: bundleIdentifier, program: executablePath) | ||
} | ||
|
||
init(label: String, program: String) { | ||
self.label = label | ||
self.program = program | ||
} | ||
} | ||
|
||
class Loader { | ||
static let appBundle = Bundle.main | ||
static let bundleIdentifier = appBundle.bundleIdentifier! | ||
static let pathToLaunchPlist = NSHomeDirectory() + LaunchctlRegistry.agentsPath + bundleIdentifier + ".plist" | ||
|
||
static func unloadAgent() { | ||
let fileManager = FileManager.default | ||
if fileManager.fileExists(atPath: pathToLaunchPlist) { | ||
Process.launchedProcess(launchPath: "/bin/launchctl", arguments: ["unload", pathToLaunchPlist]).waitUntilExit() | ||
try? fileManager.removeItem(atPath: pathToLaunchPlist) | ||
} | ||
} | ||
|
||
static func loadAgent() { | ||
var registry = LaunchctlRegistry(bundle: appBundle)! | ||
registry.processType = .interactive | ||
registry.sessionType = .aqua | ||
registry.disabled = false | ||
registry.launchOnlyOnce = true | ||
registry.arguments = [appBundle.executablePath!, "fromLaunchd"] | ||
try? FileManager.default.createDirectory(atPath: NSHomeDirectory() + LaunchctlRegistry.agentsPath, withIntermediateDirectories: false, attributes: [:]) | ||
try! (try! PropertyListEncoder().encode(registry)).write(to: URL(fileURLWithPath: pathToLaunchPlist)) | ||
Process.launchedProcess(launchPath: "/bin/launchctl", arguments: ["load", pathToLaunchPlist]) | ||
} | ||
} |