main.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. @Option(parsing: .upToNextOption, help: "Podspec files that will not be included.")
  41. var excludedSpecs: [String]
  42. @Flag(help: "Parsing mode for manifest")
  43. var mode: ParsingMode
  44. func parsePodNames(_ manifest: Manifest) throws {
  45. var output: [String] = []
  46. for pod in manifest.pods {
  47. output.append(pod.name)
  48. }
  49. do {
  50. try output.joined(separator: ",")
  51. .write(to: outputFilePath, atomically: true,
  52. encoding: String.Encoding.utf8)
  53. print("\(output) is written in \n \(outputFilePath).")
  54. } catch {
  55. throw error
  56. }
  57. }
  58. func run() throws {
  59. switch mode {
  60. case .forNoticesGeneration:
  61. try parsePodNames(FirebaseManifest.shared)
  62. case .forGHAMatrixGeneration:
  63. guard let sdkRepoURL = SDKRepoURL else {
  64. throw fatalError(
  65. "--sdk-repo-url should be specified when --for-gha-matrix-generation is on."
  66. )
  67. }
  68. let specCollector = GHAMatrixSpecCollector(
  69. SDKRepoURL: sdkRepoURL,
  70. outputSpecFileURL: outputFilePath
  71. )
  72. try specCollector.generateMatrixJson(to: outputFilePath)
  73. }
  74. }
  75. }
  76. // Start the parsing and run the tool.
  77. ManifestParser.main()