|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 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 FirebaseManifest |
| 20 | +import Utils |
| 21 | + |
| 22 | +struct InitializeRelease { |
| 23 | + static func setupRepo(gitRoot: URL) -> String { |
| 24 | + let manifest = FirebaseManifest.shared |
| 25 | + let branch = createReleaseBranch(path: gitRoot, version: manifest.version) |
| 26 | + updatePodspecs(path: gitRoot, manifest: manifest) |
| 27 | + updatePodfiles(path: gitRoot, version: manifest.version) |
| 28 | + return branch |
| 29 | + } |
| 30 | + |
| 31 | + /// The branch is based on the minor version to represent this is the branch for subsequent |
| 32 | + /// patches. |
| 33 | + private static func createReleaseBranch(path: URL, version: String) -> String { |
| 34 | + let versionParts = version.split(separator: ".") |
| 35 | + let minorVersion = "\(versionParts[0]).\(versionParts[1])" |
| 36 | + let branch = "release-\(minorVersion)" |
| 37 | + Shell.executeCommand("git checkout master", workingDir: path) |
| 38 | + Shell.executeCommand("git pull", workingDir: path) |
| 39 | + Shell.executeCommand("git checkout -b \(branch)", workingDir: path) |
| 40 | + return branch |
| 41 | + } |
| 42 | + |
| 43 | + /// Update the podspec versions. |
| 44 | + private static func updatePodspecs(path: URL, manifest: FirebaseManifest.Manifest) { |
| 45 | + for pod in manifest.pods { |
| 46 | + if !pod.isClosedSource { |
| 47 | + if pod.name == "Firebase" { |
| 48 | + updateFirebasePodspec(path: path, manifest: manifest) |
| 49 | + } else { |
| 50 | + let version = pod.podVersion ?? |
| 51 | + (pod.isBeta ? manifest.version + "-beta" : manifest.version) |
| 52 | + |
| 53 | + // Patch the new version to the podspec's version attribute. |
| 54 | + Shell.executeCommand("sed -i.bak -e \"s/\\(\\.version.*=[[:space:]]*'\\).*'/\\1" + |
| 55 | + "\(version)'/\" \(pod.name).podspec", workingDir: path) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // This function patches the versions in the Firebase.podspec. It uses Swift instead of sed |
| 62 | + // like the other version patching. |
| 63 | + // TODO: Choose one or the other mechanism. |
| 64 | + // TODO: If we keep Swift, consider using Scanner. |
| 65 | + private static func updateFirebasePodspec(path: URL, manifest: FirebaseManifest.Manifest) { |
| 66 | + let podspecFile = path.appendingPathComponent("Firebase.podspec") |
| 67 | + var contents = "" |
| 68 | + do { |
| 69 | + contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8) |
| 70 | + } catch { |
| 71 | + fatalError("Could not read Firebase podspec. \(error)") |
| 72 | + } |
| 73 | + let firebaseVersion = manifest.version |
| 74 | + for firebasePod in manifest.pods { |
| 75 | + if !firebasePod.isFirebase { |
| 76 | + continue |
| 77 | + } |
| 78 | + let pod = firebasePod.name |
| 79 | + let version = firebasePod.isBeta ? firebaseVersion + "-beta" : firebaseVersion |
| 80 | + if pod == "Firebase" { |
| 81 | + // TODO: This then block is redundant with the updatePodspecs function above and is left |
| 82 | + // until we decide to go with Swift or sed. |
| 83 | + // Replace version in string like s.version = '6.9.0' |
| 84 | + guard let range = contents.range(of: "s.version") else { |
| 85 | + fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)") |
| 86 | + } |
| 87 | + var versionStartIndex = contents.index(range.upperBound, offsetBy: 1) |
| 88 | + while contents[versionStartIndex] != "'" { |
| 89 | + versionStartIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 90 | + } |
| 91 | + var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 92 | + while contents[versionEndIndex] != "'" { |
| 93 | + versionEndIndex = contents.index(versionEndIndex, offsetBy: 1) |
| 94 | + } |
| 95 | + contents.removeSubrange(versionStartIndex ... versionEndIndex) |
| 96 | + contents.insert(contentsOf: "'" + version + "'", at: versionStartIndex) |
| 97 | + } else { |
| 98 | + // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0' |
| 99 | + guard let range = contents.range(of: pod) else { |
| 100 | + // This pod is not a top-level Firebase pod dependency. |
| 101 | + continue |
| 102 | + } |
| 103 | + var versionStartIndex = contents.index(range.upperBound, offsetBy: 2) |
| 104 | + while !contents[versionStartIndex].isWholeNumber { |
| 105 | + versionStartIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 106 | + } |
| 107 | + var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1) |
| 108 | + while contents[versionEndIndex] != "'" { |
| 109 | + versionEndIndex = contents.index(versionEndIndex, offsetBy: 1) |
| 110 | + } |
| 111 | + contents.removeSubrange(versionStartIndex ... versionEndIndex) |
| 112 | + contents.insert(contentsOf: version + "'", at: versionStartIndex) |
| 113 | + } |
| 114 | + } |
| 115 | + do { |
| 116 | + try contents.write(to: podspecFile, atomically: false, encoding: .utf8) |
| 117 | + } catch { |
| 118 | + fatalError("Failed to write \(podspecFile.path). \(error)") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static func updatePodfiles(path: URL, version: String) { |
| 123 | + // Update the Podfiles across the repo. |
| 124 | + let firestorePodfile = path.appendingPathComponent("Firestore") |
| 125 | + .appendingPathComponent("Example") |
| 126 | + let collisionPodfile = path.appendingPathComponent("SymbolCollisionTest") |
| 127 | + let sedCommand = "sed -i.bak -e \"s#\\(pod " + |
| 128 | + "'Firebase/CoreOnly',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile" |
| 129 | + Shell.executeCommand(sedCommand, workingDir: firestorePodfile) |
| 130 | + |
| 131 | + let sedCommand2 = "sed -i.bak -e \"s#\\(pod " + |
| 132 | + "'Firebase',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile" |
| 133 | + Shell.executeCommand(sedCommand2, workingDir: collisionPodfile) |
| 134 | + } |
| 135 | +} |
0 commit comments