Génération de composant SwiftUI: Color et Image
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:
@ -24,6 +24,9 @@ struct FontsOptions: ParsableArguments {
|
||||
@Option(help: "Extension name. If not specified, it will generate an UIFont extension. Using default extension name will generate static property.")
|
||||
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.")
|
||||
var extensionNameSwiftUI: String = Fonts.defaultExtensionNameSUI
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+FontsMyApp.swift")
|
||||
var extensionSuffix: String = ""
|
||||
}
|
||||
@ -31,6 +34,9 @@ struct FontsOptions: ParsableArguments {
|
||||
// MARK: - Computed var
|
||||
|
||||
extension FontsOptions {
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
var extensionFileName: String {
|
||||
if extensionSuffix.isEmpty == false {
|
||||
return "\(extensionName)+\(extensionSuffix).swift"
|
||||
@ -41,4 +47,17 @@ extension FontsOptions {
|
||||
var extensionFilePath: String {
|
||||
"\(extensionOutputPath)/\(extensionFileName)"
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
var extensionFileNameSwiftUI: String {
|
||||
if extensionSuffix.isEmpty == false {
|
||||
return "\(extensionNameSwiftUI)+\(extensionSuffix).swift"
|
||||
}
|
||||
return "\(extensionNameSwiftUI).swift"
|
||||
}
|
||||
|
||||
var extensionFilePathSwiftUI: String {
|
||||
"\(extensionOutputPath)/\(extensionFileNameSwiftUI)"
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ struct Fonts: ParsableCommand {
|
||||
|
||||
static let toolName = "Fonts"
|
||||
static let defaultExtensionName = "UIFont"
|
||||
static let defaultExtensionNameSUI = "Font"
|
||||
|
||||
// MARK: - Command Options
|
||||
|
||||
@ -52,6 +53,11 @@ struct Fonts: ParsableCommand {
|
||||
extensionName: options.extensionName,
|
||||
extensionFilePath: options.extensionFilePath)
|
||||
|
||||
FontExtensionGenerator.writeSUIExtensionFile(fontsNames: fontsNames,
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionNameSwiftUI,
|
||||
extensionFilePath: options.extensionFilePathSwiftUI)
|
||||
|
||||
print("Info.plist information:")
|
||||
print("\(FontPlistGenerator.generatePlistUIAppsFontContent(for: fontsNames))")
|
||||
|
||||
@ -70,6 +76,13 @@ struct Fonts: ParsableCommand {
|
||||
Fonts.exit(withError: error)
|
||||
}
|
||||
|
||||
// Extension for UIKit and SwiftUI should have different name
|
||||
guard options.extensionName != options.extensionNameSwiftUI else {
|
||||
let error = FontsToolError.extensionNamesCollision(options.extensionName)
|
||||
print(error.localizedDescription)
|
||||
Fonts.exit(withError: error)
|
||||
}
|
||||
|
||||
// Check if needed to regenerate
|
||||
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration,
|
||||
inputFilePath: options.inputFile,
|
||||
|
@ -8,6 +8,7 @@
|
||||
import Foundation
|
||||
|
||||
enum FontsToolError: Error {
|
||||
case extensionNamesCollision(String)
|
||||
case fcScan(String, Int32, String?)
|
||||
case inputFolderNotFound(String)
|
||||
case fileNotExists(String)
|
||||
@ -15,6 +16,9 @@ enum FontsToolError: 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 .fcScan(let path, let code, let output):
|
||||
return "error:[\(Fonts.toolName)] Error while getting fontName (fc-scan --format %{postscriptname} \(path). fc-scan exit with \(code) and output is: \(output ?? "no output")"
|
||||
|
||||
|
@ -9,6 +9,19 @@ import Foundation
|
||||
import ToolCore
|
||||
|
||||
class FontExtensionGenerator {
|
||||
|
||||
private static func getFontNameEnum(fontsNames: [String]) -> String {
|
||||
var enumDefinition = " enum FontName: String {\n"
|
||||
|
||||
fontsNames.forEach {
|
||||
enumDefinition += " case \($0.fontNameSanitize) = \"\($0)\"\n"
|
||||
}
|
||||
enumDefinition += " }\n"
|
||||
|
||||
return enumDefinition
|
||||
}
|
||||
|
||||
// MARK: - UIKit
|
||||
|
||||
static func writeExtensionFile(fontsNames: [String], staticVar: Bool, extensionName: String, extensionFilePath: String) {
|
||||
// Create extension content
|
||||
@ -47,17 +60,6 @@ class FontExtensionGenerator {
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getFontNameEnum(fontsNames: [String]) -> String {
|
||||
var enumDefinition = " enum FontName: String {\n"
|
||||
|
||||
fontsNames.forEach {
|
||||
enumDefinition += " case \($0.fontNameSanitize) = \"\($0)\"\n"
|
||||
}
|
||||
enumDefinition += " }\n"
|
||||
|
||||
return enumDefinition
|
||||
}
|
||||
|
||||
private static func getFontMethods(fontsNames: [FontName], staticVar: Bool) -> String {
|
||||
let pragma = " // MARK: - Getter"
|
||||
|
||||
@ -80,4 +82,66 @@ 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 {
|
||||
"""
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
@ -29,4 +29,20 @@ extension FontName {
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user