Optional generation of Images extension. Make extensionOutputPath optional for Images and Colors cause it's not needed if no extensionName are provided

This commit is contained in:
2025-07-17 10:23:05 +02:00
parent 4426091dcb
commit e22f9ba894
12 changed files with 132 additions and 97 deletions

View File

@@ -54,12 +54,13 @@ swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \
1. `-f`: force generation 1. `-f`: force generation
2. Input colors file 2. Input colors file
3. `--style` can be `all` or `light` 3. `--style` can be `all` or `light`
4. `--extension-output-path`: path where to generate generated extension 4. `--extension-output-path` *(optional)* : path where to generate generated extension
5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters 5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters
6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters 6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters
7. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppColor+GreatApp.swift`) 7. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppColor+GreatApp.swift`)
8. `--static-members` *(optional)*: generate static properties or not 8. `--static-members` *(optional)*: generate static properties or not
> ⚠️ Passing a `extensionOutputPath` without any `extensionName` does not generate extension. **But** passing an `extensionName` without `extensionOutputPath` will result in a error.
## Strings ## Strings
@@ -266,7 +267,7 @@ events:
## Images ## Images
Images generator will generate images assets along with extensions to access those images easily. Images generator will generate images assets along with extensions to access those images easily. If the extension name is not specified, no extension will be generated.
```sh ```sh
swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \ swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \
@@ -283,12 +284,13 @@ swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \
1. `-f`: force generation 1. `-f`: force generation
2. Input images definitions file 2. Input images definitions file
3. `--xcassets-path`: xcasset path where to generate imagesets 3. `--xcassets-path`: xcasset path where to generate imagesets
4. `--extension-output-path`: path where to generate generated extension 4. `--extension-output-path` *(optional)* : path where to generate generated extension
5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters 5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters
6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters 6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters
6. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppImage+GreatApp.swift`) 6. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppImage+GreatApp.swift`)
7. `--static-members` *(optional)*: generate static properties or not 7. `--static-members` *(optional)*: generate static properties or not
> ⚠️ Passing a `extensionOutputPath` without any `extensionName` does not generate extension. **But** passing an `extensionName` without `extensionOutputPath` will result in a error.
> ⚠️ Svg images will be copied in the assets and rendered as "Original", however if those images are not rendered correctly you can force the png generation by adding the key word "png" like this: id arrow_back 15 ? png > ⚠️ Svg images will be copied in the assets and rendered as "Original", however if those images are not rendered correctly you can force the png generation by adding the key word "png" like this: id arrow_back 15 ? png
## All at once ## All at once

View File

@@ -1,4 +1,4 @@
// Generated by ResgenSwift.Color 1.2 // Generated by ResgenSwift.Color 2.1.0
import UIKit import UIKit

View File

