Génération de composant SwiftUI: Color et Image
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
This commit is contained in:
@ -22,6 +22,7 @@ struct Colors: ParsableCommand {
|
||||
|
||||
static let toolName = "Color"
|
||||
static let defaultExtensionName = "UIColor"
|
||||
static let defaultExtensionNameSUI = "Color"
|
||||
static let assetsColorsFolderName = "Colors"
|
||||
|
||||
// MARK: - Command options
|
||||
@ -56,6 +57,13 @@ struct Colors: ParsableCommand {
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionName,
|
||||
extensionFilePath: options.extensionFilePath)
|
||||
|
||||
// Generate extension
|
||||
ColorExtensionGenerator.writeSUIExtensionFile(colors: parsedColors,
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionNameSwiftUI,
|
||||
extensionFilePath: options.extensionFilePathSwiftUI)
|
||||
|
||||
// -> Time: 0.0010340213775634766 seconds
|
||||
|
||||
print("[\(Self.toolName)] Colors generated")
|
||||
@ -80,6 +88,13 @@ struct Colors: ParsableCommand {
|
||||
Colors.exit(withError: error)
|
||||
}
|
||||
|
||||
// Extension for UIKit and SwiftUI should have different name
|
||||
guard options.extensionName != options.extensionNameSwiftUI else {
|
||||
let error = ColorsToolError.extensionNamesCollision(options.extensionName)
|
||||
print(error.localizedDescription)
|
||||
Colors.exit(withError: error)
|
||||
}
|
||||
|
||||
// Check if needed to regenerate
|
||||
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration,
|
||||
inputFilePath: options.inputFile,
|
||||
|
@ -8,6 +8,7 @@
|
||||
import Foundation
|
||||
|
||||
enum ColorsToolError: Error {
|
||||
case extensionNamesCollision(String)
|
||||
case badFormat(String)
|
||||
case writeAsset(String)
|
||||
case createAssetFolder(String)
|
||||
@ -18,6 +19,9 @@ enum ColorsToolError: Error {
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .extensionNamesCollision(let extensionName):
|
||||
return "error:[\(Fonts.toolName)] Error on extension names, extension name and SwiftUI extension name should be different (\(extensionName) is used on both)"
|
||||
|
||||
case .badFormat(let info):
|
||||
return "error:[\(Colors.toolName)] Bad line format: \(info). Accepted format are: colorName=\"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\" \"#RGB/#ARGB\""
|
||||
|
||||
|
@ -30,6 +30,9 @@ struct ColorsToolOptions: ParsableArguments {
|
||||
@Option(help: "Extension name. If not specified, it will generate an UIColor extension. Using default extension name will generate static property.")
|
||||
var extensionName: String = Colors.defaultExtensionName
|
||||
|
||||
@Option(help: "SwiftUI Extension name. If not specified, it will generate an Color extension. Using default extension name will generate static property.")
|
||||
var extensionNameSwiftUI: String = Colors.defaultExtensionNameSUI
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")
|
||||
var extensionSuffix: String?
|
||||
}
|
||||
@ -41,6 +44,8 @@ extension ColorsToolOptions {
|
||||
ColorStyle(rawValue: style) ?? .all
|
||||
}
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
var extensionFileName: String {
|
||||
if let extensionSuffix = extensionSuffix {
|
||||
return "\(extensionName)+\(extensionSuffix).swift"
|
||||
@ -51,4 +56,17 @@ extension ColorsToolOptions {
|
||||
var extensionFilePath: String {
|
||||
"\(extensionOutputPath)/\(extensionFileName)"
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
var extensionFileNameSwiftUI: String {
|
||||
if let extensionSuffix = extensionSuffix {
|
||||
return "\(extensionNameSwiftUI)+\(extensionSuffix).swift"
|
||||
}
|
||||
return "\(extensionNameSwiftUI).swift"
|
||||
}
|
||||
|
||||
var extensionFilePathSwiftUI: String {
|
||||
"\(extensionOutputPath)/\(extensionFileNameSwiftUI)"
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ struct ColorExtensionGenerator {
|
||||
let colors: [ParsedColor]
|
||||
let extensionClassname: String
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
static func writeExtensionFile(colors: [ParsedColor], staticVar: Bool, extensionName: String, extensionFilePath: String) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getExtensionContent(colors: colors,
|
||||
@ -64,4 +66,58 @@ struct ColorExtensionGenerator {
|
||||
}
|
||||
.joined(separator: "\n\n")
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
static func writeSUIExtensionFile(colors: [ParsedColor], staticVar: Bool, extensionName: String, extensionFilePath: String) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getSUIExtensionContent(colors: colors,
|
||||
staticVar: staticVar,
|
||||
extensionName: extensionName)
|
||||
|
||||
// Write content
|
||||
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
||||
do {
|
||||
try extensionContent.write(to: extensionFilePathURL, atomically: false, encoding: .utf8)
|
||||
} catch (let error) {
|
||||
let error = ColorsToolError.writeExtension(extensionFilePath, error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
Colors.exit(withError: error)
|
||||
}
|
||||
}
|
||||
|
||||
static func getSUIExtensionContent(colors: [ParsedColor], staticVar: Bool, extensionName: String) -> String {
|
||||
[
|
||||
Self.getSUIHeader(extensionClassname: extensionName),
|
||||
Self.getSUIProperties(for: colors, withStaticVar: staticVar),
|
||||
Self.getSUIFooter()
|
||||
]
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
||||
private static func getSUIHeader(extensionClassname: String) -> String {
|
||||
"""
|
||||
// Generated by ResgenSwift.\(Colors.toolName) \(ResgenSwiftVersion)
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension \(extensionClassname) {\n
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getSUIFooter() -> String {
|
||||
"""
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getSUIProperties(for colors: [ParsedColor], withStaticVar staticVar: Bool) -> String {
|
||||
colors.map {
|
||||
if staticVar {
|
||||
return $0.getSUIColorStaticProperty()
|
||||
}
|
||||
return $0.getSUIColorProperty()
|
||||
}
|
||||
.joined(separator: "\n\n")
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,8 @@ struct ParsedColor {
|
||||
"""
|
||||
}
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
func getColorProperty() -> String {
|
||||
"""
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
@ -89,4 +91,24 @@ struct ParsedColor {
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
func getSUIColorProperty() -> String {
|
||||
"""
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
var \(name): Color {
|
||||
Color("\(name)")
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
func getSUIColorStaticProperty() -> String {
|
||||
"""
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
static var \(name): Color {
|
||||
Color("\(name)")!
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user