Skip to content

Commit

Permalink
Checking if the Mail plug-in is installed in the correct version. Oth…
Browse files Browse the repository at this point in the history
…erwise the new mail plug-in will be installed
  • Loading branch information
Sn0wfreezeDev committed Aug 6, 2021
1 parent aa7c0a5 commit 78fba73
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
10 changes: 8 additions & 2 deletions OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ extension FileManager {
if isDir.boolValue == true {
try self.copyFolder(from: fileURL, to: to.appendingPathComponent(file))
} else {
// Copy file
try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file))
do {
// Copy file
try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file))
} catch {
if fileURL.lastPathComponent != "CodeResources" {
throw error
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,36 @@ struct MailPluginManager {

let pluginURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/Mail/Bundles").appendingPathComponent(mailBundleName + ".mailbundle")

let localPluginURL = Bundle.main.url(forResource: mailBundleName, withExtension: "mailbundle")!

var isMailPluginInstalled: Bool {
return FileManager.default.fileExists(atPath: pluginURL.path)
//Check if the plug-in is compatible by comparing the IDs
guard FileManager.default.fileExists(atPath: pluginURL.path) else {
return false
}

let infoPlistURL = pluginURL.appendingPathComponent("Contents/Info.plist")
let localInfoPlistURL = localPluginURL.appendingPathComponent("Contents/Info.plist")

guard let infoPlistData = try? Data(contentsOf: infoPlistURL),
let infoPlistDict = try? PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: AnyHashable],
let localInfoPlistData = try? Data(contentsOf: localInfoPlistURL),
let localInfoPlistDict = try? PropertyListSerialization.propertyList(from: localInfoPlistData, options: [], format: nil) as? [String: AnyHashable]
else { return false }

//Compare the supported plug-ins
let uuidEntries = localInfoPlistDict.keys.filter({ $0.contains("PluginCompatibilityUUIDs") })
for uuidEntry in uuidEntries {
guard let localEntry = localInfoPlistDict[uuidEntry] as? [String],
let installedEntry = infoPlistDict[uuidEntry] as? [String]
else { return false }

if localEntry != installedEntry {
return false
}
}

return true
}

/// Shows a NSSavePanel to install the mail plugin at the required place.
Expand Down Expand Up @@ -58,9 +86,12 @@ struct MailPluginManager {
throw PluginError.permissionNotGranted
}

let localPluginURL = Bundle.main.url(forResource: mailBundleName, withExtension: "mailbundle")!

do {
//Remove old plug-ins first
if FileManager.default.fileExists(atPath: pluginURL.path) {
try FileManager.default.removeItem(at: pluginURL)
}

try FileManager.default.createDirectory(at: pluginsFolderURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription)
Expand Down

0 comments on commit 78fba73

Please sign in to comment.