SwiftGen 项目教程
1. 项目的目录结构及介绍
SwiftGen 项目的目录结构如下:
swift-gen/
├── Sources/
│ ├── SwiftGen/
│ │ ├── Commands/
│ │ ├── Config/
│ │ ├── Parsers/
│ │ ├── Templates/
│ │ ├── Utils/
│ │ └── main.swift
│ ├── SwiftGenKit/
│ │ ├── Parsers/
│ │ ├── Templates/
│ │ └── Utils/
│ └── StencilSwiftKit/
│ ├── Extensions/
│ ├── Filters/
│ └── Nodes/
├── Tests/
│ ├── SwiftGenTests/
│ ├── SwiftGenKitTests/
│ └── StencilSwiftKitTests/
├── Package.swift
├── README.md
├── LICENSE
└── Rakefile
目录介绍
- Sources/: 包含项目的所有源代码。
- SwiftGen/: 包含 SwiftGen 的主要代码。
- Commands/: 包含各种命令的实现。
- Config/: 包含配置文件相关的代码。
- Parsers/: 包含各种资源解析器的实现。
- Templates/: 包含生成代码的模板。
- Utils/: 包含各种工具类和辅助函数。
- main.swift: 项目的启动文件。
- SwiftGenKit/: 包含 SwiftGen 的核心功能代码。
- Parsers/: 包含核心解析器的实现。
- Templates/: 包含核心模板的实现。
- Utils/: 包含核心工具类和辅助函数。
- StencilSwiftKit/: 包含 Stencil 模板引擎的扩展。
- Extensions/: 包含 Stencil 的扩展。
- Filters/: 包含 Stencil 的过滤器。
- Nodes/: 包含 Stencil 的节点。
- SwiftGen/: 包含 SwiftGen 的主要代码。
- Tests/: 包含项目的所有测试代码。
- SwiftGenTests/: 包含 SwiftGen 的测试代码。
- SwiftGenKitTests/: 包含 SwiftGenKit 的测试代码。
- StencilSwiftKitTests/: 包含 StencilSwiftKit 的测试代码。
- Package.swift: Swift 包管理文件。
- README.md: 项目说明文档。
- LICENSE: 项目许可证。
- Rakefile: 项目构建脚本。
2. 项目的启动文件介绍
项目的启动文件是 Sources/SwiftGen/main.swift
。这个文件是 SwiftGen 的入口点,负责解析命令行参数并调用相应的命令。
main.swift 主要功能
- 解析命令行参数。
- 根据参数调用相应的命令。
- 初始化配置文件。
- 调用解析器和模板生成器。
3. 项目的配置文件介绍
SwiftGen 使用 YAML 格式的配置文件来定义各种解析器和它们的参数。配置文件的默认名称是 swiftgen.yml
。
配置文件示例
input_dir: Resources
output_dir: Generated
strings:
paths: strings.xml
template: swift4
output: Strings.swift
fonts:
paths: Fonts
template: swift4
output: Fonts.swift
xcassets:
paths: Assets.xcassets
template: swift4
output: Assets.swift
plists:
paths: Info.plist
template: swift4
output: Plist.swift
配置文件主要内容
- input_dir: 输入文件的目录。
- output_dir: 输出文件的目录。
- strings: 本地化字符串的配置。
- paths: 输入文件路径。
- template: 使用的模板。
- output: 输出文件路径。
- fonts: 字体的配置。
- paths: 输入文件路径。
- template: 使用的模板。
- output: 输出文件路径。
- xcassets: 资源目录的配置。
- paths: 输入文件路径。
- template: 使用的模板。
- output: 输出文件路径。
- plists: Plist 文件的配置