If you want to generate warnings (or even errors) in your Xcode project if your developers accidentally use the string-based Apple APIs instead of the SwiftGen generated constants in your code, you can leverage SwiftLint to do that for you.
SwiftLint allows you to add custom rules to your .swiftlint.yml config file, based on Regular Expressions.
You can use those to detect uses of things like UIColor(red:green:blue:alpha:) and suggest to use Assets.xxx instead, or detect uses of NSLocalizedString and suggest to replace them with the L10n generated constants, etc.
Here's an example of custom rules you can add to your .swiftlint.yml SwiftLint configuration file:
custom_rules:
swiftgen_assets:
name: "SwiftGen Assets"
regex: '(UIImage|UIColor)(\.init)?\(named: ?"?.+"?(, ?in:.+?, ?compatibleWith:.+?)?\)|#imageLiteral\(resourceName: ?".+"\)'
message: "Use Asset.<asset> instead"
severity: error
swiftgen_colors:
name: "SwiftGen Colors"
regex: '(UIColor(\.init)?|#colorLiteral)\(((red|displayP3Red):.+?,green:.+?,blue:.+?,alpha:.+?)|(white:.+?,alpha:.+?)|(hue:.+?,saturation:.+?,brightness:.+?,alpha:.+?)\)'
message: "Use ColorName.<color> instead"
severity: error
swiftgen_files:
name: "SwiftGen Files"
regex: '(url\(forResource: ?"?.+"?, ?withExtension: ?"?.+"?, ?subdirectory: ?"?.+"?(, ?localization: ?"?.+"?)?\))|(path\(forResource: ?"?.+"?, ?ofType: ?"?.+"?(, ?inDirectory: ?"?.+"?(, ?forLocalization: ?"?.+"?)?)?\))'
message: "Use Files.<dir/file> instead"
severity: error
swiftgen_fonts:
name: "SwiftGen Fonts"
regex: 'UIFont(\.init)?\(name: ?"?.+"?, ?size:.+?\)'
message: "Use FontFamily.<family>.<variant>.size(<size>) instead"
severity: error
swiftgen_storyboards:
name: "SwiftGen Storyboard Scenes"
regex: '(UIStoryboard\(name: ?"?.+"?, ?bundle:.+\))|(instantiateViewController\(withIdentifier:.+?\))|(instantiateInitialViewController\(\))'
message: "Use StoryboardScene.<storyboard>.<scene>.instantiate() instead"
severity: error
swiftgen_strings:
name: "SwiftGen Strings"
regex: 'NSLocalizedString'
message: "Use L10n.key instead"
severity: errorFeel free to use this and adapt to your needs, especially:
- This example makes SwiftLint generate errors when it finds an usage of the Apple API where a SwiftGen constant should be used instead. Maybe you prefer to be less strict about it and use
severity: warningrather than error in your project - The
messagethis example configuration displays mention the name of theenumsgenerated that the user should use. If you've customized the name of such enum (typically withparams: { "enumName": "somethingElse" }in yourswiftgen.ymlconfig file), you may want to adapt the messages reported by SwiftLint accordingly.
This example configuration is only an example/suggestion. It may not be perfect, we haven't tested all the use cases, so maybe the regular expressions will have to be tuned/improved if you find a false positive or a use case that those didn't catch. If that's the case, don't hesitate to contribute by at least commenting on SwiftGen#446 to help us improve this!