InitializeRelease.swift 5.5 KB

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