Push.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private enum Destination {
  20. case cpdc, trunk
  21. }
  22. enum Push {
  23. static func pushPodsToCPDC(gitRoot: URL) {
  24. push(to: .cpdc, gitRoot: gitRoot)
  25. }
  26. static func publishPodsToTrunk(gitRoot: URL) {
  27. push(to: .trunk, gitRoot: gitRoot)
  28. }
  29. private static func push(to destination: Destination, gitRoot: URL) {
  30. let cpdcRepo = "sso://cpdc-internal/firebase"
  31. let cpdcLocation = findOrRegisterPrivateCocoaPodsRepo(
  32. repo: cpdcRepo,
  33. gitRoot: gitRoot,
  34. defaultRepoName: "cpdc-internal-firebase"
  35. )
  36. let stagingRepo = "git@github.com:firebase/SpecsStaging"
  37. let stagingLocation = findOrRegisterPrivateCocoaPodsRepo(
  38. repo: stagingRepo,
  39. gitRoot: gitRoot,
  40. defaultRepoName: "spec-staging"
  41. )
  42. let manifest = FirebaseManifest.shared
  43. for pod in manifest.pods.filter({ $0.releasing }) {
  44. let warningsOK = pod.allowWarnings ? "--allow-warnings" : ""
  45. let command: String = {
  46. switch destination {
  47. case .cpdc:
  48. var pushCommands = ""
  49. if pod.isClosedSource {
  50. // Push closed source pods to SpecsStaging to keep CI working.
  51. pushCommands =
  52. "pod repo push --skip-tests --use-json \(warningsOK) \(stagingLocation) " +
  53. pod.skipImportValidation() + " \(pod.podspecName()) " +
  54. "--sources=\(stagingRepo).git,https://cdn.cocoapods.org && "
  55. }
  56. pushCommands += "pod repo push --skip-tests --use-json \(warningsOK) \(cpdcLocation) " +
  57. pod.skipImportValidation() + " \(pod.podspecName()) " +
  58. "--sources=\(cpdcRepo).git,https://cdn.cocoapods.org"
  59. return pushCommands
  60. case .trunk:
  61. return "pod trunk push --skip-tests --synchronous \(warningsOK) " +
  62. pod.skipImportValidation() + " ~/.cocoapods/repos/\(cpdcLocation)/Specs/\(pod.name)/" +
  63. "\(manifest.versionString(pod))/\(pod.name).podspec.json"
  64. }
  65. }()
  66. Shell.executeCommand(command, workingDir: gitRoot)
  67. }
  68. }
  69. private static func findPrivateCocoaPodsRepo(repo: String, gitRoot: URL) -> String? {
  70. let command = "pod repo list | grep -B2 \(repo) | head -1"
  71. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  72. switch result {
  73. case let .error(code, output):
  74. fatalError("""
  75. `pod --version` failed for \(repo) with exit code \(code)
  76. Output from `pod repo list`:
  77. \(output)
  78. """)
  79. case let .success(output):
  80. print(output)
  81. let repoName = output.trimmingCharacters(in: .whitespacesAndNewlines)
  82. return repoName.isEmpty ? nil : repoName
  83. }
  84. }
  85. /// @param defaultRepoName The repo name to register if not exists
  86. private static func findOrRegisterPrivateCocoaPodsRepo(repo: String, gitRoot: URL,
  87. defaultRepoName: String) -> String {
  88. if let repoName = findPrivateCocoaPodsRepo(repo: repo, gitRoot: gitRoot) {
  89. return repoName
  90. }
  91. let command = "pod repo add \(defaultRepoName) \(repo)"
  92. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  93. switch result {
  94. case let .error(code, output):
  95. fatalError("""
  96. `pod --version` failed for \(repo) with exit code \(code)
  97. Output from `pod repo list`:
  98. \(output)
  99. """)
  100. case .success:
  101. return defaultRepoName
  102. }
  103. }
  104. }