order strings alphabetically by key after downloading them

This commit is contained in:
Mauro Romito
2026-04-10 15:26:31 +02:00
committed by Mauro
parent 2d2295bcc1
commit e8a5123cc9
101 changed files with 60184 additions and 60096 deletions

View File

@@ -10,6 +10,7 @@ struct DownloadStrings: ParsableCommand {
func run() throws {
try localazyDownload()
try sortStringsFiles()
try swiftgen()
}
@@ -17,6 +18,59 @@ struct DownloadStrings: ParsableCommand {
let arguments = allLanguages ? " all" : ""
try Zsh.run(command: "localazy download\(arguments)")
}
private func sortStringsFiles() throws {
let localizationsURL = URL(fileURLWithPath: "ElementX/Resources/Localizations")
let fileManager = FileManager.default
guard let enumerator = fileManager.enumerator(at: localizationsURL,
includingPropertiesForKeys: nil) else {
return
}
for case let fileURL as URL in enumerator where fileURL.pathExtension == "strings" {
try sortStringsFile(at: fileURL)
}
}
private func sortStringsFile(at url: URL) throws {
let content = try String(contentsOf: url, encoding: .utf8)
let lines = content.components(separatedBy: .newlines)
// Separate key-value lines from other lines (comments, empty lines)
let keyValuePattern = #"^\s*"(.+)"\s*=\s*".*";\s*$"#
let regex = try NSRegularExpression(pattern: keyValuePattern)
var keyValueLines = [String]()
for line in lines {
let range = NSRange(line.startIndex..., in: line)
if regex.firstMatch(in: line, range: range) != nil {
keyValueLines.append(line)
}
}
guard !keyValueLines.isEmpty else { return }
let sortedLines = keyValueLines.sorted { lhs, rhs in
// Extract keys for comparison
guard let lhsKey = extractKey(from: lhs),
let rhsKey = extractKey(from: rhs) else {
return lhs < rhs
}
return lhsKey.localizedStandardCompare(rhsKey) == .orderedAscending
}
let sortedContent = sortedLines.joined(separator: "\n") + "\n"
try sortedContent.write(to: url, atomically: true, encoding: .utf8)
}
private func extractKey(from line: String) -> String? {
guard let openQuote = line.firstIndex(of: "\"") else { return nil }
let afterOpen = line.index(after: openQuote)
guard let closeQuote = line[afterOpen...].firstIndex(of: "\"") else { return nil }
return String(line[afterOpen..<closeQuote])
}
private func swiftgen() throws {
try Zsh.run(command: "swiftgen config run --config Tools/SwiftGen/swiftgen-config.yml")