Squashed commit of the following:
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit

commit aa59ef28ea56315eb421ba044ddaf0c4066bfff8
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Mon Nov 7 10:26:28 2022 +0100

    Add trailing carrier at the end of generated extension files

commit e985950aa1e39d81d4938e15f8724c0f7723b003
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Fri Nov 4 16:37:34 2022 +0100

    Replace installation script by script that install completion file
    Setup a Makefile to install resgen

commit d574384c151259610a4c2f837b14068bb7716e44
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Fri Nov 4 14:33:39 2022 +0100

    Refactor
    Improve testability
    Add tests on SwiftUI generated code
    Add tests on  command

commit d9e76632c3037da0ed9e1dd37056685416579da9
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Thu Nov 3 15:43:47 2022 +0100

    Fixing bad merge on FontOptions

commit 76b5ebfcd1cde7a7d4c83f516a4fc937841e7715
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Thu Nov 3 15:37:28 2022 +0100

    Squashed commit of the following:

    commit 085f1ffc33
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Thu Nov 3 15:00:27 2022 +0100

        Refactor SwiftUI extension generation and generation SwiftUI extension for images

    commit 4f7d7e18b1
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Mon Oct 31 16:21:32 2022 +0100

        Génération de composant SwiftUI: Color et Image

    commit 0797667b25
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Mon Oct 31 16:21:12 2022 +0100

        Génération de composant SwiftUI: Color et Image

commit 417a2630925841dd486ae1d684d28ab7dca240e0
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Wed Oct 19 17:13:03 2022 +0200

    Update Info.plist UIAppFonts key when generating fonts (if plist path if defined)
This commit is contained in:
2022-11-07 10:32:17 +01:00
parent 6203700b0c
commit 41733d2680
75 changed files with 1487 additions and 933 deletions

View File

@ -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
@ -43,7 +44,7 @@ struct Colors: ParsableCommand {
// Get colors to generate
let parsedColors = ColorFileParser.parse(options.inputFile,
colorStyle: options.colorStyle)
colorStyle: options.style)
// -> Time: 0.0020350217819213867 seconds
// Generate all colors in xcassets
@ -55,9 +56,16 @@ struct Colors: ParsableCommand {
ColorExtensionGenerator.writeExtensionFile(colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath)
// -> Time: 0.0010340213775634766 seconds
extensionFilePath: options.extensionFilePath,
isSwiftUI: false)
// Generate extension
ColorExtensionGenerator.writeExtensionFile(colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionNameSwiftUI,
extensionFilePath: options.extensionFilePathSwiftUI,
isSwiftUI: true)
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,

View File

@ -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\""

View File

@ -16,7 +16,7 @@ struct ColorsToolOptions: ParsableArguments {
var inputFile: String
@Option(help: "Color style to generate: light for light colors only, or all for dark and light colors")
fileprivate var style: String
var style: ColorStyle
@Option(help: "Path of xcassets where to generate colors", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var xcassetsPath: String
@ -27,9 +27,12 @@ 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.")
var extensionNameSwiftUI: String = Colors.defaultExtensionNameSUI
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")
var extensionSuffix: String?
}
@ -37,9 +40,8 @@ struct ColorsToolOptions: ParsableArguments {
// MARK: - Computed var
extension ColorsToolOptions {
var colorStyle: ColorStyle {
ColorStyle(rawValue: style) ?? .all
}
// MARK: - UIKit
var extensionFileName: String {
if let extensionSuffix = extensionSuffix {
@ -51,4 +53,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)"
}
}

View File

@ -13,11 +13,18 @@ struct ColorExtensionGenerator {
let colors: [ParsedColor]
let extensionClassname: String
static func writeExtensionFile(colors: [ParsedColor], staticVar: Bool, extensionName: String, extensionFilePath: String) {
// MARK: - UIKit
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)
@ -30,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
"""
@ -52,15 +62,15 @@ struct ColorExtensionGenerator {
private static func getFooter() -> String {
"""
}
"""
}
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()
$0.getColorProperty(isStatic: staticVar, isSwiftUI: isSwiftUI)
}
.joined(separator: "\n\n")
}

View File

@ -6,8 +6,16 @@
//
import Foundation
import ArgumentParser
enum ColorStyle: String, Decodable {
enum ColorStyle: String, Decodable, ExpressibleByArgument {
case light
case all
static var allValueStrings: [String] {
[
Self.light.rawValue,
Self.all.rawValue
]
}
}

View File

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