-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings-sweeper.swift
107 lines (87 loc) · 3.28 KB
/
strings-sweeper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Foundation
// Determines unused localization keys by scanning Swift files in the project
// Optionally removes unused keys from the specified language file
// Creates a backup of the original language file before removal
let args = CommandLine.arguments
var langFile = "./Localizable.strings"
var shouldRemoveKeys = false
if args.contains("--lang") {
if let index = args.firstIndex(of: "--lang"), index + 1 < args.count {
langFile = args[index + 1]
}
}
if args.contains("--remove") {
shouldRemoveKeys = true
}
// Backup language file with a timestamp
func backupLanguageFile() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMddHHmmss"
let timestamp = dateFormatter.string(from: Date())
let backupFile = langFile + ".bak" + timestamp
do {
try FileManager.default.copyItem(atPath: langFile, toPath: backupFile)
print("Backup created: \(backupFile)")
} catch {
print("Error: Failed to create a backup of the language file.")
exit(1)
}
}
// Read the en lang file
guard let langContent = try? String(contentsOfFile: langFile) else {
print("Error: Unable to read the language file at \(langFile)")
exit(1)
}
var keys = Set<String>()
var lines = [(String, String)]()
var rootKey = ""
langContent.enumerateLines { line, _ in
let stripped = line.trimmingCharacters(in: .whitespaces)
if stripped.isEmpty {
lines.append(("", line))
return
}
var langKey = ""
if stripped.hasPrefix(".") {
let split = stripped.components(separatedBy: " = ")
langKey = rootKey + split[0]
keys.insert(langKey)
} else {
rootKey = stripped.components(separatedBy: " = ")[0]
langKey = rootKey
keys.insert(rootKey)
}
lines.append((langKey, line))
}
// Collect all Swift files
if let files = try? FileManager.default.subpathsOfDirectory(atPath: FileManager.default.currentDirectoryPath) {
// Remove build files
let filteredFiles = files.filter { !$0.hasPrefix("target/") && $0.hasSuffix(".swift") }
// Remove dynamically created keys, e.g., toast action keys
keys = keys.filter { !$0.hasPrefix("toast_actions") }
// Check Swift files if the language key is used
for file in filteredFiles {
if let content = try? String(contentsOfFile: file) {
keys = keys.filter { !content.contains($0) }
}
}
// Print unused keys
let unusedKeys = keys.joined(separator: "\n")
print(unusedKeys.isEmpty ? "No unused keys found." : "Unused keys:\n\(unusedKeys)")
if shouldRemoveKeys {
// Filter out the lines with unused keys
let modifiedLines = lines.filter { !keys.contains($0.0) }.map { $0.1 }
// Join the modified lines into a single string
let modifiedContent = modifiedLines.joined(separator: "\n")
// Backup the original language file
backupLanguageFile()
// Write the modified content back to the language file
do {
try modifiedContent.write(toFile: langFile, atomically: false, encoding: .utf8)
print("Unused keys removed from \(langFile).")
} catch {
print("Error: Failed to write modified content back to the language file.")
exit(1)
}
}
}