InitializeSource.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2021 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 Constants {}
  20. extension Constants {
  21. static let localSpecRepoName = "specstesting"
  22. static let specRepo = "https://github.com/firebase/SpecsTesting"
  23. static let sdkRepo = "https://github.com/firebase/firebase-ios-sdk"
  24. static let testingTagPrefix = "testing-"
  25. }
  26. struct InitializeSpecTesting {
  27. static func setupRepo(sdkRepoURL: URL) {
  28. let manifest = FirebaseManifest.shared
  29. addSpecRepo(repoURL: Constants.specRepo)
  30. addTestingTag(path: sdkRepoURL, manifest: manifest)
  31. updatePodspecs(path: sdkRepoURL, manifest: manifest)
  32. }
  33. // The SpecsTesting repo will be added to `${HOME}/.cocoapods/`, and all
  34. // podspecs under this dir will be the source of the specs testing.
  35. private static func addSpecRepo(repoURL: String,
  36. podRepoName: String = Constants.localSpecRepoName) {
  37. let result = Shell.executeCommandFromScript("pod repo remove \(podRepoName)")
  38. switch result {
  39. case let .error(code, output):
  40. print("\(podRepoName) was not properly removed. \(podRepoName) probably" +
  41. "does not exist in local.\n \(output)")
  42. case let .success(output):
  43. print("\(podRepoName) was removed.")
  44. }
  45. Shell.executeCommand("pod repo add \(podRepoName) \(repoURL)")
  46. }
  47. // Add a testing tag to the head of the branch.
  48. private static func addTestingTag(path sdkRepoPath: URL, manifest: FirebaseManifest.Manifest) {
  49. let manifest = FirebaseManifest.shared
  50. let testingTag = Constants.testingTagPrefix + manifest.version
  51. // Add or update the testing tag to the local sdk repo.
  52. Shell.executeCommand("git tag -af \(testingTag) -m 'spectesting'", workingDir: sdkRepoPath)
  53. }
  54. // Update the podspec source.
  55. private static func updatePodspecs(path: URL, manifest: FirebaseManifest.Manifest) {
  56. for pod in manifest.pods {
  57. let version = manifest.versionString(pod)
  58. if !pod.isClosedSource {
  59. // Replace git and tag in the source of a podspec.
  60. // Before:
  61. // s.source = {
  62. // :git => 'https://github.com/firebase/firebase-ios-sdk.git',
  63. // :tag => 'CocoaPods-' + s.version.to_s
  64. // }
  65. // After `sed`:
  66. // s.source = {
  67. // :git => '\(path.path)',
  68. // :tag => 'testing-\(manifest.version)',
  69. // }
  70. Shell.executeCommand(
  71. "sed -i.bak -e \"s|\\(.*\\:git =>[[:space:]]*\\).*|\\1'\(path.path)',| ; " +
  72. "s|\\(.*\\:tag =>[[:space:]]*\\).*|\\1'\(Constants.testingTagPrefix + manifest.version)',|\" \(pod.name).podspec",
  73. workingDir: path
  74. )
  75. }
  76. }
  77. }
  78. }