GenerationConfig.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  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. /// The config used when generating code whether called from the build or command plugin.
  17. struct GenerationConfig {
  18. /// The access level (i.e. visibility) of the generated files.
  19. enum AccessLevel: String {
  20. /// The generated files should have `internal` access level.
  21. case `internal` = "Internal"
  22. /// The generated files should have `public` access level.
  23. case `public` = "Public"
  24. /// The generated files should have `package` access level.
  25. case `package` = "Package"
  26. }
  27. /// The naming of output files with respect to the path of the source file.
  28. ///
  29. /// For an input of `foo/bar/baz.proto` the following output file will be generated:
  30. /// - `FullPath`: `foo/bar/baz.grpc.swift`
  31. /// - `PathToUnderscore`: `foo_bar_baz.grpc.swift`
  32. /// - `DropPath`: `baz.grpc.swift`
  33. enum FileNaming: String {
  34. /// Replicate the input file path with the output file(s).
  35. case fullPath = "FullPath"
  36. /// Convert path directory delimiters to underscores.
  37. case pathToUnderscores = "PathToUnderscores"
  38. /// Generate output files using only the base name of the inout file, ignoring the path.
  39. case dropPath = "DropPath"
  40. }
  41. /// The visibility of the generated files.
  42. var accessLevel: AccessLevel
  43. /// Whether server code is generated.
  44. var servers: Bool
  45. /// Whether client code is generated.
  46. var clients: Bool
  47. /// Whether message code is generated.
  48. var messages: Bool
  49. /// The naming of output files with respect to the path of the source file.
  50. var fileNaming: FileNaming
  51. /// Whether imports should have explicit access levels.
  52. var accessLevelOnImports: Bool
  53. /// Specify the directory in which to search for imports.
  54. ///
  55. /// May be specified multiple times; directories will be searched in order.
  56. /// The target source directory is always appended to the import paths.
  57. var importPaths: [String]
  58. /// The path to the `protoc` binary.
  59. ///
  60. /// If this is not set, Swift Package Manager will try to find the tool itself.
  61. var protocPath: String?
  62. /// The path into which the generated source files are created.
  63. var outputPath: String
  64. }
  65. extension GenerationConfig.AccessLevel: Codable {
  66. init?(rawValue: String) {
  67. switch rawValue.lowercased() {
  68. case "internal":
  69. self = .internal
  70. case "public":
  71. self = .public
  72. case "package":
  73. self = .package
  74. default:
  75. return nil
  76. }
  77. }
  78. }
  79. extension GenerationConfig.FileNaming: Codable {
  80. init?(rawValue: String) {
  81. switch rawValue.lowercased() {
  82. case "fullpath":
  83. self = .fullPath
  84. case "pathtounderscores":
  85. self = .pathToUnderscores
  86. case "droppath":
  87. self = .dropPath
  88. default:
  89. return nil
  90. }
  91. }
  92. }