InitializeRelease.swift 5.9 KB

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