/* * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Foundation import FirebaseManifest import Utils enum InitializeRelease { static func setupRepo(gitRoot: URL) -> String { let manifest = FirebaseManifest.shared let branch = createVersionBranch(path: gitRoot, version: manifest.version) updatePodspecs(path: gitRoot, manifest: manifest) updatePodfiles(path: gitRoot, version: manifest.version) updateSwiftPackageVersion(path: gitRoot, version: manifest.version) return branch } /// The branch is based on the minor version to represent this is the branch for subsequent /// patches. private static func createVersionBranch(path: URL, version: String) -> String { let versionParts = version.split(separator: ".") let minorVersion = "\(versionParts[0]).\(versionParts[1])" let branch = "version-\(minorVersion)" Shell.executeCommand( "git checkout \(branch) 2>/dev/null || git checkout -b \(branch)", workingDir: path ) return branch } /// Update the podspec versions. private static func updatePodspecs(path: URL, manifest: FirebaseManifest.Manifest) { for pod in manifest.pods { let version = manifest.versionString(pod) if pod.name == "Firebase" { updateFirebasePodspec(path: path, manifest: manifest) } else { updatePodspecVersion(pod: pod, version: version, path: path) // Pods depending on GoogleAppMeasurement and FirebaseFirestoreInternal specs // should pin the dependency to the new version. if pod.name.hasPrefix("GoogleAppMeasurement") || pod.name == "FirebaseFirestoreInternal" { updateDependenciesToLatest( dependency: pod.name, pods: manifest.pods, version: version, path: path ) } else if version.hasSuffix(".0.0") { let patchlessVersion = String(version[.., to version: String) { var versionStartIndex = contents.index(after: range.upperBound) while !contents[versionStartIndex].isWholeNumber { versionStartIndex = contents.index(after: versionStartIndex) } var versionEndIndex = contents.index(after: versionStartIndex) while contents[versionEndIndex] != "'" { versionEndIndex = contents.index(after: versionEndIndex) } contents.replaceSubrange(versionStartIndex ..< versionEndIndex, with: version) } private static func updatePodfiles(path: URL, version: String) { // Update the Podfiles across the repo. let firestorePodfile = path.appendingPathComponent("Firestore") .appendingPathComponent("Example") let collisionPodfile = path.appendingPathComponent("SymbolCollisionTest") let sedCommand = "sed -i.bak -e \"s#\\(pod " + "'Firebase/CoreOnly',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile" Shell.executeCommand(sedCommand, workingDir: firestorePodfile) let sedCommand2 = "sed -i.bak -e \"s#\\(pod " + "'Firebase',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile" Shell.executeCommand(sedCommand2, workingDir: collisionPodfile) } private static func updateSwiftPackageVersion(path: URL, version: String) { // Match strings like `let firebaseVersion = "7.7.0"` and update the version. Shell.executeCommand("sed -i.bak -e \"s/\\(let firebaseVersion.*=[[:space:]]*\\).*/\\1" + "\\\"\(version)\\\"/\" Package.swift", workingDir: path) } }