From ee805f8067165934b7b471e8001780873f181619 Mon Sep 17 00:00:00 2001 From: Geoffrey Foster Date: Wed, 12 Jun 2019 23:09:08 -0400 Subject: [PATCH] allows sort to operate on a single locale instead of all --- .../Strings Table/StringsTableLoader.swift | 16 ++++++++++++---- Sources/stringray/Commands/LintCommand.swift | 3 +-- Sources/stringray/Commands/SortCommand.swift | 15 ++++++++++++--- Sources/stringray/Version.swift | 2 +- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Sources/RayGun/Strings Table/StringsTableLoader.swift b/Sources/RayGun/Strings Table/StringsTableLoader.swift index e82c2a8..040d562 100644 --- a/Sources/RayGun/Strings Table/StringsTableLoader.swift +++ b/Sources/RayGun/Strings Table/StringsTableLoader.swift @@ -25,12 +25,13 @@ public struct StringsTableLoader { public static let lineNumbers = Options(rawValue: 1 << 0) public static let ignoreCached = Options(rawValue: 1 << 1) + public static let singleLocale = Options(rawValue: 1 << 2) } - public var options: Options = [] + public let options: Options - public init() { - + public init(options: Options = []) { + self.options = options } public func load(url: Foundation.URL) throws -> StringsTable { @@ -51,7 +52,14 @@ public struct StringsTableLoader { cached = try? decoder.decode(CachedStringsTable.self, from: data) } - try url.lprojURLs.forEach { + let lprojURLs: [URL] + if options.contains(.singleLocale) { + lprojURLs = [URL(fileURLWithPath: "\(base.identifier).lproj", isDirectory: false, relativeTo: url)] + } else { + lprojURLs = url.lprojURLs + } + + try lprojURLs.forEach { guard let locale = $0.locale else { return } let stringsTableURL = $0.appendingPathComponent(name).appendingPathExtension("strings") diff --git a/Sources/stringray/Commands/LintCommand.swift b/Sources/stringray/Commands/LintCommand.swift index a68eefb..46041c0 100644 --- a/Sources/stringray/Commands/LintCommand.swift +++ b/Sources/stringray/Commands/LintCommand.swift @@ -157,8 +157,7 @@ struct LintCommand: Command { } private func lint(inputs: [LintInput], reporter: Reporter, config: Linter.Config) throws { - var loader = StringsTableLoader() - loader.options = [.lineNumbers] + let loader = StringsTableLoader(options: [.lineNumbers]) let linter = Linter(reporter: reporter, config: config) var violations: [LintRuleViolation] = [] diff --git a/Sources/stringray/Commands/SortCommand.swift b/Sources/stringray/Commands/SortCommand.swift index e448fab..0ed38a9 100644 --- a/Sources/stringray/Commands/SortCommand.swift +++ b/Sources/stringray/Commands/SortCommand.swift @@ -14,6 +14,7 @@ import Utility struct SortCommand: Command { private struct Arguments { var inputFile: Foundation.URL! + var allLocales: Bool = false } let command: String = "sort" let overview: String = "Sorts the given strings table alphabetically by key." @@ -24,20 +25,28 @@ struct SortCommand: Command { binder = ArgumentBinder() let subparser = parser.add(subparser: command, overview: overview) let inputFile = subparser.add(positional: "inputFile", kind: PathArgument.self, optional: false, usage: "", completion: .filename) + let allLocales = subparser.add(option: "--all-locales", shortName: "-a", kind: Bool.self, usage: "Loads all locales under the given base one to be sorted") binder.bind(positional: inputFile) { (arguments, inputFile) in arguments.inputFile = URL(fileURLWithPath: inputFile.path.asString) } + binder.bind(option: allLocales) { (arguments, allLocales) in + arguments.allLocales = allLocales + } } func run(with arguments: ArgumentParser.Result) throws { var commandArgs = Arguments() try binder.fill(parseResult: arguments, into: &commandArgs) - try sort(url: commandArgs.inputFile) + try sort(url: commandArgs.inputFile, allLocales: commandArgs.allLocales) } - private func sort(url: Foundation.URL) throws { - let loader = StringsTableLoader() + private func sort(url: Foundation.URL, allLocales: Bool) throws { + var options:StringsTableLoader.Options = [.ignoreCached] + if !allLocales { + options.insert(.singleLocale) + } + let loader = StringsTableLoader(options: options) var table = try loader.load(url: url) table.sort() try loader.write(to: url.resourceDirectory, table: table) diff --git a/Sources/stringray/Version.swift b/Sources/stringray/Version.swift index 1781604..1598837 100644 --- a/Sources/stringray/Version.swift +++ b/Sources/stringray/Version.swift @@ -1,5 +1,5 @@ import Utility extension Version { - static var current: Version = "0.3.1" + static var current: Version = "0.4.0" }