Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
89 lines
3.0 KiB
Swift
89 lines
3.0 KiB
Swift
//
|
|
// FontToolContentGenerator.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
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
|
|
}
|
|
|
|
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,
|
|
isSwiftUI: isSwiftUI)
|
|
|
|
// 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.description)
|
|
Fonts.exit(withError: error)
|
|
}
|
|
}
|
|
|
|
static func getExtensionContent(fontsNames: [String],
|
|
staticVar: Bool,
|
|
extensionName: String,
|
|
isSwiftUI: Bool) -> String {
|
|
[
|
|
Self.getHeader(extensionClassname: extensionName, isSwiftUI: isSwiftUI),
|
|
Self.getFontNameEnum(fontsNames: fontsNames),
|
|
Self.getFontMethods(fontsNames: fontsNames, staticVar: staticVar, isSwiftUI: isSwiftUI),
|
|
Self.getFooter()
|
|
]
|
|
.joined(separator: "\n")
|
|
}
|
|
|
|
private static func getHeader(extensionClassname: String, isSwiftUI: Bool) -> String {
|
|
"""
|
|
// Generated by ResgenSwift.\(Fonts.toolName) \(ResgenSwiftVersion)
|
|
|
|
import \(isSwiftUI ? "SwiftUI" : "UIKit")
|
|
|
|
extension \(extensionClassname) {\n
|
|
"""
|
|
}
|
|
|
|
private static func getFontMethods(fontsNames: [FontName], staticVar: Bool, isSwiftUI: Bool) -> String {
|
|
let pragma = " // MARK: - Getter"
|
|
|
|
var propertiesOrMethods: [String] = fontsNames
|
|
.unique()
|
|
.map {
|
|
$0.getProperty(isStatic: staticVar, isSwiftUI: isSwiftUI)
|
|
}
|
|
|
|
propertiesOrMethods.insert(pragma, at: 0)
|
|
return propertiesOrMethods.joined(separator: "\n\n")
|
|
}
|
|
|
|
private static func getFooter() -> String {
|
|
"""
|
|
}
|
|
|
|
"""
|
|
}
|
|
}
|