Thibaut Schmitt a54a264447
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Bugs fixes, Lint fixes, Refactoring
2022-08-29 13:38:49 +02:00

117 lines
3.8 KiB
Swift

//
// Imagium.swift
//
//
// Created by Thibaut Schmitt on 24/01/2022.
//
import Foundation
import ArgumentParser
import ToolCore
struct Imagium: ParsableCommand {
// MARK: - CommandConfiguration
static var configuration = CommandConfiguration(
abstract: "A utility for generate images and an extension to access them easily.",
version: "0.1.0"
)
// MARK: - Static
static let toolName = "Imagium"
static let defaultExtensionName = "UIImage"
// MARK: - Properties
var extensionFileName: String { "\(options.extensionName)+\(options.extensionSuffix).swift" }
var extensionFilePath: String { "\(options.extensionOutputPath)/\(extensionFileName)" }
var inputFilenameWithoutExt: String {
URL(fileURLWithPath: options.inputFile)
.deletingPathExtension()
.lastPathComponent
}
// MARK: - Command Options
@OptionGroup var options: ImagiumOptions
// MARK: - Run
mutating func run() {
print("[\(Self.toolName)] Starting images generation")
// Check requirements
guard checkRequirements() else { return }
print("[\(Self.toolName)] Will generate images")
// Parse input file
let imagesToGenerate = ImageFileParser.parse(options.inputFile, platform: PlatormTag.ios)
// Generate xcassets files
let inputFolder = URL(fileURLWithPath: options.inputFile)
.deletingLastPathComponent()
.relativePath
let xcassetsGenerator = XcassetsGenerator(forceGeneration: options.forceExecutionAndGeneration)
xcassetsGenerator.generateXcassets(inputPath: inputFolder,
imagesToGenerate: imagesToGenerate,
xcassetsPath: options.xcassetsPath)
// Generate extension
ImageExtensionGenerator.writeStringsFiles(images: imagesToGenerate,
staticVar: options.extensionName == Self.defaultExtensionName,
inputFilename: inputFilenameWithoutExt,
extensionName: options.extensionName,
extensionFilePath: extensionFilePath)
print("[\(Self.toolName)] Images generated")
}
// MARK: - Requirements
private func checkRequirements() -> Bool {
guard options.forceExecutionAndGeneration == false else {
return true
}
let fileManager = FileManager()
// Input file
guard fileManager.fileExists(atPath: options.inputFile) else {
let error = ImagiumError.fileNotExists(options.inputFile)
print(error.localizedDescription)
Imagium.exit(withError: error)
}
// RSVG-Converter
_ = Imagium.getSvgConverterPath()
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(force: options.forceExecution, inputFilePath: options.inputFile, extensionFilePath: extensionFilePath) else {
print("[\(Self.toolName)] Images are already up to date :) ")
return false
}
return true
}
// MARK: - Helpers
@discardableResult
static func getSvgConverterPath() -> String {
let taskSvgConverter = Shell.shell("which", "rsvg-convert")
if taskSvgConverter.terminationStatus == 0 {
return taskSvgConverter.output!.removeCharacters(from: CharacterSet.whitespacesAndNewlines)
}
let error = ImagiumError.rsvgConvertNotFound
print(error.localizedDescription)
Imagium.exit(withError: error)
}
}
Imagium.main()