Skip to content

Commit ee805f8

Browse files
committed
allows sort to operate on a single locale instead of all
1 parent 3189d89 commit ee805f8

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

Sources/RayGun/Strings Table/StringsTableLoader.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public struct StringsTableLoader {
2525

2626
public static let lineNumbers = Options(rawValue: 1 << 0)
2727
public static let ignoreCached = Options(rawValue: 1 << 1)
28+
public static let singleLocale = Options(rawValue: 1 << 2)
2829
}
2930

30-
public var options: Options = []
31+
public let options: Options
3132

32-
public init() {
33-
33+
public init(options: Options = []) {
34+
self.options = options
3435
}
3536

3637
public func load(url: Foundation.URL) throws -> StringsTable {
@@ -51,7 +52,14 @@ public struct StringsTableLoader {
5152
cached = try? decoder.decode(CachedStringsTable.self, from: data)
5253
}
5354

54-
try url.lprojURLs.forEach {
55+
let lprojURLs: [URL]
56+
if options.contains(.singleLocale) {
57+
lprojURLs = [URL(fileURLWithPath: "\(base.identifier).lproj", isDirectory: false, relativeTo: url)]
58+
} else {
59+
lprojURLs = url.lprojURLs
60+
}
61+
62+
try lprojURLs.forEach {
5563
guard let locale = $0.locale else { return }
5664

5765
let stringsTableURL = $0.appendingPathComponent(name).appendingPathExtension("strings")

Sources/stringray/Commands/LintCommand.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ struct LintCommand: Command {
157157
}
158158

159159
private func lint(inputs: [LintInput], reporter: Reporter, config: Linter.Config) throws {
160-
var loader = StringsTableLoader()
161-
loader.options = [.lineNumbers]
160+
let loader = StringsTableLoader(options: [.lineNumbers])
162161
let linter = Linter(reporter: reporter, config: config)
163162
var violations: [LintRuleViolation] = []
164163

Sources/stringray/Commands/SortCommand.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Utility
1414
struct SortCommand: Command {
1515
private struct Arguments {
1616
var inputFile: Foundation.URL!
17+
var allLocales: Bool = false
1718
}
1819
let command: String = "sort"
1920
let overview: String = "Sorts the given strings table alphabetically by key."
@@ -24,20 +25,28 @@ struct SortCommand: Command {
2425
binder = ArgumentBinder<Arguments>()
2526
let subparser = parser.add(subparser: command, overview: overview)
2627
let inputFile = subparser.add(positional: "inputFile", kind: PathArgument.self, optional: false, usage: "", completion: .filename)
28+
let allLocales = subparser.add(option: "--all-locales", shortName: "-a", kind: Bool.self, usage: "Loads all locales under the given base one to be sorted")
2729

2830
binder.bind(positional: inputFile) { (arguments, inputFile) in
2931
arguments.inputFile = URL(fileURLWithPath: inputFile.path.asString)
3032
}
33+
binder.bind(option: allLocales) { (arguments, allLocales) in
34+
arguments.allLocales = allLocales
35+
}
3136
}
3237

3338
func run(with arguments: ArgumentParser.Result) throws {
3439
var commandArgs = Arguments()
3540
try binder.fill(parseResult: arguments, into: &commandArgs)
36-
try sort(url: commandArgs.inputFile)
41+
try sort(url: commandArgs.inputFile, allLocales: commandArgs.allLocales)
3742
}
3843

39-
private func sort(url: Foundation.URL) throws {
40-
let loader = StringsTableLoader()
44+
private func sort(url: Foundation.URL, allLocales: Bool) throws {
45+
var options:StringsTableLoader.Options = [.ignoreCached]
46+
if !allLocales {
47+
options.insert(.singleLocale)
48+
}
49+
let loader = StringsTableLoader(options: options)
4150
var table = try loader.load(url: url)
4251
table.sort()
4352
try loader.write(to: url.resourceDirectory, table: table)

Sources/stringray/Version.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Utility
22

33
extension Version {
4-
static var current: Version = "0.3.1"
4+
static var current: Version = "0.4.0"
55
}

0 commit comments

Comments
 (0)