From 8bbf489146f72e6d2911fa67ad93290f055427a5 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Sat, 4 Apr 2020 15:16:31 -0700 Subject: [PATCH] Relaunch agent if updated (#86) --- Secretive.xcodeproj/project.pbxproj | 8 +++++ Secretive/AppDelegate.swift | 8 +++++ .../Controllers/JustUpdatedChecker.swift | 36 +++++++++++++++++++ .../Controllers/LaunchAgentController.swift | 19 ++++++++++ Secretive/Views/SetupView.swift | 3 +- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Secretive/Controllers/JustUpdatedChecker.swift create mode 100644 Secretive/Controllers/LaunchAgentController.swift diff --git a/Secretive.xcodeproj/project.pbxproj b/Secretive.xcodeproj/project.pbxproj index dd04dae..14190a1 100644 --- a/Secretive.xcodeproj/project.pbxproj +++ b/Secretive.xcodeproj/project.pbxproj @@ -10,6 +10,8 @@ 50020BB024064869003D4025 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50020BAF24064869003D4025 /* AppDelegate.swift */; }; 5018F54F24064786002EB505 /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5018F54E24064786002EB505 /* Notifier.swift */; }; 50524B442420969E008DBD97 /* OpenSSHWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50524B432420969D008DBD97 /* OpenSSHWriterTests.swift */; }; + 50571E0324393C2600F76F6C /* JustUpdatedChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50571E0224393C2600F76F6C /* JustUpdatedChecker.swift */; }; + 50571E0524393D1500F76F6C /* LaunchAgentController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50571E0424393D1500F76F6C /* LaunchAgentController.swift */; }; 50617D8323FCE48E0099B055 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50617D8223FCE48E0099B055 /* AppDelegate.swift */; }; 50617D8523FCE48E0099B055 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50617D8423FCE48E0099B055 /* ContentView.swift */; }; 50617D8723FCE48E0099B055 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 50617D8623FCE48E0099B055 /* Assets.xcassets */; }; @@ -206,6 +208,8 @@ 50020BAF24064869003D4025 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 5018F54E24064786002EB505 /* Notifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifier.swift; sourceTree = ""; }; 50524B432420969D008DBD97 /* OpenSSHWriterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenSSHWriterTests.swift; sourceTree = ""; }; + 50571E0224393C2600F76F6C /* JustUpdatedChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustUpdatedChecker.swift; sourceTree = ""; }; + 50571E0424393D1500F76F6C /* LaunchAgentController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchAgentController.swift; sourceTree = ""; }; 50617D7F23FCE48E0099B055 /* Secretive.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Secretive.app; sourceTree = BUILT_PRODUCTS_DIR; }; 50617D8223FCE48E0099B055 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 50617D8423FCE48E0099B055 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -512,6 +516,8 @@ isa = PBXGroup; children = ( 508A58B2241ED2180069DC07 /* AgentStatusChecker.swift */, + 50571E0224393C2600F76F6C /* JustUpdatedChecker.swift */, + 50571E0424393D1500F76F6C /* LaunchAgentController.swift */, ); path = Controllers; sourceTree = ""; @@ -916,10 +922,12 @@ files = ( 50C385A9240B636500AF2719 /* SetupView.swift in Sources */, 50617D8523FCE48E0099B055 /* ContentView.swift in Sources */, + 50571E0324393C2600F76F6C /* JustUpdatedChecker.swift in Sources */, 50617DD223FCEFA90099B055 /* PreviewStore.swift in Sources */, 508A58B3241ED2180069DC07 /* AgentStatusChecker.swift in Sources */, 50C385A52407A76D00AF2719 /* SecretDetailView.swift in Sources */, 5099A02423FD2AAA0062B6F2 /* CreateSecretView.swift in Sources */, + 50571E0524393D1500F76F6C /* LaunchAgentController.swift in Sources */, 50B8550D24138C4F009958AC /* DeleteSecretView.swift in Sources */, 50BB046B2418AAAE00D6E079 /* EmptyStoreView.swift in Sources */, 50731669241E00C20023809E /* NoticeView.swift in Sources */, diff --git a/Secretive/AppDelegate.swift b/Secretive/AppDelegate.swift index d2e26aa..c9e116d 100644 --- a/Secretive/AppDelegate.swift +++ b/Secretive/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { }() let updater = Updater() let agentStatusChecker = AgentStatusChecker() + let justUpdatedChecker = JustUpdatedChecker() func applicationDidFinishLaunching(_ aNotification: Notification) { let contentView = ContentView(storeList: storeList, updater: updater, agentStatusChecker: agentStatusChecker, runSetupBlock: { self.runSetup(sender: nil) }) @@ -40,6 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { newMenuItem.isEnabled = true } runSetupIfNeeded() + relaunchAgentIfNeeded() } func applicationDidBecomeActive(_ notification: Notification) { @@ -89,6 +91,12 @@ extension AppDelegate { } } + func relaunchAgentIfNeeded() { + if agentStatusChecker.running && justUpdatedChecker.justUpdated { + LaunchAgentController().relaunch() + } + } + } extension AppDelegate { diff --git a/Secretive/Controllers/JustUpdatedChecker.swift b/Secretive/Controllers/JustUpdatedChecker.swift new file mode 100644 index 0000000..95a7ed3 --- /dev/null +++ b/Secretive/Controllers/JustUpdatedChecker.swift @@ -0,0 +1,36 @@ +import Foundation +import Combine +import AppKit + +protocol JustUpdatedCheckerProtocol: ObservableObject { + var justUpdated: Bool { get } +} + +class JustUpdatedChecker: ObservableObject, JustUpdatedCheckerProtocol { + + @Published var justUpdated: Bool = false + + init() { + check() + } + + func check() { + let lastBuild = UserDefaults.standard.object(forKey: Constants.previousVersionUserDefaultsKey) as? String ?? "None" + let currentBuild = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String + UserDefaults.standard.set(currentBuild, forKey: Constants.previousVersionUserDefaultsKey) + if lastBuild != currentBuild { + justUpdated = true + } + } + + + +} + +extension JustUpdatedChecker { + + enum Constants { + static let previousVersionUserDefaultsKey = "com.maxgoedjen.Secretive.lastBuild" + } + +} diff --git a/Secretive/Controllers/LaunchAgentController.swift b/Secretive/Controllers/LaunchAgentController.swift new file mode 100644 index 0000000..8077da5 --- /dev/null +++ b/Secretive/Controllers/LaunchAgentController.swift @@ -0,0 +1,19 @@ +import Foundation +import ServiceManagement + +struct LaunchAgentController { + + func install() -> Bool { + setEnabled(true) + } + + func relaunch() { + _ = setEnabled(false) + _ = setEnabled(true) + } + + private func setEnabled(_ enabled: Bool) -> Bool { + SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, enabled) + } + +} diff --git a/Secretive/Views/SetupView.swift b/Secretive/Views/SetupView.swift index c9f55a1..b475d77 100644 --- a/Secretive/Views/SetupView.swift +++ b/Secretive/Views/SetupView.swift @@ -1,6 +1,5 @@ import Foundation import SwiftUI -import ServiceManagement struct SetupView: View { @@ -117,7 +116,7 @@ struct SetupStepCommandView: View { extension SetupView { func installLaunchAgent() -> Bool { - SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, true) + LaunchAgentController().install() } func markAsDone() -> Bool {