Repository navigation
Add Check for Updates… to the app menu #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e379bef
Add Check for Updates… to the app menu
AnasLakhani 0b1c627
Check the HTTP status and the tag before comparing versions
AnasLakhani 27bcae8
Merge remote-tracking branch 'origin/main' into tmp-99
omlahore 49a7f39
Merge remote-tracking branch 'origin/main' into tmp-99
omlahore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import Foundation | ||
|
|
||
| /// The newest release on GitHub. Only asked when someone chooses Check for | ||
| /// Updates, so RemoveMacAI otherwise makes no network requests. | ||
| enum Updates { | ||
| static let latestURL = URL(string: "https://api.github.com/repos/omlahore/RemoveMacAI/releases/latest")! | ||
| static let releasesPage = URL(string: "https://github.com/omlahore/RemoveMacAI/releases/latest")! | ||
|
|
||
| struct Release { | ||
| let version: String | ||
| let page: URL | ||
| } | ||
|
|
||
| /// The latest release that isn't a beta, or why it couldn't be read. | ||
| static func latest() async -> Result<Release, Error> { | ||
| var request = URLRequest(url: latestURL, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 15) | ||
| request.setValue("application/vnd.github+json", forHTTPHeaderField: "Accept") | ||
| do { | ||
| let (data, response) = try await URLSession.shared.data(for: request) | ||
| // A rate limit or outage still sends a body, so the status comes first. | ||
| if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) { | ||
| throw UpdateError.status(http.statusCode) | ||
| } | ||
| guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], | ||
| let tag = json["tag_name"] as? String | ||
| else { throw URLError(.cannotParseResponse) } | ||
| let version = tag.hasPrefix("v") ? String(tag.dropFirst()) : tag | ||
| guard isVersion(version) else { throw UpdateError.notAVersion(tag) } | ||
| let page = (json["html_url"] as? String).flatMap(URL.init(string:)) ?? releasesPage | ||
| return .success(Release(version: version, page: page)) | ||
| } catch { | ||
| return .failure(error) | ||
| } | ||
| } | ||
|
|
||
| enum UpdateError: LocalizedError { | ||
| case status(Int) | ||
| case notAVersion(String) | ||
|
|
||
| var errorDescription: String? { | ||
| switch self { | ||
| case .status(403), .status(429): return "GitHub is limiting requests right now. Try again in a while." | ||
| case .status(let code): return "GitHub answered with HTTP \(code)." | ||
| case .notAVersion(let tag): return "The latest release is tagged \"\(tag)\", which isn't a version number." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Numbers separated by dots, optionally followed by a dash and a label, such as 1.2.0-beta.1. | ||
| static func isVersion(_ v: String) -> Bool { | ||
| v.range(of: #"^\d+(\.\d+)*(-[0-9A-Za-z.]+)?$"#, options: .regularExpression) != nil | ||
| } | ||
|
|
||
| /// Whether latest is newer than current. A beta comes before its release, | ||
| /// so 1.2.0 is newer than 1.2.0-beta.1 and 1.1.4 is not. | ||
| static func isNewer(_ latest: String, than current: String) -> Bool { | ||
| func parts(_ v: String) -> (numbers: [Int], beta: String?) { | ||
| let split = v.split(separator: "-", maxSplits: 1).map(String.init) | ||
| return ((split.first ?? "").split(separator: ".").map { Int($0) ?? 0 }, split.count > 1 ? split[1] : nil) | ||
| } | ||
| let a = parts(latest), b = parts(current) | ||
| for i in 0..<max(a.numbers.count, b.numbers.count) { | ||
| let x = i < a.numbers.count ? a.numbers[i] : 0, y = i < b.numbers.count ? b.numbers[i] : 0 | ||
| if x != y { return x > y } | ||
| } | ||
| switch (a.beta, b.beta) { | ||
| case (nil, nil): return false | ||
| case (nil, _): return true | ||
| case (_, nil): return false | ||
| case (let x?, let y?): return x.compare(y, options: .numeric) == .orderedDescending | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject numeric components that
Intcannot parse.If a release tag contains Unicode digits such as
٢.٠.٠, or a component larger thanInt.max,isVersionaccepts it butInt($0) ?? 0changes that component to zero. The menu can then report “up to date” instead of an available update or a tag error. Require ASCII digits and parseable numeric components inisVersion; do not substitute zero after parsing fails. (developer.apple.com)