InitializeRelease.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import Foundation
  17. import FirebaseManifest
  18. import Utils
  19. enum InitializeRelease {
  20. static func setupRepo(gitRoot: URL) -> String {
  21. let manifest = FirebaseManifest.shared
  22. let branch = createVersionBranch(path: gitRoot, version: manifest.version)
  23. updatePodspecs(path: gitRoot, manifest: manifest)
  24. updatePodfiles(path: gitRoot, version: manifest.version)
  25. updateSwiftPackageVersion(path: gitRoot, version: manifest.version)
  26. return branch
  27. }
  28. /// The branch is based on the minor version to represent this is the branch for subsequent
  29. /// patches.
  30. private static func createVersionBranch(path: URL, version: String) -> String {
  31. let versionParts = version.split(separator: ".")
  32. let minorVersion = "\(versionParts[0]).\(versionParts[1])"
  33. let branch = "version-\(minorVersion)"
  34. Shell.executeCommand(
  35. "git checkout \(branch) 2>/dev/null || git checkout -b \(branch)",
  36. workingDir: path
  37. )
  38. return branch
  39. }
  40. /// Update the podspec versions.
  41. private static func updatePodspecs(path: URL, manifest: FirebaseManifest.Manifest) {
  42. for pod in manifest.pods {
  43. let version = manifest.versionString(pod)
  44. if pod.name == "Firebase" {
  45. updateFirebasePodspec(path: path, manifest: manifest)
  46. } else {
  47. updatePodspecVersion(pod: pod, version: version, path: path)
  48. // Pods dependencies to update to latest.
  49. if pod.name.hasPrefix("GoogleAppMeasurement") ||
  50. pod.name == "FirebaseCore" ||
  51. pod.name == "FirebaseCoreExtension" ||
  52. pod.name == "FirebaseCoreInternal" ||
  53. pod.name == "FirebaseFirestoreInternal" ||
  54. pod.name == "FirebaseAI" {
  55. updateDependenciesToLatest(
  56. dependency: pod.name,
  57. pods: manifest.pods,
  58. version: version,
  59. path: path
  60. )
  61. } else if version.hasSuffix(".0.0") {
  62. let patchlessVersion = String(version[..<version.lastIndex(of: ".")!])
  63. updateDependenciesToLatest(
  64. dependency: pod.name,
  65. pods: manifest.pods,
  66. version: patchlessVersion,
  67. path: path
  68. )
  69. }
  70. }
  71. }
  72. }
  73. private static func updatePodspecVersion(pod: Pod,
  74. version: String,
  75. path: URL) {
  76. // Replace the pod's `version` attribute with the new version.
  77. let script = #"-e "s|(\.version.*=[[:space:]]*) '.*|\1 '\#(version)'|""#
  78. let command = "sed -i.bak -E \(script) \(pod.podspecName())"
  79. Shell.executeCommand(command, workingDir: path)
  80. }
  81. /// Update dependencies that we want pinned to the latest version.
  82. private static func updateDependenciesToLatest(dependency: String,
  83. pods: [Pod],
  84. version: String,
  85. path: URL) {
  86. let script =
  87. #"-e "s|(\.dependency '"# + dependency + #"(/.*)?',[[:space:]]*'[^0-9]*).*|\1\#(version)'|""#
  88. let podspecs = pods.map { $0.podspecName() }.joined(separator: " ")
  89. let command = "sed -i.bak -E \(script) \(podspecs)"
  90. Shell.executeCommand(command, workingDir: path)
  91. }
  92. // This function patches the versions in the Firebase.podspec. It uses Swift instead of sed
  93. // like the other version patching.
  94. // TODO: Choose one or the other mechanism.
  95. // TODO: If we keep Swift, consider using Scanner.
  96. private static func updateFirebasePodspec(path: URL, manifest: FirebaseManifest.Manifest) {
  97. let podspecFile = path.appendingPathComponent("Firebase.podspec")
  98. var contents = ""
  99. do {
  100. contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8)
  101. } catch {
  102. fatalError("Could not read Firebase podspec. \(error)")
  103. }
  104. let firebaseVersion = manifest.version
  105. for firebasePod in manifest.pods {
  106. let pod = firebasePod.name
  107. let version = firebasePod.isBeta ? firebaseVersion + "-beta" : firebaseVersion
  108. if pod == "Firebase" {
  109. // TODO: This block is redundant with `updatePodspecs`. Decide to go with Swift or sed.
  110. guard let range = contents.range(of: "s.version") else {
  111. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  112. }
  113. // Replace version in string like s.version = '6.9.0'
  114. updateVersion(&contents, in: range, to: version)
  115. } else {
  116. // Iterate through all the ranges of `pod`'s occurrences.
  117. for range in contents.ranges(of: pod) {
  118. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'.
  119. updateVersion(&contents, in: range, to: version)
  120. }
  121. }
  122. }
  123. do {
  124. try contents.write(to: podspecFile, atomically: false, encoding: .utf8)
  125. } catch {
  126. fatalError("Failed to write \(podspecFile.path). \(error)")
  127. }
  128. }
  129. /// Update the existing version to the given version by writing to a given string using the
  130. /// provided range.
  131. /// - Parameters:
  132. /// - contents: A reference to a String containing a version that will be updated.
  133. /// - range: The range containing a version substring that will be updated.
  134. /// - version: The version string to update to.
  135. private static func updateVersion(_ contents: inout String, in range: Range<String.Index>,
  136. to version: String) {
  137. var versionStartIndex = contents.index(after: range.upperBound)
  138. while !contents[versionStartIndex].isWholeNumber {
  139. versionStartIndex = contents.index(after: versionStartIndex)
  140. }
  141. var versionEndIndex = contents.index(after: versionStartIndex)
  142. while contents[versionEndIndex] != "'" {
  143. versionEndIndex = contents.index(after: versionEndIndex)
  144. }
  145. contents.replaceSubrange(versionStartIndex ..< versionEndIndex, with: version)
  146. }
  147. private static func updatePodfiles(path: URL, version: String) {
  148. // Update the Podfiles across the repo.
  149. let firestorePodfile = path.appendingPathComponent("Firestore")
  150. .appendingPathComponent("Example")
  151. let collisionPodfile = path.appendingPathComponent("SymbolCollisionTest")
  152. let sedCommand = "sed -i.bak -e \"s#\\(pod " +
  153. "'Firebase/CoreOnly',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile"
  154. Shell.executeCommand(sedCommand, workingDir: firestorePodfile)
  155. let sedCommand2 = "sed -i.bak -e \"s#\\(pod " +
  156. "'Firebase',[[:space:]]*'\\).*'#\\1\(version)'#\" Podfile"
  157. Shell.executeCommand(sedCommand2, workingDir: collisionPodfile)
  158. }
  159. private static func updateSwiftPackageVersion(path: URL, version: String) {
  160. // Match strings like `let firebaseVersion = "7.7.0"` and update the version.
  161. Shell.executeCommand("sed -i.bak -e \"s/\\(let firebaseVersion.*=[[:space:]]*\\).*/\\1" +
  162. "\\\"\(version)\\\"/\" Package.swift", workingDir: path)
  163. }
  164. }