@@ -134,8 +134,6 @@ enum CocoaPodUtils {
134
134
135
135
// Force unwrap the regular expression since we know it will work, it's a constant being passed
136
136
// 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: [ ] )
139
137
let depRegex : NSRegularExpression = try ! NSRegularExpression ( pattern: " - (.+).* " ,
140
138
options: [ ] )
141
139
let quotes = CharacterSet ( charactersIn: " \" " )
@@ -146,7 +144,7 @@ enum CocoaPodUtils {
146
144
if line. starts ( with: " DEPENDENCIES: " ) {
147
145
break
148
146
}
149
- if let ( pod, version) = detectVersion ( fromLine: line, matching : podRegex ) {
147
+ if let ( pod, version) = detectVersion ( fromLine: line) {
150
148
let corePod = pod. components ( separatedBy: " / " ) [ 0 ]
151
149
currentPod = corePod. trimmingCharacters ( in: quotes)
152
150
pods [ currentPod!] = version
@@ -262,24 +260,28 @@ enum CocoaPodUtils {
262
260
///
263
261
/// - Parameters:
264
262
/// - input: A line entry from Podfile.lock.
265
- /// - regex: The regex to match compared to the input.
266
263
/// - 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: " (): \" " ) )
283
285
284
286
return ( framework, version)
285
287
}
0 commit comments