Adding input file with font to generate (iso with colors) + adding an optional extension suffix on font extension

This commit is contained in:
Thibaut Schmitt 2022-01-03 10:18:20 +01:00
parent ac280e2cb7
commit 669f8875aa
11 changed files with 139 additions and 71 deletions

View File

@ -1,4 +1,3 @@
// Lato/Lato-Italic.ttf Lato/Lato-LightItalic.ttf Lato/Lato-Thin.ttf Lato/Lato-Bold.ttf Lato/Lato-Black.ttf Lato/Lato-Regular.ttf Lato/Lato-BlackItalic.ttf Lato/Lato-BoldItalic.ttf Lato/Lato-Light.ttf Lato/Lato-ThinItalic.ttf
// Generated from FontToolCore
import UIKit

View File

@ -0,0 +1,62 @@
// Generated from FontToolCore
import UIKit
extension R2Font {
enum FontName: String {
case LatoItalic = "Lato-Italic"
case LatoLightItalic = "Lato-LightItalic"
case LatoHairline = "Lato-Hairline"
case LatoBold = "Lato-Bold"
case LatoBlack = "Lato-Black"
case LatoRegular = "Lato-Regular"
case LatoBlackItalic = "Lato-BlackItalic"
case LatoBoldItalic = "Lato-BoldItalic"
case LatoLight = "Lato-Light"
case LatoHairlineItalic = "Lato-HairlineItalic"
}
// MARK: - Getter
func LatoItalic(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoItalic.rawValue, size: size)!
}
func LatoLightItalic(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoLightItalic.rawValue, size: size)!
}
func LatoHairline(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoHairline.rawValue, size: size)!
}
func LatoBold(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoBold.rawValue, size: size)!
}
func LatoBlack(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoBlack.rawValue, size: size)!
}
func LatoRegular(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoRegular.rawValue, size: size)!
}
func LatoBlackItalic(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoBlackItalic.rawValue, size: size)!
}
func LatoBoldItalic(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoBoldItalic.rawValue, size: size)!
}
func LatoLight(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoLight.rawValue, size: size)!
}
func LatoHairlineItalic(withSize size: CGFloat) -> UIFont {
UIFont(name: FontName.LatoHairlineItalic.rawValue, size: size)!
}
}

View File

@ -0,0 +1,10 @@
Lato-Black
Lato-BlackItalic
Lato-Bold
Lato-BoldItalic
Lato-Italic
Lato-Light
Lato-LightItalic
Lato-Regular
Lato-Thin
Lato-ThinItalic

View File

@ -0,0 +1,3 @@
Lato-Black
Lato-Bold
Lato-Regular

View File

@ -1,7 +1,15 @@
#/bin/bash
# Font
swift run -c release FontToolCore ./Fonts --extension-output-path ./Fonts/Generated --extension-name R2Font
swift run -c release FontToolCore "./Fonts/sampleFontsAll.txt" \
--extension-output-path "./Fonts/Generated" \
--extension-name "R2Font" \
--extension-suffix "GenAllScript"
# Color
swift run -c release ColorToolCore ./Colors/sampleColors1.txt --style all --xcassets-path "./Colors/colors.xcassets" --extension-output-path "./Colors/Generated/" --extension-name "UIColor" --extension-suffix "GenAllScript"
swift run -c release ColorToolCore "./Colors/sampleColors1.txt" \
--style all \
--xcassets-path "./Colors/colors.xcassets" \
--extension-output-path "./Colors/Generated/" \
--extension-name "UIColor" \
--extension-suffix "GenAllScript"

View File

