Optional generation of Colors extension
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit

This commit is contained in:
2025-07-17 09:40:08 +02:00
parent a64a956254
commit 187980933e
3 changed files with 56 additions and 33 deletions

View File

@@ -36,7 +36,7 @@ swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/fonts.txt" \
## Colors ## Colors
Colors generator generates an extension of `UIColor` (or a custom class) along with colorsets in specified xcassets. Colors generator generates colorsets in specified xcassets and an extension of `Color` (or a custom class) associated to those colorsets. If the extension name is not specified, no extension will be generated.
```sh ```sh
swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \ swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \

View File

@@ -21,8 +21,6 @@ struct Colors: ParsableCommand {
// MARK: - Static // MARK: - Static
static let toolName = "Color" static let toolName = "Color"
static let defaultExtensionName = "Color"
static let defaultExtensionNameUIKit = "UIColor"
static let assetsColorsFolderName = "Colors" static let assetsColorsFolderName = "Colors"
// MARK: - Command options // MARK: - Command options
@@ -57,22 +55,28 @@ struct Colors: ParsableCommand {
// -> Time: 3.4505380392074585 seconds // -> Time: 3.4505380392074585 seconds
// Generate extension // Generate extension
if let extensionName = options.extensionName,
let extensionFilePath = options.extensionFilePath {
ColorExtensionGenerator.writeExtensionFile( ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors, colors: parsedColors,
staticVar: options.staticMembers, staticVar: options.staticMembers,
extensionName: options.extensionName, extensionName: extensionName,
extensionFilePath: options.extensionFilePath, extensionFilePath: extensionFilePath,
isSwiftUI: true isSwiftUI: true
) )
}
// Generate extension // Generate extension
if let extensionNameUIKit = options.extensionNameUIKit,
let extensionFilePathUIKit = options.extensionFilePathUIKit {
ColorExtensionGenerator.writeExtensionFile( ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors, colors: parsedColors,
staticVar: options.staticMembers, staticVar: options.staticMembers,
extensionName: options.extensionNameUIKit, extensionName: extensionNameUIKit,
extensionFilePath: options.extensionFilePathUIKit, extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false isSwiftUI: false
) )
}
print("[\(Self.toolName)] Colors generated") print("[\(Self.toolName)] Colors generated")
} }
@@ -96,18 +100,29 @@ struct Colors: ParsableCommand {
Self.exit(withError: error) Self.exit(withError: error)
} }
// Extension for UIKit and SwiftUI should have different name // Extension for UIKit and SwiftUI should have different name (if both are defined)
guard options.extensionName != options.extensionNameUIKit else { if let extensionName = options.extensionName,
let error = ColorsToolError.extensionNamesCollision(options.extensionName) let extensionNameUIKit = options.extensionNameUIKit {
guard extensionName != extensionNameUIKit else {
let error = ColorsToolError.extensionNamesCollision(extensionName)
print(error.description) print(error.description)
Self.exit(withError: error) Self.exit(withError: error)
} }
}
// Check if needed to regenerate // Check if needed to regenerate
let fileToCompareToInput: String = {
// If there is no extension file to compare
// Then check the xcassets file instead
if let extensionFilePath = options.extensionFilePath {
return extensionFilePath
}
return options.xcassetsPath
}()
guard GeneratorChecker.shouldGenerate( guard GeneratorChecker.shouldGenerate(
force: options.forceGeneration, force: options.forceGeneration,
inputFilePath: options.inputFile, inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath extensionFilePath: fileToCompareToInput
) else { ) else {
print("[\(Self.toolName)] Colors are already up to date :) ") print("[\(Self.toolName)] Colors are already up to date :) ")
return false return false

View File

@@ -30,11 +30,11 @@ struct ColorsToolOptions: ParsableArguments {
@Option(help: "Tell if it will generate static properties or not") @Option(help: "Tell if it will generate static properties or not")
var staticMembers: Bool = false var staticMembers: Bool = false
@Option(help: "Extension name. If not specified, it will generate an Color extension.") @Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionName: String = Colors.defaultExtensionName var extensionName: String?
@Option(help: "SwiftUI Extension name. If not specified, it will generate an UIColor extension.") @Option(help: "SwiftUI Extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String = Colors.defaultExtensionNameUIKit var extensionNameUIKit: String?
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift") @Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")
var extensionSuffix: String? var extensionSuffix: String?
@@ -46,27 +46,35 @@ extension ColorsToolOptions {
// MARK: - SwiftUI // MARK: - SwiftUI
var extensionFileName: String { var extensionFileName: String? {
guard let extensionName else { return nil }
if let extensionSuffix { if let extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift" return "\(extensionName)+\(extensionSuffix).swift"
} }
return "\(extensionName).swift" return "\(extensionName).swift"
} }
var extensionFilePath: String { var extensionFilePath: String? {
"\(extensionOutputPath)/\(extensionFileName)" guard let extensionFileName else { return nil }
return "\(extensionOutputPath)/\(extensionFileName)"
} }
// MARK: - UIKit // MARK: - UIKit
var extensionFileNameUIKit: String { var extensionFileNameUIKit: String? {
guard let extensionNameUIKit else { return nil }
if let extensionSuffix { if let extensionSuffix {
return "\(extensionNameUIKit)+\(extensionSuffix).swift" return "\(extensionNameUIKit)+\(extensionSuffix).swift"
} }
return "\(extensionNameUIKit).swift" return "\(extensionNameUIKit).swift"
} }
var extensionFilePathUIKit: String { var extensionFilePathUIKit: String? {
"\(extensionOutputPath)/\(extensionFileNameUIKit)" guard let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
} }
} }