main.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. enum ParsingMode: String, EnumerableFlag {
  21. case forNoticesGeneration
  22. case forGHAMatrixGeneration
  23. }
  24. struct ManifestParser: ParsableCommand {
  25. @Option(help: "The path of the SDK repo.",
  26. transform: { str in
  27. if NSString(string: str).isAbsolutePath { return URL(fileURLWithPath: str) }
  28. let documentDir = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
  29. return documentDir.appendingPathComponent(str)
  30. })
  31. var SDKRepoURL: URL?
  32. /// Path of a text file for Firebase Pods' names.
  33. @Option(help: "An output file with Podspecs",
  34. transform: { str in
  35. if NSString(string: str).isAbsolutePath { return URL(fileURLWithPath: str) }
  36. let documentDir = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
  37. return documentDir.appendingPathComponent(str)
  38. })
  39. var outputFilePath: URL
  40. @Flag(help: "Parsing mode for manifest")
  41. var mode: ParsingMode
  42. func parsePodNames(_ manifest: Manifest) throws {
  43. var output: [String] = []
  44. for pod in manifest.pods.filter({ $0.releasing }) {
  45. output.append(pod.name)
  46. }
  47. do {
  48. try output.joined(separator: ",")
  49. .write(to: outputFilePath, atomically: true,
  50. encoding: String.Encoding.utf8)
  51. print("\(output) is written in \n \(outputFilePath).")
  52. } catch {
  53. throw error
  54. }
  55. }
  56. func run() throws {
  57. switch mode {
  58. case .forNoticesGeneration:
  59. try parsePodNames(FirebaseManifest.shared)
  60. case .forGHAMatrixGeneration:
  61. guard let sdkRepoURL = SDKRepoURL else {
  62. fatalError("--sdk-repo-url should be specified when --for-gha-matrix-generation is on.")
  63. }
  64. let specCollector = GHAMatrixSpecCollector(
  65. SDKRepoURL: sdkRepoURL,
  66. outputSpecFileURL: outputFilePath
  67. )
  68. try specCollector.generateMatrixJson(to: outputFilePath)
  69. }
  70. }
  71. }
  72. // Start the parsing and run the tool.
  73. ManifestParser.main()