-
Notifications
You must be signed in to change notification settings - Fork 3
/
Version.swift
44 lines (39 loc) · 1.24 KB
/
Version.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Foundation
import Releases
enum ReleaseError: Error {
case loadingFailed(URL, String)
case noReleaseFound(URL)
}
extension Version {
static func getLatestRelease(from url: URL) throws -> Version {
guard var version = try loadLatestRelease(from: url) else {
throw ReleaseError.noReleaseFound(url)
}
version.prefix = nil
return version
}
static func loadLatestRelease(from url: URL) throws -> Version? {
var url = url
if url.pathExtension != "git" {
url.appendPathExtension("git")
}
do {
let allReleases = try Releases.versions(for: url)
let relevantReleases = allReleases.withoutPreReleases()
let lastRelease = relevantReleases.max()
return lastRelease
} catch {
throw ReleaseError.loadingFailed(url, error.localizedDescription)
}
}
}
extension ReleaseError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .loadingFailed(url, error):
return "Could not get release data from URL \(url): \(error)"
case let .noReleaseFound(url):
return "No release found for URL: \(url)"
}
}
}