84 lines
2.9 KiB
Swift
84 lines
2.9 KiB
Swift
//
|
|
// FontTool.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
import Foundation
|
|
import CLIToolCore
|
|
import ArgumentParser
|
|
|
|
struct FontTool: ParsableCommand {
|
|
static let defaultExtensionName = "UIFont"
|
|
|
|
@Argument(help: "Input folder where fonts to generate are.")
|
|
var inputFolder: String
|
|
|
|
@Option(help: "Path where to generate the extension.")
|
|
var extensionOutputPath: String
|
|
|
|
@Option(help: "Extension name. If not specified, it will generate an UIFont extension")
|
|
var extensionName: String = Self.defaultExtensionName
|
|
|
|
public func run() throws {
|
|
print("[FontTool] Starting font generation")
|
|
|
|
let fontsData = FontToolHelper.getFontsData(fromInputFolder: inputFolder)
|
|
|
|
let extensionHeader = FontToolContentGenerator.getExtensionHeader(fontsNames: fontsData.fontsNames)
|
|
let extensionDefinitionOpening = "extension \(extensionName) {\n"
|
|
let extensionFontsNamesEnum = FontToolContentGenerator.getFontNameEnum(fontsNames: fontsData.fontsNames)
|
|
let extensionFontsMethods = FontToolContentGenerator.getFontMethods(fontsNames: fontsData.fontsNames, isUIFontExtension: isUIFontExtension())
|
|
let extensionDefinitionClosing = "}"
|
|
|
|
generateExtensionFile(extensionHeader,
|
|
extensionDefinitionOpening,
|
|
extensionFontsNamesEnum,
|
|
extensionFontsMethods,
|
|
extensionDefinitionClosing)
|
|
|
|
print("Info.plist information:")
|
|
print("\(generatePlistUIAppFonts(fontsNames: fontsData.fontsNames))")
|
|
|
|
print("[FontTool] Font generated")
|
|
}
|
|
|
|
private func generateExtensionFile(_ args: String...) {
|
|
// Create output path
|
|
let extensionFilePath = "\(extensionOutputPath)/\(extensionName)+Font.swift"
|
|
|
|
// Create file if not exists
|
|
let fileManager = FileManager()
|
|
if fileManager.fileExists(atPath: extensionFilePath) == false {
|
|
Shell.shell("touch", "\(extensionFilePath)")
|
|
}
|
|
|
|
// Create extension content
|
|
let extensionContent = args.joined(separator: "\n")
|
|
|
|
// Write content
|
|
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
|
try! extensionContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8)
|
|
}
|
|
|
|
private func generatePlistUIAppFonts(fontsNames: [String]) -> String {
|
|
var plistData = "<key>UIAppFonts</key>\n\t<array>\n"
|
|
fontsNames
|
|
.compactMap { $0 }
|
|
.forEach {
|
|
plistData += "\t\t<string>\($0)</string>\n"
|
|
}
|
|
plistData += "\t</array>\n*/"
|
|
|
|
return plistData
|
|
}
|
|
// MARK: - Helpers
|
|
|
|
private func isUIFontExtension() -> Bool {
|
|
extensionName == Self.defaultExtensionName
|
|
}
|
|
}
|
|
|
|
FontTool.main()
|