Push.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. pod.skipImportValidation() + " \(pod.podspecName()) " +
  44. "--sources=\(stagingRepo).git,https://cdn.cocoapods.org"
  45. case .trunk:
  46. return "pod trunk push --skip-tests --synchronous \(warningsOK) " +
  47. pod
  48. .skipImportValidation() + " ~/.cocoapods/repos/\(stagingLocation)/\(pod.name)/" +
  49. "\(manifest.versionString(pod))/\(pod.name).podspec.json"
  50. }
  51. }()
  52. Shell.executeCommand(command, workingDir: gitRoot)
  53. }
  54. }
  55. private static func findPrivateCocoaPodsRepo(repo: String, gitRoot: URL) -> String? {
  56. let command = "pod repo list | grep -B2 \(repo) | head -1"
  57. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  58. switch result {
  59. case let .error(code, output):
  60. fatalError("""
  61. `pod --version` failed for \(repo) with exit code \(code)
  62. Output from `pod repo list`:
  63. \(output)
  64. """)
  65. case let .success(output):
  66. print(output)
  67. let repoName = output.trimmingCharacters(in: .whitespacesAndNewlines)
  68. return repoName.isEmpty ? nil : repoName
  69. }
  70. }
  71. /// @param defaultRepoName The repo name to register if not exists
  72. private static func findOrRegisterPrivateCocoaPodsRepo(repo: String, gitRoot: URL,
  73. defaultRepoName: String) -> String {
  74. if let repoName = findPrivateCocoaPodsRepo(repo: repo, gitRoot: gitRoot) {
  75. return repoName
  76. }
  77. let command = "pod repo add \(defaultRepoName) \(repo)"
  78. let result = Shell.executeCommandFromScript(command, workingDir: gitRoot)
  79. switch result {
  80. case let .error(code, output):
  81. fatalError("""
  82. `pod --version` failed for \(repo) with exit code \(code)
  83. Output from `pod repo list`:
  84. \(output)
  85. """)
  86. case .success:
  87. return defaultRepoName
  88. }
  89. }
  90. }