Skip to content

Commit

Permalink
allows sort to operate on a single locale instead of all
Browse files Browse the repository at this point in the history
  • Loading branch information
g-Off committed Jun 13, 2019
1 parent 3189d89 commit ee805f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
16 changes: 12 additions & 4 deletions Sources/RayGun/Strings Table/StringsTableLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions Sources/stringray/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand Down
15 changes: 12 additions & 3 deletions Sources/stringray/Commands/SortCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -24,20 +25,28 @@ struct SortCommand: Command {
binder = ArgumentBinder<Arguments>()
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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/stringray/Version.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Utility

extension Version {
static var current: Version = "0.3.1"
static var current: Version = "0.4.0"
}

0 comments on commit ee805f8

Please sign in to comment.