Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active December 14, 2016 01:30
Show Gist options
  • Save mminer/e58ea8b6e5354d8b27a758296097bf0d to your computer and use it in GitHub Desktop.
Save mminer/e58ea8b6e5354d8b27a758296097bf0d to your computer and use it in GitHub Desktop.
Removes old Sparkle updates that weren't removed properly.
import Foundation
struct SparkleCleanup {
private static let oldDownloadAge: TimeInterval = 86400 // 24 hours
/// The location where Sparkle caches file downloads.
private static var cacheURL: URL {
guard let bundleIdentifier = Bundle.main.bundleIdentifier else {
fatalError("Unable to get bundle identifier.")
}
let path = "\(NSHomeDirectory())/Library/Caches/\(bundleIdentifier)/Sparkle"
return URL(fileURLWithPath: path)
}
/// Downloaded updates that Sparkle failed to trash.
private static var filesToRemove: [URL] {
let toRemove = try? FileManager.default.contentsOfDirectory(
at: cacheURL,
includingPropertiesForKeys: [URLResourceKey.creationDateKey],
options: [.skipsHiddenFiles]
).filter { url in
let resourceValues = try? (url as NSURL).resourceValues(forKeys: [URLResourceKey.creationDateKey])
guard let creationDate = resourceValues?[URLResourceKey.creationDateKey] as? NSDate else {
// Leave the file alone if it lacks a creation date.
return false
}
// Skip versions created in last 24 hours.
return creationDate.timeIntervalSinceNow < -oldDownloadAge
}
return toRemove ?? []
}
/// Removes old versions that are kept around for whatever reason.
static func removeOldVersions() {
// Run task in the background so that it doesn't hold up the program.
DispatchQueue.global(qos: .background).async {
for url in self.filesToRemove {
_ = try? FileManager.default.removeItem(at: url)
}
}
}
}
@mminer
Copy link
Author

mminer commented Dec 2, 2016

No longer necessary as of Sparkle v1.15.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment