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

108 lines
3.5 KiB
Swift

//
// Twine.swift
//
//
// Created by Thibaut Schmitt on 10/01/2022.
//
import ToolCore
import Foundation
import ArgumentParser
struct Twine: ParsableCommand {
// MARK: - Command Configuration
static var configuration = CommandConfiguration(
abstract: "Generate strings with twine.",
version: ResgenSwiftVersion
)
// MARK: - Static
static let toolName = "Twine"
static let defaultExtensionName = "String"
static let twineExecutable = "\(FileManager.default.homeDirectoryForCurrentUser.relativePath)/scripts/twine/twine"
// MARK: - Properties
var inputFilenameWithoutExt: String { URL(fileURLWithPath: options.inputFile)
.deletingPathExtension()
.lastPathComponent
}
// MARK: - Command Options
// The `@OptionGroup` attribute includes the flags, options, and
// arguments defined by another `ParsableArguments` type.
@OptionGroup var options: TwineOptions
// MARK: - Run
mutating func run() {
print("[\(Self.toolName)] Starting strings generation")
// Check requirements
guard checkRequirements() else { return }
print("[\(Self.toolName)] Will generate strings")
// Generate strings files (lproj files)
for lang in options.langs {
Shell.shell(Self.twineExecutable,
"generate-localization-file", options.inputFile,
"--lang", "\(lang)",
"\(options.outputPath)/\(lang).lproj/\(inputFilenameWithoutExt).strings",
"--tags=ios,iosonly,iosOnly")
}
// Generate extension
var extensionFilePath: String { "\(options.extensionOutputPath)/\(inputFilenameWithoutExt).swift" }
Shell.shell(Self.twineExecutable,
"generate-localization-file", options.inputFile,
"--format", "apple-swift",
"--lang", "\(options.defaultLang)",
extensionFilePath,
"--tags=ios,iosonly,iosOnly")
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 = TwineError.fileNotExists(options.inputFile)
print(error.localizedDescription)
Twine.exit(withError: error)
}
// Langs
guard options.langs.isEmpty == false else {
let error = TwineError.langsListEmpty
print(error.localizedDescription)
Twine.exit(withError: error)
}
guard options.langs.contains(options.defaultLang) else {
let error = TwineError.defaultLangsNotInLangs
print(error.localizedDescription)
Twine.exit(withError: error)
}
// "R2String+" is hardcoded in Twine formatter
let extensionFilePathGenerated = "\(options.extensionOutputPath)/R2String+\(inputFilenameWithoutExt).swift"
// Check if needed to regenerate
guard GeneratorChecker.shouldGenerate(force: options.forceGeneration, inputFilePath: options.inputFile, extensionFilePath: extensionFilePathGenerated) else {
print("[\(Self.toolName)] Strings are already up to date :) ")
return false
}
return true
}
}