GHAMatrixSpecCollector.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2022 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 ArgumentParser
  17. import FirebaseManifest
  18. import Foundation
  19. import Utils
  20. /// SDKPodspec is to help generate an array of podspec in json file, e.g.
  21. /// ``` output.json
  22. /// [{"podspec":"FirebaseABTesting.podspec"},{"podspec":"FirebaseAnalytics.podspec"}]
  23. /// ```
  24. struct SDKPodspec: Codable {
  25. let podspec: String
  26. let allowWarnings: Bool
  27. }
  28. struct GHAMatrixSpecCollector {
  29. var SDKRepoURL: URL
  30. var outputSpecFileURL: URL
  31. var excludedSDKs: [String] = []
  32. func getPodsInManifest(_ manifest: Manifest) -> [String: SDKPodspec] {
  33. var podsMap: [String: SDKPodspec] = [:]
  34. for pod in manifest.pods {
  35. podsMap[pod.name] = SDKPodspec(podspec: pod.name, allowWarnings: pod.allowWarnings)
  36. }
  37. return podsMap
  38. }
  39. func getAllPodspecs() -> [SDKPodspec] {
  40. var output: [SDKPodspec] = []
  41. let fileManager = FileManager.default
  42. let podsMap = getPodsInManifest(FirebaseManifest.shared)
  43. do {
  44. let fileURLs = try fileManager.contentsOfDirectory(
  45. at: SDKRepoURL,
  46. includingPropertiesForKeys: nil
  47. )
  48. for url in fileURLs {
  49. let fileNameComponents = url.lastPathComponent.components(separatedBy: ".")
  50. if fileNameComponents.count > 1, fileNameComponents[1] == "podspec" {
  51. let specName = fileNameComponents[0]
  52. if let spec = podsMap[specName] {
  53. output.append(spec)
  54. } else {
  55. print("\(specName) is not in manifiest")
  56. }
  57. }
  58. }
  59. } catch {
  60. print("Error while enumerating files: \(error.localizedDescription)")
  61. }
  62. return output
  63. }
  64. func generateMatrixJson(to filePath: URL) throws {
  65. let sdkPodspecs: [SDKPodspec] = getAllPodspecs()
  66. // Trim whitespaces so the GitHub Actions matrix can read.
  67. let str = try String(
  68. decoding: JSONEncoder().encode(sdkPodspecs),
  69. as: UTF8.self
  70. )
  71. try str.trimmingCharacters(in: .whitespacesAndNewlines).write(
  72. to: filePath,
  73. atomically: true,
  74. encoding: String.Encoding.utf8
  75. )
  76. }
  77. }