Skip to content

Commit afb5638

Browse files
authored
Zip fixes (#4662)
* Fix the version parsing to allow for betas. (#4652) * Remove extra quotes when parsing versions and names. (#4654) Authored-by: Ryan Wilson <[email protected]>
1 parent 0dd8dda commit afb5638

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

ZipBuilder/Sources/ZipBuilder/CocoaPodUtils.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ enum CocoaPodUtils {
134134

135135
// Force unwrap the regular expression since we know it will work, it's a constant being passed
136136
// in. If any changes are made, be sure to run this script to ensure it works.
137-
let podRegex = try! NSRegularExpression(pattern: " - (.+) \\((\\d+\\.\\d+\\.?\\d*)\\)",
138-
options: [])
139137
let depRegex: NSRegularExpression = try! NSRegularExpression(pattern: " - (.+).*",
140138
options: [])
141139
let quotes = CharacterSet(charactersIn: "\"")
@@ -146,7 +144,7 @@ enum CocoaPodUtils {
146144
if line.starts(with: "DEPENDENCIES:") {
147145
break
148146
}
149-
if let (pod, version) = detectVersion(fromLine: line, matching: podRegex) {
147+
if let (pod, version) = detectVersion(fromLine: line) {
150148
let corePod = pod.components(separatedBy: "/")[0]
151149
currentPod = corePod.trimmingCharacters(in: quotes)
152150
pods[currentPod!] = version
@@ -262,24 +260,28 @@ enum CocoaPodUtils {
262260
///
263261
/// - Parameters:
264262
/// - input: A line entry from Podfile.lock.
265-
/// - regex: The regex to match compared to the input.
266263
/// - Returns: A tuple of the framework and version, if it can be parsed.
267-
private static func detectVersion(fromLine input: String,
268-
matching regex: NSRegularExpression) -> (framework: String, version: String)? {
269-
let matches = regex.matches(in: input, range: NSRange(location: 0, length: input.utf8.count))
270-
let nsString = input as NSString
271-
272-
guard let match = matches.first else {
273-
return nil
274-
}
275-
276-
guard match.numberOfRanges == 3 else {
277-
print("Version number regex matches: expected 3, but found \(match.numberOfRanges).")
278-
return nil
279-
}
280-
281-
let framework = nsString.substring(with: match.range(at: 1)) as String
282-
let version = nsString.substring(with: match.range(at: 2)) as String
264+
private static func detectVersion(fromLine input: String) -> (framework: String, version: String)? {
265+
// Get the components of the line to parse them individually. Ignore any whitespace only Strings.
266+
let components = input.components(separatedBy: " ").filter { !$0.isEmpty }
267+
268+
// Expect three components: the `-`, the pod name, and the version in parens. This will filter out
269+
// dependencies that have version requirements like `(~> 3.2.1)` in it.
270+
guard components.count == 3 else { return nil }
271+
272+
// The first component is simple, just the `-`.
273+
guard components.first == "-" else { return nil }
274+
275+
// The second component is a pod/framework name, which we want to return eventually. Remove any
276+
// extraneous quotes.
277+
let framework = components[1].trimmingCharacters(in: CharacterSet(charactersIn: "\""))
278+
279+
// The third component is the version in parentheses, potentially with a `:` at the end. Let's
280+
// just strip the unused characters (including quotes) and return the version. We don't
281+
// necesarily have to match against semver since it's a non trivial regex and we don't actually
282+
// care, `Podfile.lock` has a standard format that we know will be valid. Also strip out any
283+
// extra quotes.
284+
let version = components[2].trimmingCharacters(in: CharacterSet(charactersIn: "():\""))
283285

284286
return (framework, version)
285287
}

0 commit comments

Comments
 (0)