-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.swift
executable file
·335 lines (271 loc) · 10.9 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env xcrun -sdk macosx swift
//
// Rumpelstiltskin.swift
// Rumpelstiltskin
//
// Created by Christian Braun on 01.07.19.
// Copyright KURZ Digital Solutions GmbH & Co. KG
//
import Foundation
public typealias NodeKey = String
public typealias NodeValue = String
//from NSHipster - http://nshipster.com/swift-literal-convertible/
struct Regex {
let pattern: String
let options: NSRegularExpression.Options?
private var matcher: NSRegularExpression? {
return try? NSRegularExpression(pattern: pattern, options: options ?? [])
}
init(pattern: String, options: NSRegularExpression.Options? = nil) {
self.pattern = pattern
self.options = options
}
func match(string: String, options: NSRegularExpression.MatchingOptions? = nil) -> Bool {
return self.matcher?.numberOfMatches(in: string, options: options ?? [], range: NSMakeRange(0, string.utf16.count)) != 0
}
}
protocol RegularExpressionMatchable {
func match(regex: Regex) -> Bool
}
extension String: RegularExpressionMatchable {
func match(regex: Regex) -> Bool {
return regex.match(string: self)
}
}
func ~=<T: RegularExpressionMatchable>(pattern: Regex, matchable: T) -> Bool {
return matchable.match(regex: pattern)
}
public protocol ValueBuilder {
var definitionPattern: String { get }
var valuePattern: String { get }
func build(for key: NodeKey, value: NodeValue) -> (definition: String, value: String)
func applies(to value: NodeValue) -> Bool
}
public struct StringValueBuilder: ValueBuilder, Sendable {
public let definitionPattern: String = "public static let {{variableName}} = "
public let valuePattern: String = "NSLocalizedString(\"{{key}}\", tableName: nil, bundle: Bundle.main, value: \"\", comment: \"\")\n"
public let valuePatternSPM: String = "NSLocalizedString(\"{{key}}\", tableName: nil, bundle: Bundle.module, value: \"\", comment: \"\")\n"
public func build(for key: NodeKey, value: NodeValue) -> (definition: String, value: String) {
let variableName = key.split(separator: ".").last!
let isSPM = CommandLine.arguments.count > 3 && CommandLine.arguments[3] == "SPM"
return (
definitionPattern
.replacingOccurrences(of: "{{variableName}}", with: variableName),
(isSPM ? valuePatternSPM : valuePattern)
.replacingOccurrences(of: "{{key}}", with: key))
}
public func applies(to value: NodeValue) -> Bool {
return true
}
}
public struct FunctionValueBuilder: ValueBuilder, Sendable {
public var definitionPattern: String = "public static func {{functionName}}({{functionParams}}) -> String"
public var valuePattern: String = """
{
return String(format: {{stringValueBuilder}}, {{formatParams}})
}\n
"""
public func build(for key: NodeKey, value: NodeValue) -> (definition: String, value: String) {
let functionName = key.split(separator: ".").last!
let parameters = createFunctionParameters(fromFormatParameters: formatPlaceholders(from: value))
var functionParams = [String]()
for (index, nameAndType) in parameters.enumerated() {
functionParams.append(index == 0 ?
"\(nameAndType.name): \(nameAndType.type)" :
"_ \(nameAndType.name): \(nameAndType.type)")
}
let formatParams = parameters.map { nameAndType in
return nameAndType.name
}.joined(separator: ", ")
return (
definitionPattern
.replacingOccurrences(of: "{{functionName}}", with: functionName)
.replacingOccurrences(of: "{{functionParams}}", with: functionParams.joined(separator: ", ")),
valuePattern
.replacingOccurrences(
of: "{{stringValueBuilder}}",
with: Rumpelstiltskin.stringValueBuilder.build(for: key, value: value).value)
.replacingOccurrences(of: "{{formatParams}}", with: formatParams)
)
}
public func applies(to value: NodeValue) -> Bool {
return value.range(of: #"%([@df]|(\.\d*f))"#, options: .regularExpression) != nil
}
func formatPlaceholders(from string: String) -> [String] {
let regex = try? NSRegularExpression(pattern: #"%([@df]|(\.\d*f))"#, options: [])
let results = regex?.matches(
in: string,
options: [],
range: NSRange(string.startIndex..., in: string))
let formatPlaceholders: [String] = results?.compactMap { result in
guard let range = Range(result.range, in: string) else { return nil }
return String(string[range])
} ?? [String]()
return formatPlaceholders
}
func createFunctionParameters(fromFormatParameters formatParameters: [String]) -> [(name: String, type: String)] {
var result = [(String, String)]()
for (index, formatParameter) in formatParameters.enumerated() {
var type = ""
switch formatParameter {
case "%@":
type = "String"
case "%d":
type = "Int"
case Regex(pattern: "(%(\\.*\\d*f)"):
type = "Float"
default:
continue
}
result.append(("value\(index + 1)", type))
}
return result
}
}
public enum IndentationType {
case spaces(tabSize: Int)
case tabs
fileprivate var string: String {
switch self {
case .spaces(let tabSize):
return String(repeating: " ", count: tabSize)
case .tabs:
return "\t"
}
}
}
public struct Indentation {
let indentationType: IndentationType
public init(indentationType: IndentationType) {
self.indentationType = indentationType
}
public func indent(_ value: String) -> String {
let lines = value.split(separator: "\n")
var result = [String]()
var indentationLevel = 0
for line in lines {
if line.contains("}") {
indentationLevel -= 1
}
result.append("\(String(repeating: indentationType.string, count: indentationLevel))\(line)")
if line.contains("{") {
indentationLevel += 1
}
}
return result.joined(separator: "\n")
}
}
public class StringNode {
let key: NodeKey
var value: NodeValue?
var references = [NodeValue: StringNode]()
public var valueBuilderInOrderOfAppliance: [ValueBuilder] = [
Rumpelstiltskin.functionValueBuilder,
Rumpelstiltskin.stringValueBuilder]
init(with key: NodeKey) {
self.key = key
}
public func swiftCode() -> String {
return """
// This code was generated by Rumpelstiltskin
// Do not modify anything by hand and run the main.swift skript instead
import Foundation
public struct Localizations {
\(swiftCodeSkipRoot())
}
"""
}
private func swiftCodeSkipRoot() -> String {
var result = ""
for reference in references.sorted(by: { a, b -> Bool in
return a.key < b.key
}) {
result += reference.value.swiftCode(combinedKey: "")
}
return result
}
private func swiftCode(combinedKey: String) -> String {
var tempCombinedKey = combinedKey
tempCombinedKey.append("\(key)")
if let value = value {
for valueBuilder in valueBuilderInOrderOfAppliance {
if valueBuilder.applies(to: value) {
let definitionAndValue = valueBuilder.build(for: tempCombinedKey, value: value)
let baseTranslationComment = "/// Base translation: \(value)"
return "\(baseTranslationComment)\n\(definitionAndValue.definition)\(definitionAndValue.value)"
}
}
return "No ValueBuilder applied"
}
tempCombinedKey.append(".")
var result = "struct \(key) {\n"
for reference in references.sorted(by: { a, b -> Bool in
return a.key < b.key
}) {
result += reference.value.swiftCode(combinedKey: tempCombinedKey)
}
result += "}\n"
return result
}
}
public class Rumpelstiltskin {
public static let stringValueBuilder = StringValueBuilder()
public static let functionValueBuilder = FunctionValueBuilder()
public static func extractStructure(from content: String) -> StringNode {
print("Begin extracting structure from Localization file")
let lines = content.components(separatedBy: "\n")
let structure = StringNode(with: "")
var lineInBlockComment = false
for line in lines {
// Ignore comments
if line.starts(with: "/*") {
lineInBlockComment = true
}
if line.contains("*/") {
lineInBlockComment = false
continue
}
if line.starts(with: "//")
|| lineInBlockComment
|| line.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
continue
}
let components = extractComponents(fromLine: line)
var currentNode = structure
for component in components.hirarchie {
let node = StringNode(with: component)
if let nodeToProcess = currentNode.references[component] {
currentNode = nodeToProcess
} else {
currentNode.references[component] = node
currentNode = node
}
}
currentNode.value = components.value
}
print("Done extracting structure")
return structure
}
static func extractComponents(fromLine line: String) -> (hirarchie: [String], value: String) {
let parts = line
.trimmingCharacters(in: .whitespacesAndNewlines)
.dropLast()
.replacingOccurrences(of: "\"", with: "")
.replacingOccurrences(of: ";", with: "")
.split(separator: "=")
let firstPart = parts[0].trimmingCharacters(in: .whitespacesAndNewlines)
let valuePart = parts[1].trimmingCharacters(in: .whitespacesAndNewlines)
return (firstPart.components(separatedBy: "."), valuePart)
}
}
func run() throws {
assert(CommandLine.arguments.count >= 3)
let data = try Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))
let dataAsString = String(data: data, encoding: .utf8)!
print("Using \(CommandLine.arguments[1]) as Localization File")
print("Writing to \(CommandLine.arguments[2])")
let code = Rumpelstiltskin.extractStructure(from: dataAsString).swiftCode()
let indentedCode = Indentation(indentationType: .spaces(tabSize: 4)).indent(code)
try indentedCode.data(using: .utf8)?.write(to: URL(string: "file://"+CommandLine.arguments[2])!)
}
try run()