Add checker class to check if generation of font are required + Split CLIToolCore in multiple file (one by class)
This commit is contained in:
@ -11,8 +11,8 @@ class FontToolContentGenerator {
|
||||
|
||||
static func getExtensionHeader(fontsNames: [String]) -> String {
|
||||
"""
|
||||
// Generated from FontToolCore
|
||||
// \(fontsNames.joined(separator: " "))
|
||||
// Generated from FontToolCore
|
||||
|
||||
import UIKit
|
||||
|
||||
|
@ -9,7 +9,7 @@ import Foundation
|
||||
import CLIToolCore
|
||||
|
||||
class FontToolHelper {
|
||||
static func getFontsData(fromInputFolder inputFolder: String) -> (fontsNames: [String], fontsFileNames: [String]) {
|
||||
static func getFontsFilenames(fromInputFolder inputFolder: String) -> [String] {
|
||||
// Get a enumerator for all files
|
||||
let fileManager = FileManager()
|
||||
|
||||
@ -28,10 +28,12 @@ class FontToolHelper {
|
||||
return false
|
||||
}
|
||||
|
||||
return fontsFileNames
|
||||
}
|
||||
|
||||
static func getFontsNames(fontsFileNames: [String]) -> [String] {
|
||||
// Get font name (font name and font file name can be different)
|
||||
let fontsNames = fontsFileNames.compactMap { getFontName(atPath: "\(inputFolder)/\($0)") }
|
||||
|
||||
return (fontsNames: fontsNames, fontsFileNames: fontsFileNames)
|
||||
fontsFileNames.compactMap { getFontName(atPath: $0) }
|
||||
}
|
||||
|
||||
private static func getFontName(atPath path: String) -> String {
|
||||
|
49
Sources/FontToolCore/GeneratorFontChecker.swift
Normal file
49
Sources/FontToolCore/GeneratorFontChecker.swift
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// GeneratorFontCheck.swift
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 22/12/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class GeneratorFontChecker {
|
||||
|
||||
/// Will check if first line contains exactly all font filenames and nothinf more
|
||||
static func shouldGenerate(force: Bool, inputFilenames: [String], extensionFilePath: String) -> Bool {
|
||||
guard force == false else {
|
||||
return true
|
||||
}
|
||||
|
||||
let fileManager = FileManager()
|
||||
guard fileManager.fileExists(atPath: extensionFilePath) else {
|
||||
return true
|
||||
}
|
||||
|
||||
let extensionFileContent = try! String(contentsOfFile: extensionFilePath, encoding: .utf8)
|
||||
|
||||
guard let extensionComparableLine = extensionFileContent.components(separatedBy: .newlines).first else {
|
||||
// First line to compare unavailable -> force generation
|
||||
return true
|
||||
}
|
||||
|
||||
let extensionFontNames: [String] = extensionComparableLine
|
||||
.split(separator: " ")
|
||||
.dropFirst()
|
||||
.map { String($0) }
|
||||
|
||||
// If count is different, some fonts has been added or removed
|
||||
if inputFilenames.count != extensionFontNames.count {
|
||||
return true
|
||||
}
|
||||
|
||||
// Same number of elements, check if all fonts to generate has been already generated in extension
|
||||
for inputFile in inputFilenames {
|
||||
if extensionFontNames.contains(inputFile) == false {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
@ -17,6 +17,9 @@ import ArgumentParser
|
||||
struct FontTool: ParsableCommand {
|
||||
static let defaultExtensionName = "UIFont"
|
||||
|
||||
@Flag(name: .customShort("f"), help: "Should force generation")
|
||||
var forceGeneration = false
|
||||
|
||||
@Argument(help: "Input folder where fonts to generate are.")
|
||||
var inputFolder: String
|
||||
|
||||
@ -26,15 +29,31 @@ struct FontTool: ParsableCommand {
|
||||
@Option(help: "Extension name. If not specified, it will generate an UIFont extension")
|
||||
var extensionName: String = Self.defaultExtensionName
|
||||
|
||||
var extensionFileName: String { "\(extensionName)+Font.swift" }
|
||||
var extensionFilePath: String { "\(extensionOutputPath)/\(extensionFileName)" }
|
||||
|
||||
public func run() throws {
|
||||
print("[FontTool] Starting fonts generation")
|
||||
|
||||
let fontsData = FontToolHelper.getFontsData(fromInputFolder: inputFolder)
|
||||
|
||||
let extensionHeader = FontToolContentGenerator.getExtensionHeader(fontsNames: fontsData.fontsNames)
|
||||
let fontsFilenames = FontToolHelper.getFontsFilenames(fromInputFolder: inputFolder)
|
||||
|
||||
// Check if needed to regenerate
|
||||
guard GeneratorFontChecker.shouldGenerate(force: forceGeneration, inputFilenames: fontsFilenames, extensionFilePath: extensionFilePath) else {
|
||||
print("[FontTool] Fonts are already up to date :) ")
|
||||
return
|
||||
}
|
||||
print("[FontTool] Will generate fonts")
|
||||
|
||||
let fontsFilesnamesWithPath = fontsFilenames.map { "\(inputFolder)/\($0)" }
|
||||
let fontsNames = FontToolHelper.getFontsNames(fontsFileNames: fontsFilesnamesWithPath)
|
||||
|
||||
// Adding fontsFilenames to header (ex: path/to/font.ttf) to make check of regeneration faster
|
||||
let extensionHeader = FontToolContentGenerator.getExtensionHeader(fontsNames: fontsFilenames)
|
||||
|
||||
let extensionDefinitionOpening = "extension \(extensionName) {\n"
|
||||
let extensionFontsNamesEnum = FontToolContentGenerator.getFontNameEnum(fontsNames: fontsData.fontsNames)
|
||||
let extensionFontsMethods = FontToolContentGenerator.getFontMethods(fontsNames: fontsData.fontsNames, isUIFontExtension: isUIFontExtension())
|
||||
let extensionFontsNamesEnum = FontToolContentGenerator.getFontNameEnum(fontsNames: fontsNames)
|
||||
let extensionFontsMethods = FontToolContentGenerator.getFontMethods(fontsNames: fontsNames, isUIFontExtension: isUIFontExtension())
|
||||
let extensionDefinitionClosing = "}"
|
||||
|
||||
generateExtensionFile(extensionHeader,
|
||||
@ -44,15 +63,12 @@ struct FontTool: ParsableCommand {
|
||||
extensionDefinitionClosing)
|
||||
|
||||
print("Info.plist information:")
|
||||
print("\(generatePlistUIAppFonts(fontsNames: fontsData.fontsNames))")
|
||||
print("\(generatePlistUIAppFonts(fontsNames: fontsNames))")
|
||||
|
||||
print("[FontTool] Fonts 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 {
|
||||
@ -86,3 +102,13 @@ struct FontTool: ParsableCommand {
|
||||
}
|
||||
|
||||
FontTool.main()
|
||||
|
||||
/*
|
||||
|
||||
1. R2Font extension
|
||||
swift run -c release FontToolCore ./SampleFiles/Fonts --extension-output-path ./SampleFiles/Fonts/Generated --extension-name R2Font
|
||||
|
||||
1. UIFont defualt extension (will gen static property)
|
||||
swift run -c release FontToolCore ./SampleFiles/Fonts --extension-output-path ./SampleFiles/Fonts/Generated
|
||||
|
||||
*/
|
||||
|
Reference in New Issue
Block a user