11import Foundation
22
33public struct LaunchAgentManager {
4+ let lastLaunchAgentVersionKey = " LastLaunchAgentVersion "
45 let serviceIdentifier : String
56 let executablePath : String
67
@@ -10,7 +11,7 @@ public struct LaunchAgentManager {
1011 }
1112
1213 var launchAgentPath : String {
13- launchAgentDirURL. appendingPathComponent ( " \( serviceIdentifier) .1. plist " ) . path
14+ launchAgentDirURL. appendingPathComponent ( " \( serviceIdentifier) .plist " ) . path
1415 }
1516
1617 public init ( serviceIdentifier: String , executablePath: String ) {
@@ -19,9 +20,11 @@ public struct LaunchAgentManager {
1920 }
2021
2122 public func setupLaunchAgentForTheFirstTimeIfNeeded( ) async throws {
22- guard !FileManager. default. fileExists ( atPath: launchAgentPath) else {
23+ if UserDefaults . standard. integer ( forKey: lastLaunchAgentVersionKey) < 40 {
24+ try await setupLaunchAgent ( )
2325 return
2426 }
27+ guard !FileManager. default. fileExists ( atPath: launchAgentPath) else { return }
2528 try await setupLaunchAgent ( )
2629 await removeObsoleteLaunchAgent ( )
2730 }
@@ -54,6 +57,9 @@ public struct LaunchAgentManager {
5457 atPath: launchAgentPath,
5558 contents: content. data ( using: . utf8)
5659 )
60+ let buildNumber = ( Bundle . main. infoDictionary ? [ " CFBundleVersion " ] as? String )
61+ . flatMap ( Int . init)
62+ UserDefaults . standard. set ( buildNumber, forKey: lastLaunchAgentVersionKey)
5763 try await launchctl ( " load " , launchAgentPath)
5864 }
5965
@@ -130,4 +136,3 @@ private func launchctl(_ args: String...) async throws {
130136struct E : Error , LocalizedError {
131137 var errorDescription : String ?
132138}
133-
0 commit comments