|
2 | 2 | // SwiftVersion.swift |
3 | 3 | // HexavillePackageDescription |
4 | 4 | // |
5 | | -// Created by Yuki Takei on 2017/08/13. |
| 5 | +// Created by Yuki Takei on 2017/12/18. |
6 | 6 | // |
7 | 7 |
|
8 | 8 | import Foundation |
9 | 9 |
|
10 | | -public enum SwiftVersionContainer { |
11 | | - case release(SwiftVersion) |
| 10 | +public enum SwiftVersion { |
| 11 | + case release(Version) |
12 | 12 | case developmentSnapshot(SwiftDevelopmentSnapshot) |
13 | 13 | } |
14 | 14 |
|
15 | | -extension SwiftVersionContainer { |
| 15 | +extension SwiftVersion { |
16 | 16 | public init(string versionString: String) throws { |
17 | 17 | if versionString.contains(substring: SwiftDevelopmentSnapshot.snapshotIdentifer) { |
18 | 18 | self = .developmentSnapshot(try SwiftDevelopmentSnapshot(string: versionString)) |
19 | 19 | } else { |
20 | | - self = .release(try SwiftVersion(string: versionString)) |
| 20 | + self = .release(try Version(string: versionString)) |
21 | 21 | } |
22 | 22 | } |
23 | 23 | } |
24 | 24 |
|
25 | | -extension SwiftVersionContainer { |
| 25 | +extension SwiftVersion { |
26 | 26 | public var versionString: String { |
27 | 27 | switch self { |
28 | 28 | case .developmentSnapshot(let snapshot): |
@@ -65,101 +65,17 @@ extension SwiftVersionContainer { |
65 | 65 | return "\(downloadBaseURLString)/\(path)/\(fileName).tar.gz" |
66 | 66 | } |
67 | 67 |
|
68 | | - public func asCompareableVersion() -> SwiftVersion { |
| 68 | + public func asCompareableVersion() -> Version { |
69 | 69 | switch self { |
70 | 70 | case .developmentSnapshot(let snapshot): |
71 | | - return SwiftVersion(major: snapshot.major, minor: snapshot.minor, patch: snapshot.patch) |
| 71 | + return Version(major: snapshot.major, minor: snapshot.minor, patch: snapshot.patch) |
72 | 72 |
|
73 | 73 | case .release(let version): |
74 | 74 | return version |
75 | 75 | } |
76 | 76 | } |
77 | 77 | } |
78 | 78 |
|
79 | | -public protocol SwiftVersionRepresentable: Hashable, Comparable { |
80 | | - var major: Int { get } |
81 | | - var minor: Int { get } |
82 | | - var patch: Int { get } |
83 | | -} |
84 | | - |
85 | | -extension SwiftVersionRepresentable { |
86 | | - public static func < <Other: SwiftVersionRepresentable>(lhs: Self, rhs: Other) -> Bool { |
87 | | - if lhs.major != rhs.major { |
88 | | - return lhs.major < rhs.major |
89 | | - } else { |
90 | | - if lhs.minor < rhs.minor { |
91 | | - return true |
92 | | - } |
93 | | - return lhs.patch < rhs.patch |
94 | | - } |
95 | | - } |
96 | | -} |
97 | | - |
98 | | -extension SwiftVersionRepresentable { |
99 | | - public var hashValue: Int { |
100 | | - return (major << 8) | minor | patch |
101 | | - } |
102 | | - |
103 | | - public static func == <Other: SwiftVersionRepresentable>(lhs: Self, rhs: Other) -> Bool { |
104 | | - return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch |
105 | | - } |
106 | | - |
107 | | - public static func ~= <Other: SwiftVersionRepresentable>(match: Self, version: Other) -> Bool { |
108 | | - return match == version |
109 | | - } |
110 | | -} |
111 | | - |
112 | | -enum SwiftVersionError: Error { |
113 | | - case invalidVersion(String) |
114 | | - case notEmpty |
115 | | -} |
116 | | - |
117 | | -public struct SwiftVersion: SwiftVersionRepresentable { |
118 | | - public let major: Int |
119 | | - public let minor: Int |
120 | | - public let patch: Int |
121 | | - |
122 | | - public var versionString: String { |
123 | | - var version = "\(major).\(minor)" |
124 | | - if patch > 0 { |
125 | | - version += ".\(patch)" |
126 | | - } |
127 | | - return version |
128 | | - } |
129 | | - |
130 | | - public init(major: Int, minor: Int, patch: Int = 0) { |
131 | | - self.major = major |
132 | | - self.minor = minor |
133 | | - self.patch = patch |
134 | | - } |
135 | | -} |
136 | | - |
137 | | -extension SwiftVersion { |
138 | | - public init(string: String) throws { |
139 | | - let components = string.components(separatedBy: ".") |
140 | | - if components.count == 0 { |
141 | | - throw SwiftVersionError.notEmpty |
142 | | - } |
143 | | - |
144 | | - var intCastedComponents: [Int] = try components.map({ |
145 | | - guard let int = Int($0) else { |
146 | | - throw SwiftVersionError.invalidVersion(string) |
147 | | - } |
148 | | - return int |
149 | | - }) |
150 | | - |
151 | | - switch intCastedComponents.count { |
152 | | - case 1: |
153 | | - self = SwiftVersion(major: intCastedComponents[0], minor: 0) |
154 | | - case 2: |
155 | | - self = SwiftVersion(major: intCastedComponents[0], minor: intCastedComponents[1]) |
156 | | - case 3: |
157 | | - self = SwiftVersion(major: intCastedComponents[0], minor: intCastedComponents[1], patch: intCastedComponents[2]) |
158 | | - default: |
159 | | - throw SwiftVersionError.invalidVersion(string) |
160 | | - } |
161 | | - } |
162 | | -} |
163 | 79 |
|
164 | 80 | public enum SwiftDevelopmentSnapshotError: Error { |
165 | 81 | case invalidDevelopmentSnapshotName(String) |
|
0 commit comments