Thibaut Schmitt eef92dc569
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Uniformisation of version number (0.9)
2022-08-29 13:43:06 +02:00

121 lines
4.1 KiB
Swift

//
// Stringium.swift
//
//
// Created by Thibaut Schmitt on 10/01/2022.
//
import ToolCore
import Foundation
import ArgumentParser
struct Stringium: ParsableCommand {
// MARK: - Command Configuration
static var configuration = CommandConfiguration(
abstract: "Generate strings with custom scripts.",
version: ResgenSwiftVersion
)
// MARK: - Static
static let toolName = "Stringium"
static let defaultExtensionName = "String"
static let noTranslationTag: String = "notranslation"
// 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 inputFilenameWithoutExt: String {
URL(fileURLWithPath: options.inputFile)
.deletingPathExtension()
.lastPathComponent
}
var generateStaticVariable: Bool {
options.extensionName == Self.defaultExtensionName
}
// MARK: - Command options
// The `@OptionGroup` attribute includes the flags, options, and
// arguments defined by another `ParsableArguments` type.
@OptionGroup var options: StringiumOptions
// MARK: - Run
mutating func run() {
print("[\(Self.toolName)] Starting strings generation")
// Check requirements
guard checkRequirements() else { return }
print("[\(Self.toolName)] Will generate strings")
// Parse input file
let sections = TwineFileParser.parse(options.inputFile)
// Generate strings files
StringsFileGenerator.writeStringsFiles(sections: sections,
langs: options.langs,
defaultLang: options.defaultLang,
tags: options.tags,
outputPath: options.stringsFileOutputPath,
inputFilenameWithoutExt: inputFilenameWithoutExt)
// Generate extension
StringsFileGenerator.writeExtensionFiles(sections: sections,
defaultLang: options.defaultLang,
tags: options.tags,
staticVar: generateStaticVariable,
inputFilename: inputFilenameWithoutExt,
extensionName: options.extensionName,
extensionFilePath: extensionFilePath)
print("[\(Self.toolName)] Strings generated")
}
// MARK: - Requirements
private func checkRequirements() -> Bool {
let fileManager = FileManager()
// Input file
guard fileManager.fileExists(atPath: options.inputFile) else {
let error = StringiumError.fileNotExists(options.inputFile)
print(error.localizedDescription)
Stringium.exit(withError: error)
}
// Langs
guard options.langs.isEmpty == false else {
let error = StringiumError.langsListEmpty
print(error.localizedDescription)
Stringium.exit(withError: error)
}
guard options.langs.contains(options.defaultLang) else {
let error = StringiumError.defaultLangsNotInLangs
print(error.localizedDescription)
Stringium.exit(withError: error)
}
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration, inputFilePath: options.inputFile, extensionFilePath: extensionFilePath) else {
print("[\(Self.toolName)] Strings are already up to date :) ")
return false
}
return true
}
}