Generation des strings sans twine et generation des tags. Refactor de toutes les commandes strings (Twine, CustomStrings, Tags) dans une commande avec des sous commandes
This commit is contained in:
111
Sources/Strings/Stringium/Stringium.swift
Normal file
111
Sources/Strings/Stringium/Stringium.swift
Normal file
@ -0,0 +1,111 @@
|
||||
//
|
||||
// Stringium.swift
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 10/01/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CLIToolCore
|
||||
import ArgumentParser
|
||||
|
||||
extension Strings {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
40
Sources/Strings/Stringium/StringiumError.swift
Normal file
40
Sources/Strings/Stringium/StringiumError.swift
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// StringToolError.swift
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 05/01/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Strings {
|
||||
enum StringiumError: Error {
|
||||
case fileNotExists(String)
|
||||
case langsListEmpty
|
||||
case defaultLangsNotInLangs
|
||||
case writeFile(String, String)
|
||||
case langNotDefined(String, String, Bool)
|
||||
|
||||
var localizedDescription: String {
|
||||
switch self {
|
||||
case .fileNotExists(let filename):
|
||||
return " error:[\(Stringium.toolName)] File \(filename) does not exists "
|
||||
|
||||
case .langsListEmpty:
|
||||
return " error:[\(Stringium.toolName)] Langs list is empty"
|
||||
|
||||
case .defaultLangsNotInLangs:
|
||||
return " error:[\(Stringium.toolName)] Langs list does not contains the default lang"
|
||||
|
||||
case .writeFile(let subErrorDescription, let filename):
|
||||
return " error:[\(Stringium.toolName)] An error occured while writing content to \(filename): \(subErrorDescription)"
|
||||
|
||||
case .langNotDefined(let lang, let definitionName, let isReference):
|
||||
if isReference {
|
||||
return " error:[\(Stringium.toolName)] Reference are handled only by TwineTool. Please use it or remove reference from you strings file."
|
||||
}
|
||||
return " error:[\(Stringium.toolName)] Lang \"\(lang)\" not found for \"\(definitionName)\""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
Sources/Strings/Stringium/StringiumOptions.swift
Normal file
37
Sources/Strings/Stringium/StringiumOptions.swift
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// StringiumOptions.swift
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 10/01/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ArgumentParser
|
||||
|
||||
extension Strings {
|
||||
struct StringiumOptions: ParsableArguments {
|
||||
@Flag(name: .customShort("f"), help: "Should force generation")
|
||||
var forceGeneration = false
|
||||
|
||||
@Argument(help: "Input files where strings ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
var inputFile: String
|
||||
|
||||
@Option(name: .customLong("output-path"), help: "Path where to strings file.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
var outputPathRaw: String
|
||||
|
||||
@Option(name: .customLong("langs"), help: "Langs to generate.")
|
||||
var langsRaw: String
|
||||
|
||||
@Option(help: "Default langs.")
|
||||
var defaultLang: String
|
||||
|
||||
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
var extensionOutputPath: String
|
||||
|
||||
@Option(help: "Extension name. If not specified, it will generate an String extension. Using default extension name will generate static property.")
|
||||
var extensionName: String = Stringium.defaultExtensionName
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+String{extensionSuffix}.swift")
|
||||
var extensionSuffix: String = ""
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user