main.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 to output the commands to generate the ordered `pod trunk push` commands.
  36. @Option(default: false,
  37. help: "Publish the podspecs to the CocoaPodsTrunk")
  38. var publish: Bool
  39. /// Set this option to only update the podspecs on SpecsStaging.
  40. @Option(default: false,
  41. help: "Update the podspecs only")
  42. var pushOnly: Bool
  43. /// Set this option to update tags only.
  44. @Option(default: false,
  45. help: "Update the tags only")
  46. var updateTagsOnly: Bool
  47. mutating func validate() throws {
  48. guard FileManager.default.fileExists(atPath: gitRoot.path) else {
  49. throw ValidationError("git-root does not exist: \(gitRoot.path)")
  50. }
  51. }
  52. func run() throws {
  53. let startDate = Date()
  54. print("Started at: \(startDate.dateTimeString())")
  55. if logOnly {
  56. Shell.setLogOnly()
  57. }
  58. if initBranch {
  59. let branch = InitializeRelease.setupRepo(gitRoot: gitRoot)
  60. let version = FirebaseManifest.shared.version
  61. Shell.executeCommand("git commit -am \"Update versions for Release \(version)\"",
  62. workingDir: gitRoot)
  63. Shell.executeCommand("git push origin \(branch)", workingDir: gitRoot)
  64. Shell.executeCommand("git branch --set-upstream-to=origin/\(branch) \(branch)",
  65. workingDir: gitRoot)
  66. Tags.createTags(gitRoot: gitRoot)
  67. Push.pushPodsToStaging(gitRoot: gitRoot)
  68. } else if updateTagsOnly {
  69. Tags.updateTags(gitRoot: gitRoot)
  70. } else if pushOnly {
  71. Push.pushPodsToStaging(gitRoot: gitRoot)
  72. } else if publish {
  73. Push.publishPodsToTrunk(gitRoot: gitRoot)
  74. }
  75. let finishDate = Date()
  76. print("Finished at: \(finishDate.dateTimeString()). " +
  77. "Duration: \(startDate.formattedDurationSince(finishDate))")
  78. }
  79. private func updateFirebasePod(newVersions: [String: String]) {
  80. let podspecFile = gitRoot.appendingPathComponent("Firebase.podspec")
  81. var contents = ""
  82. do {
  83. contents = try String(contentsOfFile: podspecFile.path, encoding: .utf8)
  84. } catch {
  85. fatalError("Could not read Firebase podspec. \(error)")
  86. }
  87. for (pod, version) in newVersions {
  88. if pod == "Firebase" {
  89. // Replace version in string like s.version = '6.9.0'
  90. guard let range = contents.range(of: "s.version") else {
  91. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  92. }
  93. var versionStartIndex = contents.index(range.upperBound, offsetBy: 1)
  94. while contents[versionStartIndex] != "'" {
  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. } else {
  104. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'
  105. guard let range = contents.range(of: pod) else {
  106. // This pod is not a top-level Firebase pod dependency.
  107. continue
  108. }
  109. var versionStartIndex = contents.index(range.upperBound, offsetBy: 2)
  110. while !contents[versionStartIndex].isWholeNumber {
  111. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  112. }
  113. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  114. while contents[versionEndIndex] != "'" {
  115. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  116. }
  117. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  118. contents.insert(contentsOf: version + "'", at: versionStartIndex)
  119. }
  120. }
  121. do {
  122. try contents.write(to: podspecFile, atomically: false, encoding: .utf8)
  123. } catch {
  124. fatalError("Failed to write \(podspecFile.path). \(error)")
  125. }
  126. }
  127. }
  128. // Start the parsing and run the tool.
  129. FirebaseReleaser.main()