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
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
This commit is contained in:
31
SampleFiles/Images/Generated/ImageYolo+GenAllScript.swift
Normal file
31
SampleFiles/Images/Generated/ImageYolo+GenAllScript.swift
Normal file
@ -0,0 +1,31 @@
|
||||
// Generated by ResgenSwift.Images 1.0
|
||||
// Images from sampleImages
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension ImageYolo {
|
||||
|
||||
var article_notification_pull_detail: Image {
|
||||
Image("article_notification_pull_detail")
|
||||
}
|
||||
|
||||
var article_notification_pull: Image {
|
||||
Image("article_notification_pull")
|
||||
}
|
||||
|
||||
var new_article: Image {
|
||||
Image("new_article")
|
||||
}
|
||||
|
||||
var welcome_background: Image {
|
||||
Image("welcome_background")
|
||||
}
|
||||
|
||||
var article_trash: Image {
|
||||
Image("article_trash")
|
||||
}
|
||||
|
||||
var ic_close_article: Image {
|
||||
Image("ic_close_article")
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
FORCE_FLAG="$1"
|
||||
|
||||
# Font
|
||||
## Font
|
||||
swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/sampleFontsAll.txt" \
|
||||
--extension-output-path "./Fonts/Generated" \
|
||||
--extension-name "UIFontYolo" \
|
||||
@ -57,4 +57,5 @@ swift run -c release ResgenSwift images $FORCE_FLAG "./Images/sampleImages.txt"
|
||||
--xcassets-path "./Images/imagium.xcassets" \
|
||||
--extension-output-path "./Images/Generated" \
|
||||
--extension-name "UIImage" \
|
||||
--extension-name-swift-ui "ImageYolo" \
|
||||
--extension-suffix "GenAllScript"
|
||||
|
@ -56,15 +56,15 @@ 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)
|
||||
|
||||
// -> Time: 0.0010340213775634766 seconds
|
||||
extensionFilePath: options.extensionFilePathSwiftUI,
|
||||
isSwiftUI: true)
|
||||
|
||||
print("[\(Self.toolName)] Colors generated")
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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)")!
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ struct FontsOptions: ParsableArguments {
|
||||
@Option(help: "Tell if it will generate static properties or methods")
|
||||
var staticMembers: Bool = false
|
||||
|
||||
@Option(help: "Extension name. If not specified, it will generate an UIFont extension. Using default extension name will generate static property.")
|
||||
@Option(help: "Extension name. If not specified, it will generate an UIFont extension.")
|
||||
var extensionName: String = Fonts.defaultExtensionName
|
||||
|
||||
@Option(help: "Extension name. If not specified, it will generate an Font extension. Using default extension name will generate static property.")
|
||||
@Option(help: "Extension name. If not specified, it will generate an Font extension.")
|
||||
var extensionNameSwiftUI: String = Fonts.defaultExtensionNameSUI
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+FontsMyApp.swift")
|
||||
|
@ -51,12 +51,14 @@ struct Fonts: ParsableCommand {
|
||||
FontExtensionGenerator.writeExtensionFile(fontsNames: fontsNames,
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionName,
|
||||
extensionFilePath: options.extensionFilePath)
|
||||
extensionFilePath: options.extensionFilePath,
|
||||
isSwiftUI: false)
|
||||
|
||||
FontExtensionGenerator.writeSUIExtensionFile(fontsNames: fontsNames,
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionNameSwiftUI,
|
||||
extensionFilePath: options.extensionFilePathSwiftUI)
|
||||
FontExtensionGenerator.writeExtensionFile(fontsNames: fontsNames,
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionNameSwiftUI,
|
||||
extensionFilePath: options.extensionFilePathSwiftUI,
|
||||
isSwiftUI: true)
|
||||
|
||||
print("Info.plist information:")
|
||||
print("\(FontPlistGenerator.generatePlistUIAppsFontContent(for: fontsNames))")
|
||||
|
@ -21,13 +21,16 @@ class FontExtensionGenerator {
|
||||
return enumDefinition
|
||||
}
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
static func writeExtensionFile(fontsNames: [String], staticVar: Bool, extensionName: String, extensionFilePath: String) {
|
||||
static func writeExtensionFile(fontsNames: [String],
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
extensionFilePath: String,
|
||||
isSwiftUI: Bool) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getExtensionContent(fontsNames: fontsNames,
|
||||
staticVar: staticVar,
|
||||
extensionName: extensionName)
|
||||
extensionName: extensionName,
|
||||
isSwiftUI: isSwiftUI)
|
||||
|
||||
// Write content
|
||||
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
||||
@ -40,37 +43,36 @@ class FontExtensionGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
static func getExtensionContent(fontsNames: [String], staticVar: Bool, extensionName: String) -> String {
|
||||
static func getExtensionContent(fontsNames: [String],
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
isSwiftUI: Bool) -> String {
|
||||
[
|
||||
Self.getHeader(extensionClassname: extensionName),
|
||||
Self.getHeader(extensionClassname: extensionName, isSwiftUI: isSwiftUI),
|
||||
Self.getFontNameEnum(fontsNames: fontsNames),
|
||||
Self.getFontMethods(fontsNames: fontsNames, staticVar: staticVar),
|
||||
Self.getFontMethods(fontsNames: fontsNames, staticVar: 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.\(Fonts.toolName) \(ResgenSwiftVersion)
|
||||
|
||||
import UIKit
|
||||
import \(isSwiftUI ? "SwiftUI" : "UIKit")
|
||||
|
||||
extension \(extensionClassname) {\n
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getFontMethods(fontsNames: [FontName], staticVar: Bool) -> String {
|
||||
private static func getFontMethods(fontsNames: [FontName], staticVar: Bool, isSwiftUI: Bool) -> String {
|
||||
let pragma = " // MARK: - Getter"
|
||||
|
||||
var propertiesOrMethods: [String] = fontsNames
|
||||
.unique()
|
||||
.map {
|
||||
if staticVar {
|
||||
return $0.staticProperty
|
||||
} else {
|
||||
return $0.method
|
||||
}
|
||||
$0.getProperty(isStatic: staticVar, isSwiftUI: isSwiftUI)
|
||||
}
|
||||
|
||||
propertiesOrMethods.insert(pragma, at: 0)
|
||||
@ -82,66 +84,4 @@ class FontExtensionGenerator {
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
static func writeSUIExtensionFile(fontsNames: [String], staticVar: Bool, extensionName: String, extensionFilePath: String) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getSUIExtensionContent(fontsNames: fontsNames,
|
||||
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 = FontsToolError.writeExtension(extensionFilePath, error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
Fonts.exit(withError: error)
|
||||
}
|
||||
}
|
||||
|
||||
static func getSUIExtensionContent(fontsNames: [String], staticVar: Bool, extensionName: String) -> String {
|
||||
[
|
||||
Self.getSUIHeader(extensionClassname: extensionName),
|
||||
Self.getFontNameEnum(fontsNames: fontsNames),
|
||||
Self.getSUIFontMethods(fontsNames: fontsNames, staticVar: staticVar),
|
||||
Self.getSUIFooter()
|
||||
]
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
||||
private static func getSUIHeader(extensionClassname: String) -> String {
|
||||
"""
|
||||
// Generated by ResgenSwift.\(Fonts.toolName) \(ResgenSwiftVersion)
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension \(extensionClassname) {\n
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getSUIFontMethods(fontsNames: [FontName], staticVar: Bool) -> String {
|
||||
let pragma = " // MARK: - Getter"
|
||||
|
||||
var propertiesOrMethods: [String] = fontsNames
|
||||
.unique()
|
||||
.map {
|
||||
if staticVar {
|
||||
return $0.suiStaticProperty
|
||||
} else {
|
||||
return $0.suiMethod
|
||||
}
|
||||
}
|
||||
|
||||
propertiesOrMethods.insert(pragma, at: 0)
|
||||
return propertiesOrMethods.joined(separator: "\n\n")
|
||||
}
|
||||
|
||||
private static func getSUIFooter() -> String {
|
||||
"""
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
@ -14,35 +14,33 @@ extension FontName {
|
||||
self.removeCharacters(from: "[]+-_")
|
||||
}
|
||||
|
||||
var method: String {
|
||||
"""
|
||||
func getProperty(isStatic: Bool, isSwiftUI: Bool) -> String {
|
||||
if isSwiftUI {
|
||||
if isStatic {
|
||||
return """
|
||||
static let \(fontNameSanitize): ((_ size: CGFloat) -> Font) = { size in
|
||||
Font.custom(FontName.\(fontNameSanitize).rawValue, size: size)
|
||||
}
|
||||
"""
|
||||
}
|
||||
return """
|
||||
func \(fontNameSanitize)(withSize size: CGFloat) -> Font {
|
||||
Font.custom(FontName.\(fontNameSanitize).rawValue, size: size)
|
||||
}
|
||||
"""
|
||||
}
|
||||
// UIKit
|
||||
if isStatic {
|
||||
return """
|
||||
static let \(fontNameSanitize): ((_ size: CGFloat) -> UIFont) = { size in
|
||||
UIFont(name: FontName.\(fontNameSanitize).rawValue, size: size)!
|
||||
}
|
||||
"""
|
||||
}
|
||||
return """
|
||||
func \(fontNameSanitize)(withSize size: CGFloat) -> UIFont {
|
||||
UIFont(name: FontName.\(fontNameSanitize).rawValue, size: size)!
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
var staticProperty: String {
|
||||
"""
|
||||
static let \(fontNameSanitize): ((_ size: CGFloat) -> UIFont) = { size in
|
||||
UIFont(name: FontName.\(fontNameSanitize).rawValue, size: size)!
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
var suiMethod: String {
|
||||
"""
|
||||
func \(fontNameSanitize)(withSize size: CGFloat) -> Font {
|
||||
Font.custom(FontName.\(fontNameSanitize).rawValue, size: size)
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
var suiStaticProperty: String {
|
||||
"""
|
||||
static let \(fontNameSanitize): ((_ size: CGFloat) -> Font) = { size in
|
||||
Font.custom(FontName.\(fontNameSanitize).rawValue, size: size)
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
@ -102,6 +102,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
|
||||
let xcassetsPath: String
|
||||
let extensionOutputPath: String
|
||||
let extensionName: String?
|
||||
let extensionNameSwiftUI: String?
|
||||
let extensionSuffix: String?
|
||||
private let staticMembers: Bool?
|
||||
|
||||
@ -119,6 +120,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
|
||||
- Xcassets path: \(xcassetsPath)
|
||||
- Extension output path: \(extensionOutputPath)
|
||||
- Extension name: \(extensionName ?? "-")
|
||||
- Extension name SwiftUI: \(extensionNameSwiftUI ?? "-")
|
||||
- Extension suffix: \(extensionSuffix ?? "-")
|
||||
"""
|
||||
}
|
||||
|
@ -31,6 +31,12 @@ extension ImagesConfiguration: Runnable {
|
||||
extensionName
|
||||
]
|
||||
}
|
||||
if let extensionNameSwiftUI = extensionNameSwiftUI {
|
||||
args += [
|
||||
"--extension-name-swift-ui",
|
||||
extensionNameSwiftUI
|
||||
]
|
||||
}
|
||||
if let extensionSuffix = extensionSuffix {
|
||||
args += [
|
||||
"--extension-suffix",
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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)"
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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)")!
|
||||
}
|
||||
"""
|
||||
|
@ -33,7 +33,7 @@ struct StringiumOptions: 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 String extension. Using default extension name will generate static property.")
|
||||
@Option(help: "Extension name. If not specified, it will generate an String extension.")
|
||||
var extensionName: String = Stringium.defaultExtensionName
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+{extensionSuffix}.swift")
|
||||
|
@ -24,7 +24,7 @@ struct TagsOptions: 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 a Tag extension. Using default extension name will generate static property.")
|
||||
@Option(help: "Extension name. If not specified, it will generate a Tag extension.")
|
||||
var extensionName: String = Tags.defaultExtensionName
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Tag{extensionSuffix}.swift")
|
||||
|
@ -23,7 +23,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
// When
|
||||
let extensionContent = ColorExtensionGenerator.getExtensionContent(colors: colors,
|
||||
staticVar: false,
|
||||
extensionName: "GenColors")
|
||||
extensionName: "GenColors",
|
||||
isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -58,7 +59,8 @@ final class ColorExtensionGeneratorTests: XCTestCase {
|
||||
// When
|
||||
let extensionContent = ColorExtensionGenerator.getExtensionContent(colors: colors,
|
||||
staticVar: true,
|
||||
extensionName: "GenColor")
|
||||
extensionName: "GenColor",
|
||||
isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
@ -17,7 +17,7 @@ final class ParsedColorTests: XCTestCase {
|
||||
let color = ParsedColor(name: "red", light: "#FF0000", dark: "#0000FF")
|
||||
|
||||
// When
|
||||
let property = color.getColorProperty()
|
||||
let property = color.getColorProperty(isStatic: false, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -35,7 +35,7 @@ final class ParsedColorTests: XCTestCase {
|
||||
let color = ParsedColor(name: "red", light: "#FF0000", dark: "#0000FF")
|
||||
|
||||
// When
|
||||
let property = color.getColorStaticProperty()
|
||||
let property = color.getColorProperty(isStatic: true, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
@ -23,7 +23,8 @@ final class FontExtensionGeneratorTests: XCTestCase {
|
||||
// When
|
||||
let extensionContent = FontExtensionGenerator.getExtensionContent(fontsNames: fontNames,
|
||||
staticVar: false,
|
||||
extensionName: "GenFonts")
|
||||
extensionName: "GenFonts",
|
||||
isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
@ -17,7 +17,7 @@ final class FontNameTests: XCTestCase {
|
||||
let fontName: FontName = "CircularStdBold"
|
||||
|
||||
// When
|
||||
let property = fontName.staticProperty
|
||||
let property = fontName.getProperty(isStatic: true, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -34,7 +34,7 @@ final class FontNameTests: XCTestCase {
|
||||
let fontName: FontName = "[Circular_Std+Bold-Underline]"
|
||||
|
||||
// When
|
||||
let property = fontName.staticProperty
|
||||
let property = fontName.getProperty(isStatic: true, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -51,7 +51,7 @@ final class FontNameTests: XCTestCase {
|
||||
let fontName: FontName = "CircularStdBold"
|
||||
|
||||
// When
|
||||
let property = fontName.method
|
||||
let property = fontName.getProperty(isStatic: false, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -68,7 +68,7 @@ final class FontNameTests: XCTestCase {
|
||||
let fontName: FontName = "[Circular_Std+Bold-Underline]"
|
||||
|
||||
// When
|
||||
let property = fontName.method
|
||||
let property = fontName.getProperty(isStatic: false, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
@ -24,7 +24,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
let extensionContent = ImageExtensionGenerator.getExtensionContent(images: images,
|
||||
staticVar: false,
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename")
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -59,7 +60,8 @@ final class ImageExtensionGeneratorTests: XCTestCase {
|
||||
let extensionContent = ImageExtensionGenerator.getExtensionContent(images: images,
|
||||
staticVar: true,
|
||||
extensionName: "GenImages",
|
||||
inputFilename: "myInputFilename")
|
||||
inputFilename: "myInputFilename",
|
||||
isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
@ -43,7 +43,7 @@ final class ParsedImageTests: XCTestCase {
|
||||
height: 10)
|
||||
|
||||
// When
|
||||
let property = parsedImage.getImageProperty()
|
||||
let property = parsedImage.getImageProperty(isStatic: false, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
@ -64,7 +64,7 @@ final class ParsedImageTests: XCTestCase {
|
||||
height: 10)
|
||||
|
||||
// When
|
||||
let property = parsedImage.getStaticImageProperty()
|
||||
let property = parsedImage.getImageProperty(isStatic: true, isSwiftUI: false)
|
||||
|
||||
// Expect
|
||||
let expect = """
|
||||
|
Reference in New Issue
Block a user