-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathJSONDiagnosticConsumer.swift
79 lines (70 loc) · 2.73 KB
/
JSONDiagnosticConsumer.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
//===--- JSONDiagnosticConsumer.swift - JSON Serializer for Diagnostics ---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
// This file provides a JSON serializer for compiler diagnostics emitted using
// the DiagnosticEngine.
//===----------------------------------------------------------------------===//
import Foundation
public final class JSONDiagnosticConsumer: DiagnosticConsumer {
/// Enumerates the possible places this consumer might output diagnostics.
private enum OutputFormat {
case url(URL)
case stdout
}
/// The current set of diagnostics that have been emitted.
private var diagnostics = [Diagnostic]()
/// The output format this consumer will output the generated JSON to.
private let outputFormat: OutputFormat
/// Creates a JSONDiagnosticConsumer that will output the generated JSON
/// to a file at the given URL.
/// - parameter outputURL: The URL that the consumer will write JSON
/// diagnostics to.
/// - note: If a file exists at the URL already, it will be overwritten.
public init(outputURL: URL) {
self.outputFormat = .url(outputURL)
}
/// Creates a JSONDiagnosticConsumer that will output the generated JSON
/// to standard output.
public init() {
self.outputFormat = .stdout
}
/// Adds the diagnostic to the list of registered diagnostics.
public func handle(_ diagnostic: Diagnostic) {
self.diagnostics.append(diagnostic)
}
/// Writes the contents of the diagnostics as JSON out to the specified
/// output location.
public func finalize() {
do {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(diagnostics)
switch outputFormat {
case .url(let url):
if FileManager.default.fileExists(atPath: url.path) {
try FileManager.default.removeItem(at: url)
}
FileManager.default.createFile(atPath: url.path, contents: data)
case .stdout:
FileHandle.standardOutput.write(data)
}
} catch {
let dummyEngine = DiagnosticEngine()
let printingConsumer = PrintingDiagnosticConsumer()
dummyEngine.addConsumer(printingConsumer)
dummyEngine.diagnose(.couldNotOutputJSON)
}
}
}
extension Diagnostic.Message {
static let couldNotOutputJSON =
Diagnostic.Message(.error, "failed emitting JSON diagnostics")
}