-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathPLULiteralOutput.swift
287 lines (248 loc) · 11.1 KB
/
PLULiteralOutput.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
internal func swiftLiteralDataWithPropertyList(_ plist: Any, originalFileName: String) throws -> Data {
let string = try _swiftLiteralDataWithPropertyList(plist, depth: 0, indent: true, originalFilename: originalFileName)
let withNewline = string.appending("\n")
return withNewline.data(using: .utf8)!
}
internal func objcLiteralDataWithPropertyList(_ plist: Any, originalFileName: String, newFileName: String) throws -> Data {
let string = try _objcLiteralDataWithPropertyList(plist, depth: 0, indent: true, originalFilename: originalFileName, outputFilename: newFileName)
let withNewline = string.appending(";\n")
return withNewline.data(using: .utf8)!
}
internal func objcLiteralHeaderDataWithPropertyList(_ plist: Any, originalFileName: String, newFileName: String) throws -> Data {
let result = try _objCLiteralVaribleWithPropertyList(plist, forHeader: true, originalFilename: originalFileName, outputFilename: newFileName)
// Add final semi-colon
let withNewline = result.appending(";\n")
return withNewline.data(using: .utf8)!
}
internal enum LiteralFormat {
case swift
case objc
}
internal func propertyListIsValidForLiteralFormat(_ plist: Any, format: LiteralFormat) -> Bool {
switch format {
case .swift:
return PropertyListSerialization.propertyList(plist, isValidFor: .binary)
case .objc:
if let _ = plist as? String {
return true
} else if let _ = plist as? NSNumber {
return true
} else if let array = plist as? [Any] {
for item in array {
if !propertyListIsValidForLiteralFormat(item, format: format) {
return false
}
}
return true
} else if let dictionary = plist as? [AnyHashable: Any] {
for (key, value) in dictionary {
if !propertyListIsValidForLiteralFormat(key, format: format) {
return false
}
if !propertyListIsValidForLiteralFormat(value, format: format) {
return false
}
}
return true
} else {
return false
}
}
}
// MARK: - Helpers
internal func _indentation(forDepth depth: Int, numberOfSpaces: Int = 4) -> String {
var result = ""
for _ in 0..<depth {
let spaces = repeatElement(Character(" "), count: numberOfSpaces)
result.append(contentsOf: spaces)
}
return result
}
private func varName(from file: String) -> String {
let filenameStem = file.stem
var varName = filenameStem.replacingOccurrences(of: "-", with: "_").replacingOccurrences(of: " ", with: "_")
let invalidChars = CharacterSet.symbols.union(.controlCharacters)
while let contained = varName.rangeOfCharacter(from: invalidChars) {
varName.removeSubrange(contained)
}
return varName
}
extension String {
fileprivate var escapedForQuotesAndEscapes: String {
var result = self
let knownCommonEscapes = ["\\b", "\\s", "\"", "\\w", "\\.", "\\|", "\\*", "\\)", "\\("]
for escape in knownCommonEscapes {
result = result.replacingOccurrences(of: escape, with: "\\\(escape)")
}
return result
}
}
// MARK: - ObjC
private func _objcLiteralDataWithPropertyList(_ plist: Any, depth: Int, indent: Bool, originalFilename: String, outputFilename: String) throws -> String {
var result = ""
if depth == 0 {
result.append(try _objCLiteralVaribleWithPropertyList(plist, forHeader: false, originalFilename: originalFilename, outputFilename: outputFilename))
}
if indent {
result.append(_indentation(forDepth: depth))
}
if let num = plist as? NSNumber {
return result.appending(try num.propertyListFormatted(objCStyle: true))
} else if let string = plist as? String {
return result.appending("@\"\(string.escapedForQuotesAndEscapes)\"")
} else if let array = plist as? [Any] {
result.append("@[\n")
for element in array {
result.append( try _objcLiteralDataWithPropertyList(element, depth: depth + 1, indent: true, originalFilename: originalFilename, outputFilename: outputFilename))
result.append(",\n")
}
result.append(_indentation(forDepth: depth))
result.append("]")
} else if let dictionary = plist as? [String : Any] {
result.append("@{\n")
let sortedKeys = Array(dictionary.keys).sorted(by: sortDictionaryKeys)
for key in sortedKeys {
result.append(_indentation(forDepth: depth + 1))
result.append("@\"\(key)\" : ")
let value = dictionary[key]!
let valueString = try _objcLiteralDataWithPropertyList(value, depth: depth + 1, indent: false, originalFilename: originalFilename, outputFilename: outputFilename)
result.append("\(valueString),\n")
}
result.append(_indentation(forDepth: depth))
result.append("}")
} else {
throw PLUContextError.invalidPropertyListObject("Objective-C literal syntax does not support classes of type \(type(of: plist))")
}
return result
}
private func _objCLiteralVaribleWithPropertyList(_ plist: Any, forHeader: Bool, originalFilename: String, outputFilename: String) throws -> String {
let objCName: String
if let _ = plist as? NSNumber {
objCName = "NSNumber"
} else if let _ = plist as? String {
objCName = "NSString"
} else if let _ = plist as? [Any] {
objCName = "NSArray"
} else if let _ = plist as? [AnyHashable : Any] {
objCName = "NSDictionary"
} else {
throw PLUContextError.invalidPropertyListObject("Objective-C literal syntax does not support classes of type \(type(of: plist))")
}
var result = ""
if forHeader {
result.append("#import <Foundation/Foundation.h>\n\n")
} else if outputFilename != "-" {
// Don't emit for stdout
result.append("#import \"\(outputFilename.lastComponent?.stem ?? "").h\"\n\n")
}
result.append("/// Generated from \(originalFilename.lastComponent ?? "a file")\n")
// The most common usage will be to generate things that aren't exposed to others via a public header. We default to hidden visibility so as to avoid unintended exported symbols.
result.append("__attribute__((visibility(\"hidden\")))\n")
if forHeader {
result.append("extern ")
}
result.append("\(objCName) * const \(varName(from: originalFilename))")
if !forHeader {
result.append(" = ")
}
return result
}
// MARK: - Swift
private func _swiftLiteralDataWithPropertyList(_ plist: Any, depth: Int, indent: Bool, originalFilename: String) throws -> String {
var result = ""
if depth == 0 {
result.append("/// Generated from \(originalFilename.lastComponent ?? "a file")\n")
// Previous implementation would attempt to determine dynamically if the type annotation was by checking if there was a collection of different types. For now, this just always adds it.
result.append("let \(varName(from: originalFilename))")
// Dictionaries and Arrays need to check for specific type annotation, in case they contain different types. Other types do not.
if let dictionary = plist as? [String: Any] {
var lastType: PlutilExpectType?
var needsAnnotation = false
for (_, value) in dictionary {
if let lastType {
if lastType != PlutilExpectType(propertyList: value) {
needsAnnotation = true
break
}
} else {
lastType = PlutilExpectType(propertyList: value)
}
}
if needsAnnotation {
result.append(" : [String : Any]")
}
} else if let array = plist as? [Any] {
var lastType: PlutilExpectType?
var needsAnnotation = false
for value in array {
if let lastType {
if lastType != PlutilExpectType(propertyList: value) {
needsAnnotation = true
break
}
} else {
lastType = PlutilExpectType(propertyList: value)
}
}
if needsAnnotation {
result.append(" : [Any]")
}
}
result.append(" = ")
}
if indent {
result.append(_indentation(forDepth: depth))
}
if let num = plist as? NSNumber {
result.append(try num.propertyListFormatted(objCStyle: false))
} else if let string = plist as? String {
// FEATURE: Support triple-quote when string is multi-line.
// For now, do one simpler thing and replace newlines with literal \n
let escaped = string.escapedForQuotesAndEscapes.replacingOccurrences(of: "\n", with: "\\n")
result.append("\"\(escaped)\"")
} else if let array = plist as? [Any] {
result.append("[\n")
for element in array {
result.append( try _swiftLiteralDataWithPropertyList(element, depth: depth + 1, indent: true, originalFilename: originalFilename))
result.append(",\n")
}
result.append(_indentation(forDepth: depth))
result.append("]")
} else if let dictionary = plist as? [String : Any] {
result.append("[\n")
let sortedKeys = Array(dictionary.keys).sorted(by: sortDictionaryKeys)
for key in sortedKeys {
result.append(_indentation(forDepth: depth + 1))
result.append("\"\(key)\" : ")
let value = dictionary[key]!
let valueString = try _swiftLiteralDataWithPropertyList(value, depth: depth + 1, indent: false, originalFilename: originalFilename)
result.append("\(valueString),\n")
}
result.append(_indentation(forDepth: depth))
result.append("]")
} else if let data = plist as? Data {
result.append("Data(bytes: [")
for byte in data {
result.append(String(format: "0x%02X", byte))
result.append(",")
}
result.append("])")
} else if let date = plist as? Date {
result.append("Date(timeIntervalSinceReferenceDate: \(date.timeIntervalSinceReferenceDate))")
} else {
throw PLUContextError.invalidPropertyListObject("Swift literal syntax does not support classes of type \(type(of: plist))")
}
return result
}