@ -49,6 +49,14 @@ struct ColorTool: ParsableCommand {
print("[ColorTool] Will use inputFile \(inputFile) to generate \(colorStyle) colors in xcassets \(xcassetsPath)")
print("[ColorTool] Extension will be \(extensionFilePath)")
// Check requirements
let fileManager = FileManager()
guard fileManager.fileExists(atPath: xcassetsPath) else {
ColorTool.exit(withError: ColorToolError.fileNotExists(xcassetsPath))
}
guard fileManager.fileExists(atPath: inputFile) else {
ColorTool.exit(withError: ColorToolError.fileNotExists(xcassetsPath))
}
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(force: forceGeneration, inputFilePath: inputFile, extensionFilePath: extensionFilePath) else {
@ -57,12 +65,6 @@ struct ColorTool: ParsableCommand {
}
print("[ColorTool] Will generate colors")
// Check if Xcasset exists
let fileManager = FileManager()
guard fileManager.fileExists(atPath: xcassetsPath) else {
ColorTool.exit(withError: ColorToolError.fileNotExists(xcassetsPath))
}
// Delete current colors
Shell.shell("rm", "-rf", "\(xcassetsPath)/Colors/*")

View File

@ -11,7 +11,6 @@ class FontToolContentGenerator {
static func getExtensionHeader(fontsNames: [String]) -> String {
"""
// \(fontsNames.joined(separator: " "))
// Generated from FontToolCore
import UIKit

View File

@ -11,21 +11,28 @@ import Foundation
enum FontToolError: Error {
case fcScan(String, Int32, String?)
case inputFolderNotFound(String)
case fileNotExists(String)
var description: String {
switch self {
case .fcScan(let path, let code, let output):
return """
[FontTool]
error: [FontTool]
Error while getting fontName (fc-scan --format %{postscriptname} \(path).
fc-scan exit with \(code) and output is: \(output ?? "no output")
"""
case .inputFolderNotFound(let inputFolder):
return """
[ColorTool]
error: [FontTool]
Input folder not found: \(inputFolder)
"""
case .fileNotExists(let filename):
return """
error: [FontTool]
File \(filename) does not exists
"""
}
}
}

View File

@ -12,7 +12,6 @@ class FontToolHelper {
static func getFontsFilenames(fromInputFolder inputFolder: String) -> [String] {
// Get a enumerator for all files
let fileManager = FileManager()
guard fileManager.fileExists(atPath: inputFolder) else {
FontTool.exit(withError: FontToolError.inputFolderNotFound(inputFolder))
}

View File

@ -1,49 +0,0 @@
//
// 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
}
}

View File

@ -13,15 +13,15 @@ import ArgumentParser
Lire l'infoPlist et check si les fonts dedans sont les memes que celles à générer
*/
//swift run -c release FontToolCore ./SampleFiles/Fonts --extension-output-path ~/Desktop --extension-name R2Font
//swift run -c release FontToolCore ./SampleFiles/Fonts/sampleFonts.txt --extension-output-path ~/Desktop --extension-name R2Font
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
@Argument(help: "Input files where fonts ared defined.")
var inputFile: String
@Option(help: "Path where to generate the extension.")
var extensionOutputPath: String
@ -29,22 +29,45 @@ 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" }
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")
var extensionSuffix: String = ""
var extensionFileName: String { "\(extensionName)+Font\(extensionSuffix).swift" }
var extensionFilePath: String { "\(extensionOutputPath)/\(extensionFileName)" }
public func run() throws {
print("[FontTool] Starting fonts generation")
let fontsFilenames = FontToolHelper.getFontsFilenames(fromInputFolder: inputFolder)
// Check requirements
let fileManager = FileManager()
guard fileManager.fileExists(atPath: inputFile) else {
FontTool.exit(withError: FontToolError.fileNotExists(inputFile))
}
// Check if needed to regenerate
guard GeneratorFontChecker.shouldGenerate(force: forceGeneration, inputFilenames: fontsFilenames, extensionFilePath: extensionFilePath) else {
guard GeneratorChecker.shouldGenerate(force: forceGeneration, inputFilePath: inputFile, extensionFilePath: extensionFilePath) else {
print("[FontTool] Fonts are already up to date :) ")
return
}
print("[FontTool] Will generate fonts")
// Get fonts to generate
let fontsToGenerate = getFontsToGenerate()
let inputFolder = URL(fileURLWithPath: inputFile).deletingLastPathComponent().relativePath
let fontsFilenames = FontToolHelper
.getFontsFilenames(fromInputFolder: inputFolder)
.filter { fontNameWithPath in
let fontName = URL(fileURLWithPath: fontNameWithPath)
.deletingPathExtension()
.lastPathComponent
if fontsToGenerate.contains(fontName) {
return true
}
return false
}
let fontsFilesnamesWithPath = fontsFilenames.map { "\(inputFolder)/\($0)" }
let fontsNames = FontToolHelper.getFontsNames(fontsFileNames: fontsFilesnamesWithPath)
@ -96,6 +119,11 @@ struct FontTool: ParsableCommand {
}
// MARK: - Helpers
private func getFontsToGenerate() -> [String] {
let inputFileContent = try! String(contentsOfFile: inputFile, encoding: .utf8)
return inputFileContent.components(separatedBy: .newlines)
}
private func isUIFontExtension() -> Bool {
extensionName == Self.defaultExtensionName
}
@ -106,9 +134,9 @@ FontTool.main()
/*
1. R2Font extension
swift run -c release FontToolCore ./SampleFiles/Fonts --extension-output-path ./SampleFiles/Fonts/Generated --extension-name R2Font
swift run -c release FontToolCore ./SampleFiles/Fonts/sampleFonts.txt --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
swift run -c release FontToolCore ./SampleFiles/Fonts/sampleFonts.txt --extension-output-path ./SampleFiles/Fonts/Generated
*/