Refactor + Bugs fixes on Strings + Gestion de la génération des images
This commit is contained in:
@ -9,89 +9,86 @@ import Foundation
|
||||
import CLIToolCore
|
||||
import ArgumentParser
|
||||
|
||||
extension Strings {
|
||||
struct Twine: ParsableCommand {
|
||||
static var configuration = CommandConfiguration(abstract: "Generate strings with twine.")
|
||||
|
||||
struct Twine: ParsableCommand {
|
||||
static var configuration = CommandConfiguration(abstract: "Generate strings with twine.")
|
||||
static let toolName = "Twine"
|
||||
static let defaultExtensionName = "String"
|
||||
static let twineExecutable = "\(FileManager.default.homeDirectoryForCurrentUser.relativePath)/scripts/twine/twine"
|
||||
|
||||
var langs: [String] { options.langsRaw.split(separator: " ").map { String($0) } }
|
||||
var inputFilenameWithoutExt: String { URL(fileURLWithPath: options.inputFile)
|
||||
.deletingPathExtension()
|
||||
.lastPathComponent
|
||||
}
|
||||
|
||||
// The `@OptionGroup` attribute includes the flags, options, and
|
||||
// arguments defined by another `ParsableArguments` type.
|
||||
@OptionGroup var options: TwineOptions
|
||||
|
||||
mutating func run() {
|
||||
print("[\(Self.toolName)] Starting strings generation")
|
||||
|
||||
static let toolName = "Twine"
|
||||
static let defaultExtensionName = "String"
|
||||
static let twineExecutable = "\(FileManager.default.homeDirectoryForCurrentUser.relativePath)/scripts/twine/twine"
|
||||
// Check requirements
|
||||
guard checkRequirements() else { return }
|
||||
|
||||
var langs: [String] { options.langsRaw.split(separator: " ").map { String($0) } }
|
||||
var inputFilenameWithoutExt: String { URL(fileURLWithPath: options.inputFile)
|
||||
.deletingPathExtension()
|
||||
.lastPathComponent
|
||||
}
|
||||
print("[\(Self.toolName)] Will generate strings")
|
||||
|
||||
// The `@OptionGroup` attribute includes the flags, options, and
|
||||
// arguments defined by another `ParsableArguments` type.
|
||||
@OptionGroup var options: TwineOptions
|
||||
|
||||
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 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" }
|
||||
// Generate strings files (lproj files)
|
||||
for lang in langs {
|
||||
Shell.shell(Self.twineExecutable,
|
||||
"generate-localization-file", options.inputFile,
|
||||
"--format", "apple-swift",
|
||||
"--lang", "\(options.defaultLang)",
|
||||
extensionFilePath,
|
||||
"--lang", "\(lang)",
|
||||
"\(options.outputPath)/\(lang).lproj/\(inputFilenameWithoutExt).strings",
|
||||
"--tags=ios,iosonly,iosOnly")
|
||||
|
||||
print("[\(Self.toolName)] Strings generated")
|
||||
}
|
||||
|
||||
// MARK: - Requirements
|
||||
// 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")
|
||||
|
||||
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 langs.isEmpty == false else {
|
||||
let error = TwineError.langsListEmpty
|
||||
print(error.localizedDescription)
|
||||
Twine.exit(withError: error)
|
||||
}
|
||||
|
||||
guard 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
|
||||
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 langs.isEmpty == false else {
|
||||
let error = TwineError.langsListEmpty
|
||||
print(error.localizedDescription)
|
||||
Twine.exit(withError: error)
|
||||
}
|
||||
|
||||
guard 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
|
||||
}
|
||||
}
|
||||
|
@ -7,25 +7,21 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Strings {
|
||||
enum TwineError: Error {
|
||||
case fileNotExists(String)
|
||||
case langsListEmpty
|
||||
case defaultLangsNotInLangs
|
||||
|
||||
enum TwineError: Error {
|
||||
case fileNotExists(String)
|
||||
case langsListEmpty
|
||||
case defaultLangsNotInLangs
|
||||
|
||||
var localizedDescription: String {
|
||||
switch self {
|
||||
case .fileNotExists(let filename):
|
||||
return " error:[\(Twine.toolName)] File \(filename) does not exists "
|
||||
|
||||
case .langsListEmpty:
|
||||
return " error:[\(Twine.toolName)] Langs list is empty"
|
||||
|
||||
case .defaultLangsNotInLangs:
|
||||
return " error:[\(Twine.toolName)] Langs list does not contains the default lang"
|
||||
}
|
||||
var localizedDescription: String {
|
||||
switch self {
|
||||
case .fileNotExists(let filename):
|
||||
return " error:[\(Twine.toolName)] File \(filename) does not exists "
|
||||
|
||||
case .langsListEmpty:
|
||||
return " error:[\(Twine.toolName)] Langs list is empty"
|
||||
|
||||
case .defaultLangsNotInLangs:
|
||||
return " error:[\(Twine.toolName)] Langs list does not contains the default lang"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import Foundation
|
||||
import ArgumentParser
|
||||
|
||||
struct TwineOptions: ParsableArguments {
|
||||
@Flag(name: .customShort("f"), help: "Should force generation")
|
||||
@Flag(name: [.customShort("f"), .customShort("F")], help: "Should force generation")
|
||||
var forceGeneration = false
|
||||
|
||||
@Argument(help: "Input files where strings ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
|
Reference in New Issue
Block a user