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

@@ -10,18 +10,20 @@ import Foundation
class ImageExtensionGenerator {
// MARK: - pragm
// MARK: - UIKit
static func generateExtensionFile(images: [ParsedImage],
staticVar: Bool,
inputFilename: String,
extensionName: String,
extensionFilePath: String) {
extensionFilePath: String,
isSwiftUI: Bool) {
// Create extension conten1t
let extensionContent = Self.getExtensionContent(images: images,
staticVar: staticVar,
extensionName: extensionName,
inputFilename: inputFilename)
inputFilename: inputFilename,
isSwiftUI: isSwiftUI)
// Write content
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
@@ -34,38 +36,35 @@ class ImageExtensionGenerator {
}
}
// MARK: - Extension content
static func getExtensionContent(images: [ParsedImage], staticVar: Bool, extensionName: String, inputFilename: String) -> String {
static func getExtensionContent(images: [ParsedImage],
staticVar: Bool,
extensionName: String,
inputFilename: String,
isSwiftUI: Bool) -> String {
[
Self.getHeader(inputFilename: inputFilename, extensionClassname: extensionName),
Self.getProperties(images: images, staticVar: staticVar),
Self.getHeader(inputFilename: inputFilename, extensionClassname: extensionName, isSwiftUI: isSwiftUI),
Self.getProperties(images: images, staticVar: staticVar, isSwiftUI: isSwiftUI),
Self.getFooter()
]
.joined(separator: "\n")
}
// MARK: - Extension part
private static func getHeader(inputFilename: String, extensionClassname: String) -> String {
private static func getHeader(inputFilename: String,
extensionClassname: String,
isSwiftUI: Bool) -> String {
"""
// Generated by ResgenSwift.\(Images.toolName) \(ResgenSwiftVersion)
// Images from \(inputFilename)
import UIKit
import \(isSwiftUI ? "SwiftUI" : "UIKit")
extension \(extensionClassname) {
"""
}
private static func getProperties(images: [ParsedImage], staticVar: Bool) -> String {
if staticVar {
return images
.map { "\n\($0.getStaticImageProperty())" }
.joined(separator: "\n")
}
return images
.map { "\n\($0.getImageProperty())" }
private static func getProperties(images: [ParsedImage], staticVar: Bool, isSwiftUI: Bool) -> String {
images
.map { "\n\($0.getImageProperty(isStatic: staticVar, isSwiftUI: isSwiftUI))" }
.joined(separator: "\n")
}

View File

@@ -22,6 +22,7 @@ struct Images: ParsableCommand {
static let toolName = "Images"
static let defaultExtensionName = "UIImage"
static let defaultExtensionNameSUI = "Image"
// MARK: - Command Options
@@ -56,8 +57,15 @@ struct Images: ParsableCommand {
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath)
extensionFilePath: options.extensionFilePath,
isSwiftUI: false)
ImageExtensionGenerator.generateExtensionFile(images: imagesToGenerate,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: options.extensionNameSwiftUI,
extensionFilePath: options.extensionFilePathSwiftUI,
isSwiftUI: true)
print("[\(Self.toolName)] Images generated")
}
@@ -81,6 +89,13 @@ struct Images: ParsableCommand {
// RSVG-Converter
_ = Images.getSvgConverterPath()
// Extension for UIKit and SwiftUI should have different name
guard options.extensionName != options.extensionNameSwiftUI else {
let error = ImagesError.extensionNamesCollision(options.extensionName)
print(error.localizedDescription)
Images.exit(withError: error)
}
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(force: options.forceExecution,
inputFilePath: options.inputFile,

View File

@@ -8,6 +8,7 @@
import Foundation
enum ImagesError: Error {
case extensionNamesCollision(String)
case inputFolderNotFound(String)
case fileNotExists(String)
case unknownImageExtension(String)
@@ -19,6 +20,9 @@ enum ImagesError: Error {
var localizedDescription: 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 .inputFolderNotFound(let inputFolder):
return " error:[\(Images.toolName)] Input folder not found: \(inputFolder)"

View File

@@ -27,9 +27,12 @@ struct ImagesOptions: 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 UIImage extension. Using default extension name will generate static property.")
@Option(help: "Extension name. If not specified, it will generate an UIImage extension.")
var extensionName: String = Images.defaultExtensionName
@Option(help: "Extension name. If not specified, it will generate an Image extension.")
var extensionNameSwiftUI: String = Images.defaultExtensionNameSUI
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Image{extensionSuffix}.swift")
var extensionSuffix: String?
}
@@ -37,6 +40,9 @@ struct ImagesOptions: ParsableArguments {
// MARK: - Computed var
extension ImagesOptions {
// MARK: - UIKit
var extensionFileName: String {
if let extensionSuffix = extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift"
@@ -48,6 +54,21 @@ extension ImagesOptions {
"\(extensionOutputPath)/\(extensionFileName)"
}
// MARK: - SwiftUI
var extensionFileNameSwiftUI: String {
if let extensionSuffix = extensionSuffix {
return "\(extensionNameSwiftUI)+\(extensionSuffix).swift"
}
return "\(extensionNameSwiftUI).swift"
}
var extensionFilePathSwiftUI: String {
"\(extensionOutputPath)/\(extensionFileNameSwiftUI)"
}
// MARK: -
var inputFilenameWithoutExt: String {
URL(fileURLWithPath: inputFile)
.deletingPathExtension()

View File

@@ -72,17 +72,16 @@ struct ParsedImage {
// MARK: - Extension property
func getImageProperty() -> String {
"""
var \(name): UIImage {
UIImage(named: "\(name)")!
}
"""
}
func getStaticImageProperty() -> String {
"""
static var \(name): UIImage {
func getImageProperty(isStatic: Bool, isSwiftUI: Bool) -> String {
if isSwiftUI {
return """
\(isStatic ? "static ": "")var \(name): Image {
Image("\(name)")
}
"""
}
return """
\(isStatic ? "static ": "")var \(name): UIImage {
UIImage(named: "\(name)")!
}
"""