|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +import ManifestReader |
| 20 | + |
| 21 | +// Get the launch arguments, parsed by user defaults. |
| 22 | +let args = LaunchArgs.shared |
| 23 | + |
| 24 | +// Keep timing for how long it takes to change the Firebase pod versions. |
| 25 | +let buildStart = Date() |
| 26 | +var cocoaPodsUpdateMessage: String = "" |
| 27 | + |
| 28 | +var paths = FirebasePod.FilesystemPaths(currentReleasePath: args.currentReleasePath, |
| 29 | + gitRootPath: args.gitRootPath) |
| 30 | + |
| 31 | +/// Assembles the expected versions based on the release manifest passed in. |
| 32 | +/// Returns an array with the pod name as the key and version as the value, |
| 33 | +private func getExpectedVersions() -> [String: String] { |
| 34 | + // Merge the versions from the current release and the known public versions. |
| 35 | + var releasingVersions: [String: String] = [:] |
| 36 | + |
| 37 | + // Override any of the expected versions with the current release manifest, if it exists. |
| 38 | + let currentRelease = ManifestReader.loadCurrentRelease(fromTextproto: paths.currentReleasePath) |
| 39 | + print("Overriding the following Pod versions, taken from the current release manifest:") |
| 40 | + for pod in currentRelease.sdk { |
| 41 | + releasingVersions[pod.sdkName] = pod.sdkVersion |
| 42 | + print("\(pod.sdkName): \(pod.sdkVersion)") |
| 43 | + } |
| 44 | + |
| 45 | + if !releasingVersions.isEmpty { |
| 46 | + print("Updating Firebase Pod in git installation at \(paths.gitRootPath)) " + |
| 47 | + "with the following versions: \(releasingVersions)") |
| 48 | + } |
| 49 | + |
| 50 | + return releasingVersions |
| 51 | +} |
| 52 | + |
| 53 | +private func updateFirebasePod(newVersions: [String: String]) { |
| 54 | + let podspecFile = paths.gitRootPath + "/Firebase.podspec" |
| 55 | + var contents = "" |
| 56 | + do { |
| 57 | + contents = try String(contentsOfFile: podspecFile, encoding: .utf8) |
| 58 | + } catch { |
| 59 | + fatalError("Could not read Firebase podspec. \(error)") |
| 60 | + } |
| 61 | + for (pod, version) in newVersions { |
| 62 | + if pod == "Firebase" { |
| 63 | + // Replace version in string like s.version = '6.9.0' |
| 64 | + guard let range = contents.range(of: "s.version") else { |
| 65 | + fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)") |
| 66 | + } |
| 67 | + var versionStartIndex = contents.index(range.upperBound, offsetBy: 1) |
| 68 | + while contents[versionStartIndex] != "'" { |
| 69 | + versionStartIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 70 | + } |
| 71 | + var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 72 | + while contents[versionEndIndex] != "'" { |
| 73 | + versionEndIndex = contents.index(versionEndIndex, offsetBy: 1) |
| 74 | + } |
| 75 | + contents.removeSubrange(versionStartIndex...versionEndIndex) |
| 76 | + contents.insert(contentsOf:"'" + version + "'", at:versionStartIndex) |
| 77 | + } else { |
| 78 | + // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0' |
| 79 | + guard let range = contents.range(of: pod) else { |
| 80 | + // This pod is not a top-level Firebase pod dependency. |
| 81 | + continue |
| 82 | + } |
| 83 | + var versionStartIndex = contents.index(range.upperBound, offsetBy: 2) |
| 84 | + while !contents[versionStartIndex].isWholeNumber { |
| 85 | + versionStartIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 86 | + } |
| 87 | + var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 88 | + while contents[versionEndIndex] != "'" { |
| 89 | + versionEndIndex = contents.index(versionEndIndex, offsetBy: 1) |
| 90 | + } |
| 91 | + contents.removeSubrange(versionStartIndex...versionEndIndex) |
| 92 | + contents.insert(contentsOf:version + "'", at:versionStartIndex) |
| 93 | + } |
| 94 | + } |
| 95 | + do { |
| 96 | + try contents.write(toFile: podspecFile, atomically: false, encoding: String.Encoding.utf8) |
| 97 | + } |
| 98 | + catch { |
| 99 | + fatalError("Failed to write \(podspecFile). \(error)") |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +do { |
| 104 | + let newVersions = getExpectedVersions() |
| 105 | + updateFirebasePod(newVersions: newVersions) |
| 106 | + print("Updating Firebase pod for version \(String(describing: newVersions["Firebase"]!))") |
| 107 | + |
| 108 | + // Get the time since the tool start. |
| 109 | + let secondsSinceStart = -Int(buildStart.timeIntervalSinceNow) |
| 110 | + print(""" |
| 111 | + Time profile: |
| 112 | + It took \(secondsSinceStart) seconds (~\(secondsSinceStart / 60)m) to update the Firebase pod. |
| 113 | + \(cocoaPodsUpdateMessage) |
| 114 | + """) |
| 115 | +} |
| 116 | + |
0 commit comments