main.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if initBranch {
  65. let branch = InitializeRelease.setupRepo(gitRoot: gitRoot)
  66. let version = FirebaseManifest.shared.version
  67. Shell.executeCommand("git commit -am \"Update versions for Release \(version)\"",
  68. workingDir: gitRoot)
  69. Shell.executeCommand("git push origin \(branch)", workingDir: gitRoot)
  70. Shell.executeCommand("git branch --set-upstream-to=origin/\(branch) \(branch)",
  71. workingDir: gitRoot)
  72. Tags.createTags(gitRoot: gitRoot)
  73. Push.pushPodsToStaging(gitRoot: gitRoot)
  74. } else if updateTagsOnly {
  75. let tag = "CocoaPods-\(FirebaseManifest.shared.version)"
  76. let podsNeedingStaging = Shell.executeCommandFromScript(
  77. "git diff --name-only \(tag) -- *.podspec",
  78. outputToConsole: false,
  79. workingDir: gitRoot
  80. )
  81. Tags.updateTags(gitRoot: gitRoot)
  82. if case let .success(pods) = podsNeedingStaging, !pods.isEmpty {
  83. Shell.executeCommand(
  84. "echo -e \"\\033[33m⚠ Warning – the following pods need re-staging:\n \(pods)\\033[33m\"",
  85. outputToConsole: false
  86. )
  87. }
  88. } else if pushOnly {
  89. Push.pushPodsToStaging(gitRoot: gitRoot)
  90. } else if publish {
  91. Push.publishPodsToTrunk(gitRoot: gitRoot)
  92. }
  93. let finishDate = Date()
  94. print("Finished at: \(finishDate.dateTimeString()). " +
  95. "Duration: \(startDate.formattedDurationSince(finishDate))")
  96. }
  97. private func updateFirebasePod(newVersions: [String: String]) {
  98. let podspecFile = gitRoot.appendingPathComponent("Firebase.podspec")
  99. var contents = ""
  100. do {
  101. contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8)
  102. } catch {
  103. fatalError("Could not read Firebase podspec. \(error)")
  104. }
  105. for (pod, version) in newVersions {
  106. if pod == "Firebase" {
  107. // Replace version in string like s.version = '6.9.0'
  108. guard let range = contents.range(of: "s.version") else {
  109. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  110. }
  111. var versionStartIndex = contents.index(range.upperBound, offsetBy: 1)
  112. while contents[versionStartIndex] != "'" {
  113. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  114. }
  115. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  116. while contents[versionEndIndex] != "'" {
  117. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  118. }
  119. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  120. contents.insert(contentsOf: "'" + version + "'", at: versionStartIndex)
  121. } else {
  122. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'
  123. guard let range = contents.range(of: pod) else {
  124. // This pod is not a top-level Firebase pod dependency.
  125. continue
  126. }
  127. var versionStartIndex = contents.index(range.upperBound, offsetBy: 2)
  128. while !contents[versionStartIndex].isWholeNumber {
  129. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  130. }
  131. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  132. while contents[versionEndIndex] != "'" {
  133. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  134. }
  135. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  136. contents.insert(contentsOf: version + "'", at: versionStartIndex)
  137. }
  138. }
  139. do {
  140. try contents.write(to: podspecFile, atomically: false, encoding: .utf8)
  141. } catch {
  142. fatalError("Failed to write \(podspecFile.path). \(error)")
  143. }
  144. }
  145. }
  146. // Start the parsing and run the tool.
  147. FirebaseReleaser.main()