main.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 ArgumentParser
  18. import FirebaseManifest
  19. import Utils
  20. struct FirebaseReleaser: ParsableCommand {
  21. /// The root of the Firebase git repo.
  22. // TODO: Add a default that sets the current repo - ['git', 'rev-parse', '--show-toplevel']
  23. @Option(help: "The root of the firebase-ios-sdk checked out git repo.",
  24. transform: URL.init(fileURLWithPath:))
  25. var gitRoot: URL
  26. /// Log commands only and do not make any repository or source changes.
  27. /// Useful for testing and for generating the list of push commands.
  28. @Option(default: false,
  29. help: "Log without executing the shell commands")
  30. var logOnly: Bool
  31. /// Set this option when starting a release.
  32. @Option(default: false,
  33. help: "Initialize the release branch")
  34. var initBranch: Bool
  35. /// Set this option when starting a release.
  36. @Option(default: "main",
  37. help: "The base branch to use. Defaults to `main`.")
  38. var baseBranch: String
  39. /// Set this option to output the commands to generate the ordered `pod trunk push` commands.
  40. @Option(default: false,
  41. help: "Publish the podspecs to the CocoaPodsTrunk")
  42. var publish: Bool
  43. /// Set this option to only update the podspecs on SpecsStaging.
  44. @Option(default: false,
  45. help: "Update the podspecs only")
  46. var pushOnly: Bool
  47. /// Set this option to update tags only.
  48. @Option(default: false,
  49. help: "Update the tags only")
  50. var updateTagsOnly: Bool
  51. mutating func validate() throws {
  52. guard FileManager.default.fileExists(atPath: gitRoot.path) else {
  53. throw ValidationError("git-root does not exist: \(gitRoot.path)")
  54. }
  55. }
  56. func run() throws {
  57. let startDate = Date()
  58. print("Started at: \(startDate.dateTimeString())")
  59. if logOnly {
  60. Shell.setLogOnly()
  61. }
  62. Shell.executeCommand("git checkout \(baseBranch)", workingDir: gitRoot)
  63. Shell.executeCommand("git pull origin \(baseBranch)", workingDir: gitRoot)
  64. Shell.executeCommand("git fetch origin --tags --force", workingDir: gitRoot)
  65. if initBranch {
  66. let branch = InitializeRelease.setupRepo(gitRoot: gitRoot)
  67. let version = FirebaseManifest.shared.version
  68. Shell.executeCommand("git commit -am \"Update versions for Release \(version)\"",
  69. workingDir: gitRoot)
  70. Shell.executeCommand("git push origin \(branch)", workingDir: gitRoot)
  71. Shell.executeCommand("git branch --set-upstream-to=origin/\(branch) \(branch)",
  72. workingDir: gitRoot)
  73. Tags.createTags(gitRoot: gitRoot)
  74. Push.pushPodsToStaging(gitRoot: gitRoot)
  75. } else if updateTagsOnly {
  76. let tag = "CocoaPods-\(FirebaseManifest.shared.version)"
  77. let podsNeedingStaging = Shell.executeCommandFromScript(
  78. "git diff --name-only \(tag) -- *.podspec",
  79. outputToConsole: false,
  80. workingDir: gitRoot
  81. )
  82. Tags.updateTags(gitRoot: gitRoot)
  83. if case let .success(pods) = podsNeedingStaging, !pods.isEmpty {
  84. Shell.executeCommand(
  85. "echo -e \"\\033[33m⚠ Warning – the following pods need re-staging:\n \(pods)\\033[33m\"",
  86. outputToConsole: false
  87. )
  88. }
  89. } else if pushOnly {
  90. Push.pushPodsToStaging(gitRoot: gitRoot)
  91. } else if publish {
  92. Push.publishPodsToTrunk(gitRoot: gitRoot)
  93. }
  94. let finishDate = Date()
  95. print("Finished at: \(finishDate.dateTimeString()). " +
  96. "Duration: \(startDate.formattedDurationSince(finishDate))")
  97. }
  98. private func updateFirebasePod(newVersions: [String: String]) {
  99. let podspecFile = gitRoot.appendingPathComponent("Firebase.podspec")
  100. var contents = ""
  101. do {
  102. contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8)
  103. } catch {
  104. fatalError("Could not read Firebase podspec. \(error)")
  105. }
  106. for (pod, version) in newVersions {
  107. if pod == "Firebase" {
  108. // Replace version in string like s.version = '6.9.0'
  109. guard let range = contents.range(of: "s.version") else {
  110. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  111. }
  112. var versionStartIndex = contents.index(range.upperBound, offsetBy: 1)
  113. while contents[versionStartIndex] != "'" {
  114. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  115. }
  116. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  117. while contents[versionEndIndex] != "'" {
  118. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  119. }
  120. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  121. contents.insert(contentsOf: "'" + version + "'", at: versionStartIndex)
  122. } else {
  123. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'
  124. guard let range = contents.range(of: pod) else {
  125. // This pod is not a top-level Firebase pod dependency.
  126. continue
  127. }
  128. var versionStartIndex = contents.index(range.upperBound, offsetBy: 2)
  129. while !contents[versionStartIndex].isWholeNumber {
  130. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  131. }
  132. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  133. while contents[versionEndIndex] != "'" {
  134. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  135. }
  136. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  137. contents.insert(contentsOf: version + "'", at: versionStartIndex)
  138. }
  139. }
  140. do {
  141. try contents.write(to: podspecFile, atomically: false, encoding: .utf8)
  142. } catch {
  143. fatalError("Failed to write \(podspecFile.path). \(error)")
  144. }
  145. }
  146. }
  147. // Start the parsing and run the tool.
  148. FirebaseReleaser.main()