Handle bundle for Image and Color
Some checks reported warnings
gitea-openium/resgen.swift/pipeline/head This commit is unstable
Some checks reported warnings
gitea-openium/resgen.swift/pipeline/head This commit is unstable
This commit is contained in:
@@ -63,7 +63,8 @@ struct Colors: ParsableCommand {
|
||||
extensionName: extensionName,
|
||||
extensionFilePath: extensionFilePath,
|
||||
isSwiftUI: true,
|
||||
visibility: options.extensionVisibility
|
||||
visibility: options.extensionVisibility,
|
||||
assetBundle: options.assetBundle
|
||||
)
|
||||
}
|
||||
|
||||
@@ -76,7 +77,8 @@ struct Colors: ParsableCommand {
|
||||
extensionName: extensionNameUIKit,
|
||||
extensionFilePath: extensionFilePathUIKit,
|
||||
isSwiftUI: false,
|
||||
visibility: options.extensionVisibility
|
||||
visibility: options.extensionVisibility,
|
||||
assetBundle: options.assetBundle
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -44,6 +44,11 @@ struct ColorsToolOptions: ParsableArguments {
|
||||
)
|
||||
var extensionVisibility: ExtensionVisibility = .internal
|
||||
|
||||
@Option(
|
||||
help: "Bundle where the asset are generated"
|
||||
)
|
||||
var assetBundle: AssetBundle = .main
|
||||
|
||||
@Option(
|
||||
help: "Path where to generate the extension.",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
|
@@ -21,7 +21,8 @@ struct ColorExtensionGenerator {
|
||||
extensionName: String,
|
||||
extensionFilePath: String,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getExtensionContent(
|
||||
@@ -29,7 +30,8 @@ struct ColorExtensionGenerator {
|
||||
staticVar: staticVar,
|
||||
extensionName: extensionName,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
)
|
||||
|
||||
// Write content
|
||||
@@ -48,7 +50,8 @@ struct ColorExtensionGenerator {
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
[
|
||||
Self.getHeader(
|
||||
@@ -59,7 +62,8 @@ struct ColorExtensionGenerator {
|
||||
for: colors,
|
||||
withStaticVar: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
),
|
||||
Self.getFooter()
|
||||
]
|
||||
@@ -87,13 +91,15 @@ struct ColorExtensionGenerator {
|
||||
for colors: [ParsedColor],
|
||||
withStaticVar staticVar: Bool,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
colors.map {
|
||||
$0.getColorProperty(
|
||||
isStatic: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
)
|
||||
}
|
||||
.joined(separator: "\n\n")
|
||||
|
@@ -83,20 +83,21 @@ struct ParsedColor {
|
||||
func getColorProperty(
|
||||
isStatic: Bool,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
if isSwiftUI {
|
||||
return """
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
\(visibility) \(isStatic ? "static " : "")var \(name): Color {
|
||||
Color("\(name)")
|
||||
Color("\(name)", bundle: Bundle.\(assetBundle))
|
||||
}
|
||||
"""
|
||||
}
|
||||
return """
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
\(isStatic ? "" : "@objc ")\(visibility) \(isStatic ? "static " : "")var \(name): UIColor {
|
||||
UIColor(named: "\(name)")!
|
||||
UIColor(named: "\(name)", in: Bundle.\(assetBundle), compatibleWith: nil)!
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
@@ -19,7 +19,8 @@ enum ImageExtensionGenerator {
|
||||
extensionName: String,
|
||||
extensionFilePath: String,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) {
|
||||
// Create extension conten1t
|
||||
let extensionContent = Self.getExtensionContent(
|
||||
@@ -28,7 +29,8 @@ enum ImageExtensionGenerator {
|
||||
extensionName: extensionName,
|
||||
inputFilename: inputFilename,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
)
|
||||
|
||||
// Write content
|
||||
@@ -48,7 +50,8 @@ enum ImageExtensionGenerator {
|
||||
extensionName: String,
|
||||
inputFilename: String,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
[
|
||||
Self.getHeader(
|
||||
@@ -60,7 +63,8 @@ enum ImageExtensionGenerator {
|
||||
images: images,
|
||||
staticVar: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
),
|
||||
Self.getFooter()
|
||||
]
|
||||
@@ -86,11 +90,18 @@ enum ImageExtensionGenerator {
|
||||
images: [ParsedImage],
|
||||
staticVar: Bool,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
images
|
||||
.map {
|
||||
"\n\($0.getImageProperty(isStatic: staticVar, isSwiftUI: isSwiftUI, visibility: visibility))"
|
||||
.map { parsedImage in
|
||||
let property = parsedImage.getImageProperty(
|
||||
isStatic: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility,
|
||||
assetBundle: assetBundle
|
||||
)
|
||||
return "\n\(property)"
|
||||
}
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
@@ -62,7 +62,8 @@ struct Images: ParsableCommand {
|
||||
extensionName: extensionName,
|
||||
extensionFilePath: extensionFilePath,
|
||||
isSwiftUI: true,
|
||||
visibility: options.extensionVisibility
|
||||
visibility: options.extensionVisibility,
|
||||
assetBundle: options.assetBundle
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,7 +76,8 @@ struct Images: ParsableCommand {
|
||||
extensionName: extensionNameUIKit,
|
||||
extensionFilePath: extensionFilePathUIKit,
|
||||
isSwiftUI: false,
|
||||
visibility: options.extensionVisibility
|
||||
visibility: options.extensionVisibility,
|
||||
assetBundle: options.assetBundle
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -51,6 +51,11 @@ struct ImagesOptions: ParsableArguments {
|
||||
)
|
||||
var extensionVisibility: ExtensionVisibility = .internal
|
||||
|
||||
@Option(
|
||||
help: "Bundle where the asset are generated"
|
||||
)
|
||||
var assetBundle: AssetBundle = .main
|
||||
|
||||
@Option(
|
||||
help: "Path where to generate the extension.",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
|
@@ -130,17 +130,22 @@ struct ParsedImage {
|
||||
|
||||
// MARK: - Extension property
|
||||
|
||||
func getImageProperty(isStatic: Bool, isSwiftUI: Bool, visibility: ExtensionVisibility) -> String {
|
||||
func getImageProperty(
|
||||
isStatic: Bool,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility,
|
||||
assetBundle: AssetBundle
|
||||
) -> String {
|
||||
if isSwiftUI {
|
||||
return """
|
||||
\(visibility) \(isStatic ? "static " : "")var \(name): Image {
|
||||
Image("\(name)")
|
||||
Image("\(name)", bundle: Bundle.\(assetBundle))
|
||||
}
|
||||
"""
|
||||
}
|
||||
return """
|
||||
\(visibility) \(isStatic ? "static " : "")var \(name): UIImage {
|
||||
UIImage(named: "\(name)")!
|
||||
UIImage(named: "\(name)", in: Bundle.\(assetBundle), with: nil)!
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
@@ -26,7 +26,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
staticVar: false,
|
||||
extensionName: "GenColors",
|
||||
isSwiftUI: false,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -39,12 +40,12 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
|
||||
/// Color colorOne is #FF00FF (light) or #00FF00 (dark)"
|
||||
@objc public var colorOne: UIColor {
|
||||
UIColor(named: "colorOne")!
|
||||
UIColor(named: "colorOne", in: Bundle.main, compatibleWith: nil)!
|
||||
}
|
||||
|
||||
/// Color colorTwo is #F0F0F0 (light) or #0F0F0F (dark)"
|
||||
@objc public var colorTwo: UIColor {
|
||||
UIColor(named: "colorTwo")!
|
||||
UIColor(named: "colorTwo", in: Bundle.main, compatibleWith: nil)!
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -65,7 +66,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
staticVar: true,
|
||||
extensionName: "GenColor",
|
||||
isSwiftUI: false,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -78,12 +80,12 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
|
||||
/// Color colorOne is #FF00FF (light) or #00FF00 (dark)"
|
||||
public static var colorOne: UIColor {
|
||||
UIColor(named: "colorOne")!
|
||||
UIColor(named: "colorOne", in: Bundle.module, compatibleWith: nil)!
|
||||
}
|
||||
|
||||
/// Color colorTwo is #F0F0F0 (light) or #0F0F0F (dark)"
|
||||
public static var colorTwo: UIColor {
|
||||
UIColor(named: "colorTwo")!
|
||||
UIColor(named: "colorTwo", in: Bundle.module, compatibleWith: nil)!
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -104,7 +106,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
staticVar: false,
|
||||
extensionName: "GenColors",
|
||||
isSwiftUI: true,
|
||||
visibility: .package
|
||||
visibility: .package,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -117,12 +120,12 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
|
||||
/// Color colorOne is #FF00FF (light) or #00FF00 (dark)"
|
||||
package var colorOne: Color {
|
||||
Color("colorOne")
|
||||
Color("colorOne", bundle: Bundle.main)
|
||||
}
|
||||
|
||||
/// Color colorTwo is #F0F0F0 (light) or #0F0F0F (dark)"
|
||||
package var colorTwo: Color {
|
||||
Color("colorTwo")
|
||||
Color("colorTwo", bundle: Bundle.main)
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -143,7 +146,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
staticVar: true,
|
||||
extensionName: "GenColor",
|
||||
isSwiftUI: true,
|
||||
visibility: .internal
|
||||
visibility: .internal,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -156,12 +160,12 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
|
||||
/// Color colorOne is #FF00FF (light) or #00FF00 (dark)"
|
||||
internal static var colorOne: Color {
|
||||
Color("colorOne")
|
||||
Color("colorOne", bundle: Bundle.module)
|
||||
}
|
||||
|
||||
/// Color colorTwo is #F0F0F0 (light) or #0F0F0F (dark)"
|
||||
internal static var colorTwo: Color {
|
||||
Color("colorTwo")
|
||||
Color("colorTwo", bundle: Bundle.module)
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
@@ -20,14 +20,15 @@ final class ParsedColorTests: XCTestCase {
|
||||
let property = color.getColorProperty(
|
||||
isStatic: false,
|
||||
isSwiftUI: false,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
/// Color red is #FF0000 (light) or #0000FF (dark)"
|
||||
@objc public var red: UIColor {
|
||||
UIColor(named: "red")!
|
||||
UIColor(named: "red", in: Bundle.main, compatibleWith: nil)!
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -42,14 +43,15 @@ final class ParsedColorTests: XCTestCase {
|
||||
let property = color.getColorProperty(
|
||||
isStatic: true,
|
||||
isSwiftUI: false,
|
||||
visibility: .private
|
||||
visibility: .private,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
/// Color red is #FF0000 (light) or #0000FF (dark)"
|
||||
private static var red: UIColor {
|
||||
UIColor(named: "red")!
|
||||
UIColor(named: "red", in: Bundle.module, compatibleWith: nil)!
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -64,14 +66,15 @@ final class ParsedColorTests: XCTestCase {
|
||||
let property = color.getColorProperty(
|
||||
isStatic: false,
|
||||
isSwiftUI: true,
|
||||
visibility: .package
|
||||
visibility: .package,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
/// Color red is #FF0000 (light) or #0000FF (dark)"
|
||||
package var red: Color {
|
||||
Color("red")
|
||||
Color("red", bundle: Bundle.main)
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -86,14 +89,15 @@ final class ParsedColorTests: XCTestCase {
|
||||
let property = color.getColorProperty(
|
||||
isStatic: true,
|
||||
isSwiftUI: true,
|
||||
visibility: .internal
|
||||
visibility: .internal,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
/// Color red is #FF0000 (light) or #0000FF (dark)"
|
||||
internal static var red: Color {
|
||||
Color("red")
|
||||
Color("red", bundle: Bundle.module)
|
||||
}
|
||||
"""
|
||||
|
||||
|
@@ -27,7 +27,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: false,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -40,11 +41,11 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extension GenImages {
|
||||
|
||||
public var image_one: UIImage {
|
||||
UIImage(named: "image_one")!
|
||||
UIImage(named: "image_one", in: Bundle.main, with: nil)!
|
||||
}
|
||||
|
||||
public var image_two: UIImage {
|
||||
UIImage(named: "image_two")!
|
||||
UIImage(named: "image_two", in: Bundle.main, with: nil)!
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -66,7 +67,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: false,
|
||||
visibility: .internal
|
||||
visibility: .internal,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -79,11 +81,11 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extension GenImages {
|
||||
|
||||
internal static var image_one: UIImage {
|
||||
UIImage(named: "image_one")!
|
||||
UIImage(named: "image_one", in: Bundle.module, with: nil)!
|
||||
}
|
||||
|
||||
internal static var image_two: UIImage {
|
||||
UIImage(named: "image_two")!
|
||||
UIImage(named: "image_two", in: Bundle.module, with: nil)!
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -105,7 +107,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: true,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -118,11 +121,11 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extension GenImages {
|
||||
|
||||
public var image_one: Image {
|
||||
Image("image_one")
|
||||
Image("image_one", bundle: Bundle.main)
|
||||
}
|
||||
|
||||
public var image_two: Image {
|
||||
Image("image_two")
|
||||
Image("image_two", bundle: Bundle.main)
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -144,7 +147,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: true,
|
||||
visibility: .package
|
||||
visibility: .package,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
@@ -157,11 +161,11 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
extension GenImages {
|
||||
|
||||
package static var image_one: Image {
|
||||
Image("image_one")
|
||||
Image("image_one", bundle: Bundle.module)
|
||||
}
|
||||
|
||||
package static var image_two: Image {
|
||||
Image("image_two")
|
||||
Image("image_two", bundle: Bundle.module)
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
@@ -48,13 +48,14 @@ final class ParsedImageTests: XCTestCase {
|
||||
let property = parsedImage.getImageProperty(
|
||||
isStatic: false,
|
||||
isSwiftUI: false,
|
||||
visibility: .public
|
||||
visibility: .public,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
public var \(imageName): UIImage {
|
||||
UIImage(named: "\(imageName)")!
|
||||
UIImage(named: "\(imageName)", in: Bundle.main, with: nil)!
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -75,13 +76,14 @@ final class ParsedImageTests: XCTestCase {
|
||||
let property = parsedImage.getImageProperty(
|
||||
isStatic: true,
|
||||
isSwiftUI: false,
|
||||
visibility: .internal
|
||||
visibility: .internal,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
internal static var \(imageName): UIImage {
|
||||
UIImage(named: "\(imageName)")!
|
||||
UIImage(named: "\(imageName)", in: Bundle.module, with: nil)!
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -102,13 +104,14 @@ final class ParsedImageTests: XCTestCase {
|
||||
let property = parsedImage.getImageProperty(
|
||||
isStatic: false,
|
||||
isSwiftUI: true,
|
||||
visibility: .private
|
||||
visibility: .private,
|
||||
assetBundle: .main
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
private var \(imageName): Image {
|
||||
Image("\(imageName)")
|
||||
Image("\(imageName)", bundle: Bundle.main)
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -129,13 +132,14 @@ final class ParsedImageTests: XCTestCase {
|
||||
let property = parsedImage.getImageProperty(
|
||||
isStatic: true,
|
||||
isSwiftUI: true,
|
||||
visibility: .package
|
||||
visibility: .package,
|
||||
assetBundle: .module
|
||||
)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
package static var \(imageName): Image {
|
||||
Image("\(imageName)")
|
||||
Image("\(imageName)", bundle: Bundle.module)
|
||||
}
|
||||
"""
|
||||
|
||||
|
Reference in New Issue
Block a user