diff --git a/SampleFiles/Fonts/Generated/R2Font+Font.swift b/SampleFiles/Fonts/Generated/R2Font+Font.swift index 5891e12..59b1101 100644 --- a/SampleFiles/Fonts/Generated/R2Font+Font.swift +++ b/SampleFiles/Fonts/Generated/R2Font+Font.swift @@ -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 diff --git a/SampleFiles/Fonts/Generated/R2Font+FontGenAllScript.swift b/SampleFiles/Fonts/Generated/R2Font+FontGenAllScript.swift new file mode 100644 index 0000000..59b1101 --- /dev/null +++ b/SampleFiles/Fonts/Generated/R2Font+FontGenAllScript.swift @@ -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)! + } + +} \ No newline at end of file diff --git a/SampleFiles/Fonts/sampleFontsAll.txt b/SampleFiles/Fonts/sampleFontsAll.txt new file mode 100644 index 0000000..fe6b8b3 --- /dev/null +++ b/SampleFiles/Fonts/sampleFontsAll.txt @@ -0,0 +1,10 @@ +Lato-Black +Lato-BlackItalic +Lato-Bold +Lato-BoldItalic +Lato-Italic +Lato-Light +Lato-LightItalic +Lato-Regular +Lato-Thin +Lato-ThinItalic diff --git a/SampleFiles/Fonts/sampleFontsSpecific.txt b/SampleFiles/Fonts/sampleFontsSpecific.txt new file mode 100644 index 0000000..cda44d0 --- /dev/null +++ b/SampleFiles/Fonts/sampleFontsSpecific.txt @@ -0,0 +1,3 @@ +Lato-Black +Lato-Bold +Lato-Regular diff --git a/SampleFiles/genAllRessources.sh b/SampleFiles/genAllRessources.sh index 8415a27..ee35443 100755 --- a/SampleFiles/genAllRessources.sh +++ b/SampleFiles/genAllRessources.sh @@ -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" diff --git a/Sources/ColorToolCore/main.swift b/Sources/ColorToolCore/main.swift index 9757119..b496ed7 100644 --- a/Sources/ColorToolCore/main.swift +++ b/Sources/ColorToolCore/main.swift @@ -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/*") diff --git a/Sources/FontToolCore/FontToolContentGenerator.swift b/Sources/FontToolCore/FontToolContentGenerator.swift index bdba759..9be2ede 100644 --- a/Sources/FontToolCore/FontToolContentGenerator.swift +++ b/Sources/FontToolCore/FontToolContentGenerator.swift @@ -11,7 +11,6 @@ class FontToolContentGenerator { static func getExtensionHeader(fontsNames: [String]) -> String { """ - // \(fontsNames.joined(separator: " ")) // Generated from FontToolCore import UIKit diff --git a/Sources/FontToolCore/FontToolError.swift b/Sources/FontToolCore/FontToolError.swift index 9967f29..6f9ff04 100644 --- a/Sources/FontToolCore/FontToolError.swift +++ b/Sources/FontToolCore/FontToolError.swift @@ -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 + """ } } } diff --git a/Sources/FontToolCore/FontToolHelper.swift b/Sources/FontToolCore/FontToolHelper.swift index 7b1fddf..55751c7 100644 --- a/Sources/FontToolCore/FontToolHelper.swift +++ b/Sources/FontToolCore/FontToolHelper.swift @@ -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)) } diff --git a/Sources/FontToolCore/GeneratorFontChecker.swift b/Sources/FontToolCore/GeneratorFontChecker.swift deleted file mode 100644 index 78f11c8..0000000 --- a/Sources/FontToolCore/GeneratorFontChecker.swift +++ /dev/null @@ -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 - } -} diff --git a/Sources/FontToolCore/main.swift b/Sources/FontToolCore/main.swift index 7447583..a8e7f90 100644 --- a/Sources/FontToolCore/main.swift +++ b/Sources/FontToolCore/main.swift @@ -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 */