Build tool plugin that auto generates constants from assets catalogs so that they can be referenced in a type-safe way.
Add the following dependency to your package:
dependencies: [
.package(url: "https://github.com/nodes-ios/AssetsConstantsGenerator", from: "0.0.1")
]
and then add the plugin to the targets you wish to generate assets constants. Example:
.target(
name: "Assets",
dependencies: [],
plugins: [
plugin(name: "generate-constants", package: "AssetsConstansGenerator")
]
),
Now every time the targets get built it auto generates a similar file to the one above in the build directory.
import UIKit
#if canImport(SwiftUI)
import SwiftUI
#endif
@available(iOS 11.0, *)
extension UIImage {
public enum Pets {
public static let anne = UIImage(named: "anne", in: .module, compatibleWith: nil)!
public enum Cats {
public static let grey = UIImage(named: "grey", in: .module, compatibleWith: nil)!
public static let sueAndSally = UIImage(named: "sueAndSally", in: .module, compatibleWith: nil)!
public static let tommy = UIImage(named: "tommy", in: .module, compatibleWith: nil)!
}
public enum Dogs {
public static let jay = UIImage(named: "jay", in: .module, compatibleWith: nil)!
}
public enum Pigeons {
public static let bob = UIImage(named: "bob", in: .module, compatibleWith: nil)!
}
}
}
@available(iOS 13.0, *)
extension Image {
public enum Pets {
public static let anne = Image("anne", bundle: .module)
public enum Cats {
public static let grey = Image("grey", bundle: .module)
public static let sueAndSally = Image("sueAndSally", bundle: .module)
public static let tommy = Image("tommy", bundle: .module)
}
public enum Dogs {
public static let jay = Image("jay", bundle: .module)
}
public enum Pigeons {
public static let bob = Image("bob", bundle: .module)
}
}
}
@available(iOS 11.0, *)
extension UIColor {
public enum Colors {
public static let bittersweet = UIColor(named: "bittersweet", in: .module, compatibleWith: nil)!
public static let darkLiverHorses = UIColor(named: "darkLiverHorses", in: .module, compatibleWith: nil)!
public static let davysGrey = UIColor(named: "davysGrey", in: .module, compatibleWith: nil)!
public static let persianGreet = UIColor(named: "persianGreet", in: .module, compatibleWith: nil)!
public static let white = UIColor(named: "white", in: .module, compatibleWith: nil)!
}
}
@available(iOS 13.0, *)
extension Color {
public enum Colors {
public static let bittersweet = Color("bittersweet", bundle: .module)
public static let darkLiverHorses = Color("darkLiverHorses", bundle: .module)
public static let davysGrey = Color("davysGrey", bundle: .module)
public static let persianGreet = Color("persianGreet", bundle: .module)
public static let white = Color("white", bundle: .module)
}
}
@available(iOS 11.0, *)
extension NSDataAsset {
public enum Data {
public static let userDefaults = NSDataAsset(name: "userDefaults", bundle: .module)
}
}