@@ -1,6 +0,0 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -110,6 +110,17 @@ struct Colors: ParsableCommand {
} }
} }
// If an extension need to be generated, ensure extensionOutputPath is defined
if options.extensionName != nil ||
options.extensionNameUIKit != nil {
guard let extensionOutputPath = options.extensionOutputPath,
extensionOutputPath.isEmpty == false else {
let error = ColorsToolError.missingExtensionPath
print(error.description)
Self.exit(withError: error)
}
}
// Check if needed to regenerate // Check if needed to regenerate
let fileToCompareToInput: String = { let fileToCompareToInput: String = {
// If there is no extension file to compare // If there is no extension file to compare

View File

@@ -17,6 +17,7 @@ enum ColorsToolError: Error {
case fileNotExists(String) case fileNotExists(String)
case badColorDefinition(String, String) case badColorDefinition(String, String)
case deleteExistingColors(String) case deleteExistingColors(String)
case missingExtensionPath
var description: String { var description: String {
switch self { switch self {
@@ -43,6 +44,9 @@ enum ColorsToolError: Error {
case .deleteExistingColors(let assetsFolder): case .deleteExistingColors(let assetsFolder):
return "error: [\(Colors.toolName)] An error occured while deleting colors folder `\(assetsFolder)`" return "error: [\(Colors.toolName)] An error occured while deleting colors folder `\(assetsFolder)`"
case .missingExtensionPath:
return "error: [\(Colors.toolName)] Extension need to be generated but no `extensionOutputPath` is provided"
} }
} }
} }

View File

@@ -24,12 +24,12 @@ struct ColorsToolOptions: ParsableArguments {
@Option(help: "Path of xcassets where to generate colors", transform: { $0.replaceTiltWithHomeDirectoryPath() }) @Option(help: "Path of xcassets where to generate colors", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var xcassetsPath: String var xcassetsPath: String
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@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: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String?
@Option(help: "Extension name. If not specified, no extension will be generated.") @Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionName: String? var extensionName: String?
@@ -56,7 +56,7 @@ extension ColorsToolOptions {
} }
var extensionFilePath: String? { var extensionFilePath: String? {
guard let extensionFileName else { return nil } guard let extensionOutputPath, let extensionFileName else { return nil }
return "\(extensionOutputPath)/\(extensionFileName)" return "\(extensionOutputPath)/\(extensionFileName)"
} }
@@ -73,7 +73,7 @@ extension ColorsToolOptions {
} }
var extensionFilePathUIKit: String? { var extensionFilePathUIKit: String? {
guard let extensionFileNameUIKit else { return nil } guard let extensionOutputPath, let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)" return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
} }

View File

@@ -131,7 +131,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
let inputFile: String let inputFile: String
let style: String let style: String
let xcassetsPath: String let xcassetsPath: String
let extensionOutputPath: String let extensionOutputPath: String?
let extensionName: String? let extensionName: String?
let extensionNameUIKit: String? let extensionNameUIKit: String?
let extensionSuffix: String? let extensionSuffix: String?
@@ -148,7 +148,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
inputFile: String, inputFile: String,
style: String, style: String,
xcassetsPath: String, xcassetsPath: String,
extensionOutputPath: String, extensionOutputPath: String?,
extensionName: String?, extensionName: String?,
extensionNameUIKit: String?, extensionNameUIKit: String?,
extensionSuffix: String?, extensionSuffix: String?,
@@ -170,7 +170,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
- Input file: \(inputFile) - Input file: \(inputFile)
- Style: \(style) - Style: \(style)
- Xcassets path: \(xcassetsPath) - Xcassets path: \(xcassetsPath)
- Extension output path: \(extensionOutputPath) - Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-") - Extension name: \(extensionName ?? "-")
- Extension name UIKit: \(extensionNameUIKit ?? "-") - Extension name UIKit: \(extensionNameUIKit ?? "-")
- Extension suffix: \(extensionSuffix ?? "-") - Extension suffix: \(extensionSuffix ?? "-")
@@ -230,7 +230,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
let inputFile: String let inputFile: String
let xcassetsPath: String let xcassetsPath: String
let extensionOutputPath: String let extensionOutputPath: String?
let extensionName: String? let extensionName: String?
let extensionNameUIKit: String? let extensionNameUIKit: String?
let extensionSuffix: String? let extensionSuffix: String?
@@ -246,7 +246,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
internal init( internal init(
inputFile: String, inputFile: String,
xcassetsPath: String, xcassetsPath: String,
extensionOutputPath: String, extensionOutputPath: String?,
extensionName: String?, extensionName: String?,
extensionNameUIKit: String?, extensionNameUIKit: String?,
extensionSuffix: String?, extensionSuffix: String?,
@@ -266,7 +266,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
Images configuration: Images configuration:
- Input file: \(inputFile) - Input file: \(inputFile)
- Xcassets path: \(xcassetsPath) - Xcassets path: \(xcassetsPath)
- Extension output path: \(extensionOutputPath) - Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-") - Extension name: \(extensionName ?? "-")
- Extension name UIKit: \(extensionNameUIKit ?? "-") - Extension name UIKit: \(extensionNameUIKit ?? "-")
- Extension suffix: \(extensionSuffix ?? "-") - Extension suffix: \(extensionSuffix ?? "-")

View File

@@ -27,29 +27,23 @@ extension ColorsConfiguration: Runnable {
style, style,
"--xcassets-path", "--xcassets-path",
xcassetsPath.prependIfRelativePath(projectDirectory), xcassetsPath.prependIfRelativePath(projectDirectory),
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members", "--static-members",
"\(staticMembersOptions)" "\(staticMembersOptions)"
] ]
if let extensionName { // Add optional parameters
args += [ [
"--extension-name", ("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
extensionName ("--extension-name", extensionName),
] ("--extension-name-ui-kit", extensionNameUIKit),
} ("--extension-suffix", extensionSuffix)
if let extensionNameUIKit { ].forEach { argumentName, argumentValue in
args += [ if let argumentValue {
"--extension-name-ui-kit", args += [
extensionNameUIKit argumentName,
] argumentValue
} ]
if let extensionSuffix { }
args += [
"--extension-suffix",
extensionSuffix
]
} }
return args return args

View File

@@ -25,31 +25,23 @@ extension ImagesConfiguration: Runnable {
inputFile.prependIfRelativePath(projectDirectory), inputFile.prependIfRelativePath(projectDirectory),
"--xcassets-path", "--xcassets-path",
xcassetsPath.prependIfRelativePath(projectDirectory), xcassetsPath.prependIfRelativePath(projectDirectory),
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members", "--static-members",
"\(staticMembersOptions)" "\(staticMembersOptions)"
] ]
if let extensionName { // Add optional parameters
args += [ [
"--extension-name", ("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
extensionName ("--extension-name", extensionName),
] ("--extension-name-ui-kit", extensionNameUIKit),
} ("--extension-suffix", extensionSuffix)
].forEach { argumentName, argumentValue in
if let extensionNameUIKit { if let argumentValue {
args += [ args += [
"--extension-name-ui-kit", argumentName,
extensionNameUIKit argumentValue
] ]
} }
if let extensionSuffix {
args += [
"--extension-suffix",
extensionSuffix
]
} }
return args return args

View File

@@ -21,8 +21,6 @@ struct Images: ParsableCommand {
// MARK: - Static // MARK: - Static
static let toolName = "Images" static let toolName = "Images"
static let defaultExtensionName = "Image"
static let defaultExtensionNameUIKit = "UIImage"
// MARK: - Command Options // MARK: - Command Options
@@ -55,23 +53,29 @@ struct Images: ParsableCommand {
) )
// Generate extension // Generate extension
ImageExtensionGenerator.generateExtensionFile( if let extensionName = options.extensionName,
images: imagesToGenerate, let extensionFilePath = options.extensionFilePath {
staticVar: options.staticMembers, ImageExtensionGenerator.generateExtensionFile(
inputFilename: options.inputFilenameWithoutExt, images: imagesToGenerate,
extensionName: options.extensionName, staticVar: options.staticMembers,
extensionFilePath: options.extensionFilePath, inputFilename: options.inputFilenameWithoutExt,
isSwiftUI: true extensionName: extensionName,
) extensionFilePath: extensionFilePath,
isSwiftUI: true
)
}
ImageExtensionGenerator.generateExtensionFile( if let extensionNameUIKit = options.extensionNameUIKit,
images: imagesToGenerate, let extensionFilePathUIKit = options.extensionFilePathUIKit {
staticVar: options.staticMembers, ImageExtensionGenerator.generateExtensionFile(
inputFilename: options.inputFilenameWithoutExt, images: imagesToGenerate,
extensionName: options.extensionNameUIKit, staticVar: options.staticMembers,
extensionFilePath: options.extensionFilePathUIKit, inputFilename: options.inputFilenameWithoutExt,
isSwiftUI: false extensionName: extensionNameUIKit,
) extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false
)
}
print("[\(Self.toolName)] Images generated") print("[\(Self.toolName)] Images generated")
} }
@@ -96,17 +100,39 @@ struct Images: ParsableCommand {
_ = Self.getSvgConverterPath() _ = Self.getSvgConverterPath()
// Extension for UIKit and SwiftUI should have different name // Extension for UIKit and SwiftUI should have different name
guard options.extensionName != options.extensionNameUIKit else { if let extensionName = options.extensionName,
let error = ImagesError.extensionNamesCollision(options.extensionName) let extensionNameUIKit = options.extensionNameUIKit {
print(error.description) guard extensionName != extensionNameUIKit else {
Self.exit(withError: error) let error = ImagesError.extensionNamesCollision(extensionName)
print(error.description)
Self.exit(withError: error)
}
}
// If an extension need to be generated, ensure extensionOutputPath is defined
if options.extensionName != nil ||
options.extensionNameUIKit != nil {
guard let extensionOutputPath = options.extensionOutputPath,
extensionOutputPath.isEmpty == false else {
let error = ImagesError.missingExtensionPath
print(error.description)
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.forceExecution, force: options.forceExecution,
inputFilePath: options.inputFile, inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath extensionFilePath: fileToCompareToInput
) else { ) else {
print("[\(Self.toolName)] Images are already up to date :) ") print("[\(Self.toolName)] Images are already up to date :) ")
return false return false

View File

@@ -18,6 +18,7 @@ enum ImagesError: Error {
case magickConvertNotFound case magickConvertNotFound
case writeFile(String, String) case writeFile(String, String)
case createAssetFolder(String) case createAssetFolder(String)
case missingExtensionPath
case unknown(String) case unknown(String)
var description: String { var description: String {
@@ -49,6 +50,9 @@ enum ImagesError: Error {
case .createAssetFolder(let folder): case .createAssetFolder(let folder):
return "error: [\(Colors.toolName)] An error occured while creating folder `\(folder)`" return "error: [\(Colors.toolName)] An error occured while creating folder `\(folder)`"
case .missingExtensionPath:
return "error: [\(Colors.toolName)] Extension need to be generated but no `extensionOutputPath` is provided"
case .unknown(let errorDescription): case .unknown(let errorDescription):
return "error: [\(Images.toolName)] Unknown error: \(errorDescription)" return "error: [\(Images.toolName)] Unknown error: \(errorDescription)"
} }

View File

@@ -24,17 +24,17 @@ struct ImagesOptions: ParsableArguments {
@Option(help: "Xcassets path where to generate images.", transform: { $0.replaceTiltWithHomeDirectoryPath() }) @Option(help: "Xcassets path where to generate images.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var xcassetsPath: String var xcassetsPath: String
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@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 Image extension.") @Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionName: String = Images.defaultExtensionName var extensionOutputPath: String?
@Option(help: "Extension name. If not specified, it will generate an UIImage extension.") @Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String = Images.defaultExtensionNameUIKit var extensionName: String?
@Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String?
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Image{extensionSuffix}.swift") @Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Image{extensionSuffix}.swift")
var extensionSuffix: String? var extensionSuffix: String?
@@ -46,28 +46,36 @@ extension ImagesOptions {
// 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 extensionOutputPath, 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 extensionOutputPath, let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
} }
// MARK: - // MARK: -