-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.swift
56 lines (51 loc) · 1.8 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
import Foundation
import WebIDL
main()
func main() {
do {
let startTime = Date()
let idl = try IDLParser.parseIDL()
let outputPath = "Sources/WebAPIKit/Generated.swift"
var contents: [SwiftSource] = []
print("Generating bindings...")
contents.append(try IDLBuilder.generateIDLBindings(idl: idl))
print("Generating closure property wrappers...")
contents.append(try IDLBuilder.generateClosureTypes())
print("Generating JSString constants...")
contents.append(try IDLBuilder.generateStrings())
print("Generating union protocols...")
contents.append(try IDLBuilder.generateUnions())
try IDLBuilder.writeFile(
path: outputPath,
content: contents.joined(separator: "\n\n").source
)
SwiftFormatter.run(source: outputPath)
print("Done in \(Int(Date().timeIntervalSince(startTime) * 1000))ms.")
} catch {
handleDecodingError(error)
}
}
private func handleDecodingError(_ error: Error) {
switch error as? DecodingError {
case let .dataCorrupted(ctx), let .typeMismatch(_, ctx):
debugContext(ctx)
case let .valueNotFound(type, ctx):
print("Value of type \(type) not found")
debugContext(ctx)
case let .keyNotFound(key, ctx):
print("Key \(key.stringValue) not found")
debugContext(ctx)
case nil, .some:
print(error.localizedDescription)
}
exit(1)
}
private func debugContext(_ ctx: DecodingError.Context) {
print("Key path: <root>\(ctx.codingPath.map { "." + $0.stringValue }.joined())")
print(ctx.debugDescription)
if let underlying = ctx.underlyingError as NSError?,
let debugDescription = underlying.userInfo["NSDebugDescription"]
{
print(debugDescription)
}
}