Files
resgen.swift/Sources/ResgenSwift/Fonts/Fonts.swift
Thibaut Schmitt 3092376b65 Optional generation of extension (R.xx)
Optional generation of Colors/Fonts/Images/Stringium extension

Fix swiftlint warning
2025-07-17 16:04:51 +02:00

118 lines
3.5 KiB
Swift

//
// Fonts.swift
//
//
// Created by Thibaut Schmitt on 13/12/2021.
//
import ArgumentParser
import Foundation
import ToolCore
struct Fonts: 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: ResgenSwiftVersion
)
// MARK: - Static
static let toolName = "Fonts"
static let defaultExtensionName = "Font"
// MARK: - Command Options
@OptionGroup var options: FontsOptions
// MARK: - Run
func run() throws {
print("[\(Self.toolName)] Starting fonts generation")
print("[\(Self.toolName)] Will use inputFile \(options.inputFile) to generate fonts")
// 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 = FontsToolHelper.getFontPostScriptName(
for: fontsToGenerate,
inputFolder: inputFolder
)
// Generate extension
FontExtensionGenerator.writeExtensionFile(
fontsNames: fontsNames,
staticVar: options.staticMembers,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath,
isSwiftUI: true
)
if let extensionNameUIKit = options.extensionNameUIKit,
let extensionFilePathUIKit = options.extensionFilePathUIKit {
FontExtensionGenerator.writeExtensionFile(
fontsNames: fontsNames,
staticVar: options.staticMembers,
extensionName: extensionNameUIKit,
extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false
)
}
if options.infoPlistPaths.isEmpty == false {
let plistUpdateFontsData = FontPlistGenerator.generatePlistUIAppsFontContent(
for: fontsNames,
infoPlistPaths: options.infoPlistPaths
)
print("Info.plist has been updated with:")
print(plistUpdateFontsData)
}
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 = FontsToolError.fileNotExists(options.inputFile)
print(error.description)
Self.exit(withError: error)
}
// Extension for UIKit and SwiftUI should have different name
guard options.extensionName != options.extensionNameUIKit else {
let error = FontsToolError.extensionNamesCollision(options.extensionName)
print(error.description)
Self.exit(withError: error)
}
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(
force: options.forceGeneration,
inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath
) else {
print("[\(Self.toolName)] Fonts are already up to date :) ")
return false
}
return true
}
}