109 lines
4.0 KiB
Swift
109 lines
4.0 KiB
Swift
//
|
|
// Stringium.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 10/01/2022.
|
|
//
|
|
|
|
import Foundation
|
|
import CLIToolCore
|
|
import ArgumentParser
|
|
|
|
struct Stringium: ParsableCommand {
|
|
static var configuration = CommandConfiguration(abstract: "Generate strings with custom scripts.")
|
|
|
|
static let toolName = "Stringium"
|
|
static let defaultExtensionName = "String"
|
|
static let noTranslationTag: String = "notranslation"
|
|
|
|
var extensionFileName: String { "\(options.extensionName)+String\(options.extensionSuffix).swift" }
|
|
var extensionFilePath: String { "\(options.extensionOutputPath)/\(extensionFileName)" }
|
|
|
|
var langs: [String] {
|
|
options.langsRaw
|
|
.split(separator: " ")
|
|
.map { String($0) }
|
|
}
|
|
var inputFilenameWithoutExt: String {
|
|
URL(fileURLWithPath: options.inputFile)
|
|
.deletingPathExtension()
|
|
.lastPathComponent
|
|
}
|
|
var stringsFileOutputPath: String {
|
|
var outputPath = options.outputPathRaw
|
|
if outputPath.last == "/" {
|
|
outputPath = String(outputPath.dropLast())
|
|
}
|
|
return outputPath
|
|
}
|
|
|
|
// The `@OptionGroup` attribute includes the flags, options, and
|
|
// arguments defined by another `ParsableArguments` type.
|
|
@OptionGroup var options: StringiumOptions
|
|
|
|
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: langs,
|
|
defaultLang: options.defaultLang,
|
|
tags: ["ios", "iosonly", Self.noTranslationTag],
|
|
outputPath: stringsFileOutputPath,
|
|
inputFilenameWithoutExt: inputFilenameWithoutExt)
|
|
|
|
// Generate extension
|
|
StringsFileGenerator.writeExtensionFiles(sections: sections,
|
|
defaultLang: options.defaultLang,
|
|
tags: ["ios", "iosonly", Self.noTranslationTag],
|
|
staticVar: options.extensionName == Self.defaultExtensionName,
|
|
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 langs.isEmpty == false else {
|
|
let error = StringiumError.langsListEmpty
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
|
|
guard 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
|
|
}
|
|
}
|