|
| 1 | +/* |
| 2 | + * id-updater |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +import Foundation |
| 21 | + |
| 22 | +@main |
| 23 | +class Updater: Update, UpdateDelegate, @unchecked Sendable { |
| 24 | + private let path: String |
| 25 | + |
| 26 | + init(path: String) { |
| 27 | + self.path = path |
| 28 | + super.init() |
| 29 | + delegate = self |
| 30 | + print("Installed \(path): \(self.baseversion ?? "")") |
| 31 | + request() |
| 32 | + } |
| 33 | + |
| 34 | + static func launch(_ path: String, arguments: [String]) -> Int32 { |
| 35 | + let task = Process() |
| 36 | + task.launchPath = path |
| 37 | + task.arguments = arguments |
| 38 | + task.launch() |
| 39 | + task.waitUntilExit() |
| 40 | + return task.terminationStatus |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Update Delegate |
| 44 | + |
| 45 | + func didFinish(_ error: Error?) { |
| 46 | + if let error = error { |
| 47 | + print("Error: \(error.localizedDescription)") |
| 48 | + } |
| 49 | + exit(0) |
| 50 | + } |
| 51 | + |
| 52 | + func message(_ message: String) { |
| 53 | + print(message) |
| 54 | + _ = Updater.launch("/usr/bin/open", arguments: [path]) |
| 55 | + } |
| 56 | + |
| 57 | + func updateAvailable(_ available: String, filename: String) { |
| 58 | + print("Update available \(available) \(filename)") |
| 59 | + _ = Updater.launch("/usr/bin/open", arguments: [path]) |
| 60 | + } |
| 61 | + |
| 62 | + static func main() { |
| 63 | + let arguments = CommandLine.arguments |
| 64 | + guard arguments.count == 2 else { |
| 65 | + print("Usage: \(arguments[0]) [-task | -remove | -daily | -weekly | -monthly]") |
| 66 | + exit(1) |
| 67 | + } |
| 68 | + |
| 69 | + let directoryPath = ("~/Library/LaunchAgents" as NSString).expandingTildeInPath |
| 70 | + let PATH = directoryPath + "/ee.ria.id-updater.plist" |
| 71 | + let components = Calendar.current.dateComponents([.hour, .minute, .weekday, .day], from: Date()) |
| 72 | + let schedule: [String: Any] |
| 73 | + |
| 74 | + switch arguments[1] { |
| 75 | + case "-task": |
| 76 | + _ = Updater(path: NSString(string: "\(Bundle.main.executablePath ?? arguments[0])/../../..").standardizingPath) |
| 77 | + return RunLoop.main.run() |
| 78 | + case "-remove": |
| 79 | + let result = Updater.launch("/bin/launchctl", arguments: ["unload", "-w", PATH]) |
| 80 | + try? FileManager.default.removeItem(atPath: PATH) |
| 81 | + exit(result) |
| 82 | + case "-daily": |
| 83 | + schedule = ["Hour": components.hour!, "Minute": components.minute!] |
| 84 | + case "-weekly": |
| 85 | + schedule = ["Hour": components.hour!, "Minute": components.minute!, "Weekday": components.weekday!] |
| 86 | + case "-monthly": |
| 87 | + schedule = ["Hour": components.hour!, "Minute": components.minute!, "Day": components.day!] |
| 88 | + default: |
| 89 | + print("Invalid argument: \(arguments[1])") |
| 90 | + exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + let settings: [String: Any] = [ |
| 94 | + "Label": "ee.ria.id-updater", |
| 95 | + "ProgramArguments": [arguments[0], "-task"], |
| 96 | + "StartCalendarInterval": schedule |
| 97 | + ] |
| 98 | + let plistData = try? PropertyListSerialization.data(fromPropertyList: settings, format: .xml, options: 0) |
| 99 | + try? FileManager.default.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil) |
| 100 | + FileManager.default.createFile(atPath: PATH, contents: plistData, attributes: nil) |
| 101 | + exit(Updater.launch("/bin/launchctl", arguments: ["load", "-w", PATH])) |
| 102 | + } |
| 103 | +} |
0 commit comments