Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
124 lines
4.5 KiB
Swift
124 lines
4.5 KiB
Swift
//
|
|
// main.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 20/12/2021.
|
|
//
|
|
|
|
import ToolCore
|
|
import Foundation
|
|
import ArgumentParser
|
|
|
|
struct ColorTool: ParsableCommand {
|
|
|
|
// MARK: - CommandConfiguration
|
|
|
|
static var configuration = CommandConfiguration(
|
|
abstract: "A utility for generate colors assets and their getters.",
|
|
version: ResgenSwiftVersion
|
|
)
|
|
|
|
// MARK: - Static
|
|
|
|
static let toolName = "ColorTool"
|
|
static let defaultExtensionName = "UIColor"
|
|
static let assetsColorsFolderName = "Colors"
|
|
|
|
// 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: ColorToolOptions
|
|
|
|
// MARK: - Run
|
|
|
|
public func run() throws {
|
|
print("[\(Self.toolName)] Starting colors generation")
|
|
print("[\(Self.toolName)] Will use inputFile \(options.inputFile) to generate \(options.colorStyle) colors in xcassets \(options.xcassetsPath)")
|
|
|
|
// Check requirements
|
|
guard checkRequirements() else { return }
|
|
|
|
print("[\(Self.toolName)] Will generate colors")
|
|
|
|
// Delete current colors
|
|
deleteCurrentColors()
|
|
|
|
// Get colors to generate
|
|
let parsedColors = ColorFileParser.parse(options.inputFile,
|
|
colorStyle: options.colorStyle)
|
|
|
|
// Generate all colors in xcassets
|
|
ColorXcassetHelper.generateXcassetColors(colors: parsedColors,
|
|
to: options.xcassetsPath)
|
|
|
|
// Generate extension
|
|
ColorExtensionGenerator.writeExtensionFile(colors: parsedColors,
|
|
staticVar: generateStaticVariable,
|
|
extensionName: options.extensionName,
|
|
extensionFilePath: extensionFilePath)
|
|
|
|
print("[\(Self.toolName)] Colors generated")
|
|
}
|
|
|
|
// MARK: - Requirements
|
|
|
|
private func checkRequirements() -> Bool {
|
|
let fileManager = FileManager()
|
|
|
|
// Check if input file exists
|
|
guard fileManager.fileExists(atPath: options.inputFile) else {
|
|
let error = ColorToolError.fileNotExists(options.inputFile)
|
|
print(error.localizedDescription)
|
|
ColorTool.exit(withError: error)
|
|
}
|
|
|
|
// Check if xcassets file exists
|
|
guard fileManager.fileExists(atPath: options.xcassetsPath) else {
|
|
let error = ColorToolError.fileNotExists(options.xcassetsPath)
|
|
print(error.localizedDescription)
|
|
ColorTool.exit(withError: error)
|
|
}
|
|
|
|
// Check if needed to regenerate
|
|
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration, inputFilePath: options.inputFile, extensionFilePath: extensionFilePath) else {
|
|
print("[\(Self.toolName)] Colors are already up to date :) ")
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func deleteCurrentColors() {
|
|
Shell.shell("rm", "-rf", "\(options.xcassetsPath)/Colors/*")
|
|
}
|
|
}
|
|
|
|
ColorTool.main()
|
|
|
|
/*
|
|
Command samples:
|
|
|
|
1. UIColor extension without suffix
|
|
swift run -c release ColorToolCore -f ./SampleFiles/Colors/sampleColors1.txt --style all --xcassets-path "./SampleFiles/Colors/colors.xcassets" --extension-output-path "./SampleFiles/Colors/Generated/" --extension-name "UIColor"
|
|
|
|
2. UIColor extension with custom suffix
|
|
swift run -c release ColorToolCore -f ./SampleFiles/Colors/sampleColors1.txt --style all --xcassets-path "./SampleFiles/Colors/colors.xcassets" --extension-output-path "./SampleFiles/Colors/Generated/" --extension-name "UIColor" --extension-suffix "SampleApp"
|
|
|
|
3. Custom extension with only light theme colors (R2Color)
|
|
swift run -c release ColorToolCore -f ./SampleFiles/Colors/sampleColors1.txt --style light --xcassets-path "./SampleFiles/Colors/colors.xcassets" --extension-output-path "./SampleFiles/Colors/Generated/" --extension-name "R2Color"
|
|
*/
|