Refactor SwiftUI extension generation and generation SwiftUI extension for images
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit

This commit is contained in:
2022-11-03 15:00:27 +01:00
parent 4f7d7e18b1
commit 085f1ffc33
25 changed files with 219 additions and 264 deletions

View File

@ -56,16 +56,16 @@ struct Colors: ParsableCommand {
ColorExtensionGenerator.writeExtensionFile(colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath)
extensionFilePath: options.extensionFilePath,
isSwiftUI: false)
// Generate extension
ColorExtensionGenerator.writeSUIExtensionFile(colors: parsedColors,
ColorExtensionGenerator.writeExtensionFile(colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionNameSwiftUI,
extensionFilePath: options.extensionFilePathSwiftUI)
extensionFilePath: options.extensionFilePathSwiftUI,
isSwiftUI: true)
// -> Time: 0.0010340213775634766 seconds
print("[\(Self.toolName)] Colors generated")
}

View File

@ -27,10 +27,10 @@ struct ColorsToolOptions: ParsableArguments {
@Option(help: "Tell if it will generate static properties or not")
var staticMembers: Bool = false
@Option(help: "Extension name. If not specified, it will generate an UIColor extension. Using default extension name will generate static property.")
@Option(help: "Extension name. If not specified, it will generate an UIColor extension.")
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.")
@Option(help: "SwiftUI Extension name. If not specified, it will generate an Color extension.")
var extensionNameSwiftUI: String = Colors.defaultExtensionNameSUI
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")

View File

@ -15,11 +15,16 @@ struct ColorExtensionGenerator {
// MARK: - UIKit
static func writeExtensionFile(colors: [ParsedColor], staticVar: Bool, extensionName: String, extensionFilePath: String) {
static func writeExtensionFile(colors: [ParsedColor],
staticVar: Bool,
extensionName: String,
extensionFilePath: String,
isSwiftUI: Bool) {
// Create extension content
let extensionContent = Self.getExtensionContent(colors: colors,
staticVar: staticVar,
extensionName: extensionName)
extensionName: extensionName,
isSwiftUI: isSwiftUI)
// Write content
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
@ -32,20 +37,23 @@ struct ColorExtensionGenerator {
}
}
static func getExtensionContent(colors: [ParsedColor], staticVar: Bool, extensionName: String) -> String {
static func getExtensionContent(colors: [ParsedColor],
staticVar: Bool,
extensionName: String,
isSwiftUI: Bool) -> String {
[
Self.getHeader(extensionClassname: extensionName),
Self.getProperties(for: colors, withStaticVar: staticVar),
Self.getHeader(extensionClassname: extensionName, isSwiftUI: isSwiftUI),
Self.getProperties(for: colors, withStaticVar: staticVar, isSwiftUI: isSwiftUI),
Self.getFooter()
]
.joined(separator: "\n")
}
private static func getHeader(extensionClassname: String) -> String {
private static func getHeader(extensionClassname: String, isSwiftUI: Bool) -> String {
"""
// Generated by ResgenSwift.\(Colors.toolName) \(ResgenSwiftVersion)
import UIKit
import \(isSwiftUI ? "SwiftUI" : "UIKit")
extension \(extensionClassname) {\n
"""
@ -57,66 +65,11 @@ struct ColorExtensionGenerator {
"""
}
private static func getProperties(for colors: [ParsedColor], withStaticVar staticVar: Bool) -> String {
private static func getProperties(for colors: [ParsedColor],
withStaticVar staticVar: Bool,
isSwiftUI: Bool) -> String {
colors.map {
if staticVar {
return $0.getColorStaticProperty()
}
return $0.getColorProperty()
}
.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()
$0.getColorProperty(isStatic: staticVar, isSwiftUI: isSwiftUI)
}
.joined(separator: "\n\n")
}

View File

@ -74,41 +74,20 @@ struct ParsedColor {
// MARK: - UIKit
func getColorProperty() -> String {
"""
func getColorProperty(isStatic: Bool, isSwiftUI: Bool) -> String {
if isSwiftUI {
return """
/// Color \(name) is \(light) (light) or \(dark) (dark)"
\(isStatic ? "static " : "")var \(name): Color {
Color("\(name)")
}
"""
}
return """
/// Color \(name) is \(light) (light) or \(dark) (dark)"
@objc var \(name): UIColor {
\(isStatic ? "static " : "@objc ")var \(name): UIColor {
UIColor(named: "\(name)")!
}
"""
}
func getColorStaticProperty() -> String {
"""
/// Color \(name) is \(light) (light) or \(dark) (dark)"
static var \(name): UIColor {
UIColor(named: "\(name)")!
}
"""
}
// 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)")!
}
"""
}
}