// // FontToolContentGenerator.swift // // // Created by Thibaut Schmitt on 13/12/2021. // import Foundation import ToolCore class FontExtensionGenerator { static func writeExtensionFile(fontsNames: [String], staticVar: Bool, extensionName: String, extensionFilePath: String) { // Check file if not exists let fileManager = FileManager() if fileManager.fileExists(atPath: extensionFilePath) == false { Shell.shell("touch", "\(extensionFilePath)") } // Create extension content let extensionContent = [ Self.getHeader(extensionClassname: extensionName), Self.getFontNameEnum(fontsNames: fontsNames), Self.getFontMethods(fontsNames: fontsNames, staticVar: staticVar), Self.getFooter() ] .joined(separator: "\n") // Write content let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath) do { try extensionContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8) } catch (let error) { let error = FontToolError.writeExtension(extensionFilePath, error.localizedDescription) print(error.localizedDescription) FontTool.exit(withError: error) } } private static func getHeader(extensionClassname: String) -> String { """ // Generated by ResgenSwift.\(FontTool.toolName) \(ResgenSwiftVersion) import UIKit extension \(extensionClassname) {\n """ } private static func getFontNameEnum(fontsNames: [String]) -> String { var enumDefinition = "\tenum FontName: String {\n" fontsNames.forEach { enumDefinition += "\t\tcase \($0.removeCharacters(from: "[]+-_")) = \"\($0)\"\n" } enumDefinition += "\t}\n" return enumDefinition } private static func getFontMethods(fontsNames: [FontName], staticVar: Bool) -> String { let pragma = "\t// MARK: - Getter" var propertiesOrMethods: [String] = fontsNames .unique() .map { if staticVar { return $0.staticProperty } else { return $0.method } } propertiesOrMethods.insert(pragma, at: 0) return propertiesOrMethods.joined(separator: "\n\n") } private static func getFooter() -> String { """ } """ } }