main.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. Tags.updateTags(gitRoot: gitRoot)
  76. } else if pushOnly {
  77. Push.pushPodsToStaging(gitRoot: gitRoot)
  78. } else if publish {
  79. Push.publishPodsToTrunk(gitRoot: gitRoot)
  80. }
  81. let finishDate = Date()
  82. print("Finished at: \(finishDate.dateTimeString()). " +
  83. "Duration: \(startDate.formattedDurationSince(finishDate))")
  84. }
  85. private func updateFirebasePod(newVersions: [String: String]) {
  86. let podspecFile = gitRoot.appendingPathComponent("Firebase.podspec")
  87. var contents = ""
  88. do {
  89. contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8)
  90. } catch {
  91. fatalError("Could not read Firebase podspec. \(error)")
  92. }
  93. for (pod, version) in newVersions {
  94. if pod == "Firebase" {
  95. // Replace version in string like s.version = '6.9.0'
  96. guard let range = contents.range(of: "s.version") else {
  97. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  98. }
  99. var versionStartIndex = contents.index(range.upperBound, offsetBy: 1)
  100. while contents[versionStartIndex] != "'" {
  101. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  102. }
  103. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  104. while contents[versionEndIndex] != "'" {
  105. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  106. }
  107. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  108. contents.insert(contentsOf: "'" + version + "'", at: versionStartIndex)
  109. } else {
  110. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'
  111. guard let range = contents.range(of: pod) else {
  112. // This pod is not a top-level Firebase pod dependency.
  113. continue
  114. }
  115. var versionStartIndex = contents.index(range.upperBound, offsetBy: 2)
  116. while !contents[versionStartIndex].isWholeNumber {
  117. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  118. }
  119. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  120. while contents[versionEndIndex] != "'" {
  121. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  122. }
  123. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  124. contents.insert(contentsOf: version + "'", at: versionStartIndex)
  125. }
  126. }
  127. do {
  128. try contents.write(to: podspecFile, atomically: false, encoding: .utf8)
  129. } catch {
  130. fatalError("Failed to write \(podspecFile.path). \(error)")
  131. }
  132. }
  133. }
  134. // Start the parsing and run the tool.
  135. FirebaseReleaser.main()