Skip to content

Commit 78fba73

Browse files
committed
Checking if the Mail plug-in is installed in the correct version. Otherwise the new mail plug-in will be installed
1 parent aa7c0a5 commit 78fba73

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ extension FileManager {
3030
if isDir.boolValue == true {
3131
try self.copyFolder(from: fileURL, to: to.appendingPathComponent(file))
3232
} else {
33-
// Copy file
34-
try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file))
33+
do {
34+
// Copy file
35+
try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file))
36+
} catch {
37+
if fileURL.lastPathComponent != "CodeResources" {
38+
throw error
39+
}
40+
}
3541
}
3642
}
3743
}

OpenHaystack/OpenHaystack/HaystackApp/Mail Plugin/MailPluginManager.swift

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)