@@ -20,8 +20,36 @@ struct MailPluginManager {
2020
2121 let pluginURL = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " Library/Mail/Bundles " ) . appendingPathComponent ( mailBundleName + " .mailbundle " )
2222
23+ let localPluginURL = Bundle . main. url ( forResource: mailBundleName, withExtension: " mailbundle " ) !
24+
2325 var isMailPluginInstalled : Bool {
24- return FileManager . default. fileExists ( atPath: pluginURL. path)
26+ //Check if the plug-in is compatible by comparing the IDs
27+ guard FileManager . default. fileExists ( atPath: pluginURL. path) else {
28+ return false
29+ }
30+
31+ let infoPlistURL = pluginURL. appendingPathComponent ( " Contents/Info.plist " )
32+ let localInfoPlistURL = localPluginURL. appendingPathComponent ( " Contents/Info.plist " )
33+
34+ guard let infoPlistData = try ? Data ( contentsOf: infoPlistURL) ,
35+ let infoPlistDict = try ? PropertyListSerialization . propertyList ( from: infoPlistData, options: [ ] , format: nil ) as? [ String : AnyHashable ] ,
36+ let localInfoPlistData = try ? Data ( contentsOf: localInfoPlistURL) ,
37+ let localInfoPlistDict = try ? PropertyListSerialization . propertyList ( from: localInfoPlistData, options: [ ] , format: nil ) as? [ String : AnyHashable ]
38+ else { return false }
39+
40+ //Compare the supported plug-ins
41+ let uuidEntries = localInfoPlistDict. keys. filter ( { $0. contains ( " PluginCompatibilityUUIDs " ) } )
42+ for uuidEntry in uuidEntries {
43+ guard let localEntry = localInfoPlistDict [ uuidEntry] as? [ String ] ,
44+ let installedEntry = infoPlistDict [ uuidEntry] as? [ String ]
45+ else { return false }
46+
47+ if localEntry != installedEntry {
48+ return false
49+ }
50+ }
51+
52+ return true
2553 }
2654
2755 /// Shows a NSSavePanel to install the mail plugin at the required place.
@@ -58,9 +86,12 @@ struct MailPluginManager {
5886 throw PluginError . permissionNotGranted
5987 }
6088
61- let localPluginURL = Bundle . main. url ( forResource: mailBundleName, withExtension: " mailbundle " ) !
62-
6389 do {
90+ //Remove old plug-ins first
91+ if FileManager . default. fileExists ( atPath: pluginURL. path) {
92+ try FileManager . default. removeItem ( at: pluginURL)
93+ }
94+
6495 try FileManager . default. createDirectory ( at: pluginsFolderURL, withIntermediateDirectories: true , attributes: nil )
6596 } catch {
6697 print ( error. localizedDescription)
0 commit comments