Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
96 lines
3.1 KiB
Swift
96 lines
3.1 KiB
Swift
//
|
|
// FontTool.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
import Foundation
|
|
import ToolCore
|
|
import ArgumentParser
|
|
|
|
struct FontTool: ParsableCommand {
|
|
|
|
// MARK: - CommandConfiguration
|
|
|
|
static var configuration = CommandConfiguration(
|
|
abstract: "A utility to generate an helpful etension to access your custom font from code and also Info.plist UIAppsFont content.",
|
|
version: "0.1.0"
|
|
)
|
|
|
|
// MARK: - Static
|
|
|
|
static let toolName = "FontTool"
|
|
static let defaultExtensionName = "UIFont"
|
|
|
|
// MARK: - Properties
|
|
|
|
var extensionFileName: String {
|
|
if options.extensionSuffix.isEmpty == false {
|
|
return "\(options.extensionName)+\(options.extensionSuffix).swift"
|
|
}
|
|
return "\(options.extensionName).swift"
|
|
}
|
|
var extensionFilePath: String { "\(options.extensionOutputPath)/\(extensionFileName)" }
|
|
var generateStaticVariable: Bool {
|
|
options.extensionName == Self.defaultExtensionName
|
|
}
|
|
|
|
// MARK: - Command Options
|
|
|
|
@OptionGroup var options: FontOptions
|
|
|
|
// MARK: - Run
|
|
|
|
public func run() throws {
|
|
print("[\(Self.toolName)] Starting fonts generation")
|
|
|
|
// Check requirements
|
|
guard checkRequirements() else { return }
|
|
|
|
print("[\(Self.toolName)] Will generate fonts")
|
|
|
|
// Get fonts to generate
|
|
let fontsToGenerate = FontFileParser.parse(options.inputFile)
|
|
|
|
// Get real font names
|
|
let inputFolder = URL(fileURLWithPath: options.inputFile).deletingLastPathComponent().relativePath
|
|
let fontsNames = FontToolHelper.getFontPostScriptName(for: fontsToGenerate,
|
|
inputFolder: inputFolder)
|
|
|
|
// Generate extension
|
|
FontExtensionGenerator.writeExtensionFile(fontsNames: fontsNames,
|
|
staticVar: generateStaticVariable,
|
|
extensionName: options.extensionName,
|
|
extensionFilePath: extensionFilePath)
|
|
|
|
print("Info.plist information:")
|
|
print("\(FontPlistGenerator.generatePlistUIAppsFontContent(for: fontsNames))")
|
|
|
|
print("[\(Self.toolName)] Fonts generated")
|
|
}
|
|
|
|
// MARK: - Requirements
|
|
|
|
private func checkRequirements() -> Bool {
|
|
let fileManager = FileManager()
|
|
|
|
// Check input file exists
|
|
guard fileManager.fileExists(atPath: options.inputFile) else {
|
|
let error = FontToolError.fileNotExists(options.inputFile)
|
|
print(error.localizedDescription)
|
|
FontTool.exit(withError: error)
|
|
}
|
|
|
|
// Check if needed to regenerate
|
|
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration, inputFilePath: options.inputFile, extensionFilePath: extensionFilePath) else {
|
|
print("[\(Self.toolName)] Fonts are already up to date :) ")
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
FontTool.main()
|