Push.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 staging, trunk
  21. }
  22. enum Push {
  23. static func pushPodsToStaging(gitRoot: URL) {
  24. push(to: .staging, 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 stagingRepo = "git@github.com:firebase/SpecsStaging"
  31. let stagingLocation = findOrRegisterPrivateCocoaPodsRepo(
  32. repo: stagingRepo,
  33. gitRoot: gitRoot,
  34. defaultRepoName: "spec-staging"
  35. )
  36. let manifest = FirebaseManifest.shared
  37. for pod in manifest.pods.filter({ $0.releasing }) {
  38. let warningsOK = pod.allowWarnings ? "--allow-warnings" : ""
  39. let command: String = {
  40. switch destination {
  41. case .staging:
  42. return "pod repo push --skip-tests --use-json \(warningsOK) \(stagingLocation) " +
  43. "--skip-import-validation" + " \(pod.podspecName()) " +
  44. "--sources=\(stagingRepo).git,https://cdn.cocoapods.org"
  45. case .trunk:
  46. return "pod trunk push --skip-tests --synchronous \(warningsOK) " +
  47. "--skip-import-validation" + " ~/.cocoapods/repos/\(stagingLocation)/\(pod.name)/" +
  48. "\(manifest.versionString(pod))/\(pod.name).podspec.json"
  49. }
  50. }()
  51. Shell.executeCommand(command, workingDir: gitRoot)
  52. }
  53. }
  54. private static func findPrivateCocoaPodsRepo(repo: String, gitRoot: URL) -> String? {
  55. let command = "pod repo list | grep -B2 \(repo) | head -1"
  56. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  57. switch result {
  58. case let .error(code, output):
  59. fatalError("""
  60. `pod --version` failed for \(repo) with exit code \(code)
  61. Output from `pod repo list`:
  62. \(output)
  63. """)
  64. case let .success(output):
  65. print(output)
  66. let repoName = output.trimmingCharacters(in: .whitespacesAndNewlines)
  67. return repoName.isEmpty ? nil : repoName
  68. }
  69. }
  70. /// @param defaultRepoName The repo name to register if not exists
  71. private static func findOrRegisterPrivateCocoaPodsRepo(repo: String, gitRoot: URL,
  72. defaultRepoName: String) -> String {
  73. if let repoName = findPrivateCocoaPodsRepo(repo: repo, gitRoot: gitRoot) {
  74. return repoName
  75. }
  76. let command = "pod repo add \(defaultRepoName) \(repo)"
  77. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  78. switch result {
  79. case let .error(code, output):
  80. fatalError("""
  81. `pod --version` failed for \(repo) with exit code \(code)
  82. Output from `pod repo list`:
  83. \(output)
  84. """)
  85. case .success:
  86. return defaultRepoName
  87. }
  88. }
  89. }