SPMGen

Swift 代码生成器。

安装

Homebrew

brew install edudo-inc/formulae/spmgen

Makefile

# Download repo
git clone https://github.com/edudo-inc/spmgen.git

# Navigate to repo directory
cd spmgen

# Build and install using Make
make install

# You can also delete spmgen using `make uninstall` command

资源命令

SPMGen 为各种资源类型提供静态资源工厂。

支持的资源

资源 扩展名 是否可靠
ColorResource(颜色资源) .xcassets 是 (true)
FontResource(字体资源) .ttf .otf 是 (true)
ImageResource(图片资源) .xcassets 是 (true)
NibResource .xib 未使用
StoryboardResource .storyboard 未使用
SCNSceneResource .scnassets/.scn 是 (true)

集成

添加 SPMGen 依赖到您的包

.package(url: "https://github.com/edudo-inc/spmgen.git", from: "1.0.1")

创建具有以下结构的 <#Project#>Resources 目标

Sources
  <#Project#>Resources
    Resources
      <#Assets#>

指定资源处理并添加 SPMResources 依赖到您的目标

.target(
  name: "<#Project#>Resources",
  dependencies: [
    .product(
      name: "SPMResources",
      package: "spmgen"
    )
  ],
  resources: [
    .process("Resources")
  ]
)

添加脚本到您的 Run Script 目标构建阶段

spmgen resources "$SRCROOT/Sources/<#Project#>Resources/Resources" \
  --output "$SRCROOT/Sources/<#Project#>Resources/SPMGen.swift" \
  --indentor " " \
  --indentation-width 2

# You can also add `--disable-exports` flag to disable `@_exported` attribute
# for `import SPMResources` declaration in generated file

添加 <#Project#>Resources 目标作为其他目标的依赖

.target(
  name: "<#Project#>Module",
  dependencies: [
    .target(name: "<#Project#>Resources")
  ]
)

用法

导入您的 <#Project#>Resources 包并使用 .resource() 静态工厂初始化对象

import <#Project#>Resources
import UIKit

let label = UILabel()
label.backgroundColor = .resource(.accentColor)
label.textColor = .resource(.primaryText)
label.font = .primary(ofSize: 12, weight: .semibold, style: .italic)

let imageView = UIImageView(image: .resource(.logo))

注意: 字体需要额外的设置

例如,您想添加具有不同样式的 MonsterratArimo 字体

CasePaths 命令

使用以下命令为项目中的所有枚举生成 CasePaths

spmgen casepaths "<path_to_sources>" \
  --indentor " " \
  --indentation-width 2

待办: 支持带有排除路径和基于类型名称的排除项的配置文件。