forked from swiftlang/swift-evolution-metadata-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractionJob.swift
More file actions
287 lines (225 loc) · 13.2 KB
/
Copy pathExtractionJob.swift
File metadata and controls
287 lines (225 loc) · 13.2 KB
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
// 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
import EvolutionMetadataModel
/* The swift-evolution-extraction tool can perform three tasks:
1. Extract: Extraction of metadata to produce a json file
2. Validate: Validation of proposals to ensure metadata can be extracted as expected (Not yet implemented)
3. Snapshot: Capture a local snapshot of inputs and expected results that can be used for local testing
An `ExtractionJob` captures all of the required inputs and potential output locations.
*/
public struct ExtractionJob: Sendable {
public enum Source: Sendable, Codable, Equatable {
case network
case snapshot(URL)
case files([URL])
case pullRequest(Int)
}
public enum Output: Sendable, Codable, Equatable {
case metadataJSON(URL)
case snapshot(URL)
case validationReport(URL)
case none
var snapshotURL: URL? {
if case let .snapshot(url) = self { url }
else { nil }
}
}
struct JobMetadata: Sendable, Codable, Equatable {
let commit: String?
let extractionDate: Date
}
let proposalSpecs: [ProposalSpec]
let previousResults: EvolutionMetadata?
let forcedExtractionIDs: [String]
let jobMetadata: JobMetadata
let output: Output
let snapshot: Snapshot?
private init(output: Output, snapshot: Snapshot?, proposalSpecs: [ProposalSpec], previousResults: EvolutionMetadata?, forcedExtractionIDs: [String], jobMetadata: JobMetadata) {
self.proposalSpecs = proposalSpecs
self.previousResults = previousResults
self.forcedExtractionIDs = forcedExtractionIDs
self.output = output
self.snapshot = snapshot
self.jobMetadata = jobMetadata
}
public func run() async throws {
// Extract Metadata
let evolutionMetadata = try await EvolutionMetadataExtractor.extractEvolutionMetadata(for: self)
// Compare results against expected results, if extraction job has them
compareResultsToExpectedValuesIfPresent(evolutionMetadata)
// Output Results
try outputResults(evolutionMetadata)
}
public static func makeExtractionJob(source: Source, output: Output, ignorePreviousResults: Bool = false, forcedExtractionIDs: [String] = [], extractionDate: Date = Date()) async throws -> ExtractionJob {
switch source {
case .network:
try await makeNetworkExtractionJob(output: output, ignorePreviousResults: ignorePreviousResults, forcedExtractionIDs: forcedExtractionIDs, extractionDate: extractionDate)
case .snapshot(let snapshotURL):
try await makeSnapshotExtractionJob(snapshotURL: snapshotURL, output: output, ignorePreviousResults: ignorePreviousResults, forcedExtractionIDs: forcedExtractionIDs, extractionDate: extractionDate)
case .files(let fileURLs):
try makeFilesExtractionJob(fileURLs: fileURLs, output: output, ignorePreviousResults: ignorePreviousResults, forcedExtractionIDs: forcedExtractionIDs, extractionDate: extractionDate)
case .pullRequest(let pullRequestID):
try await makePullRequestExtractionJob(pullRequestID: pullRequestID, output: output, ignorePreviousResults: ignorePreviousResults, forcedExtractionIDs: forcedExtractionIDs, extractionDate: extractionDate)
}
}
}
// MARK: - Gather Job Requirements & Create
extension ExtractionJob {
private static func makeNetworkExtractionJob(output: Output, ignorePreviousResults: Bool, forcedExtractionIDs: [String], extractionDate: Date) async throws -> ExtractionJob {
async let previousResults = previousResults(from: PreviousResultsFetcher.previousResultsURL, ignorePreviousResults: ignorePreviousResults)
let mainBranchInfo = try await GitHubFetcher.fetchMainBranch()
let sha = mainBranchInfo.commit.sha
let proposalContentItems = try await GitHubFetcher.fetchProposalContentItems(for: sha)
// The proposals/ directory may have subdirectories for
// proposals from specific workgroups. For now, proposals
// in those subdirectories are filtered out of this proposal
// specs array.
let proposalSpecs = proposalContentItems.enumerated().compactMap {
$1.proposalSpec(sortIndex: $0)
}
let jobMetadata = JobMetadata(commit: sha, extractionDate: extractionDate)
let snapshot: Snapshot?
if case let .snapshot(destURL) = output {
snapshot = Snapshot(sourceURL: nil, destURL: destURL, proposalListing: proposalContentItems, directoryContents: [], proposalSpecs: [], previousResults: nil, expectedResults: nil, branchInfo: mainBranchInfo, snapshotDate: extractionDate)
} else {
snapshot = nil
}
return ExtractionJob(output: output, snapshot: snapshot, proposalSpecs: proposalSpecs, previousResults: try await previousResults, forcedExtractionIDs: forcedExtractionIDs, jobMetadata: jobMetadata)
}
private static func makeSnapshotExtractionJob(snapshotURL: URL, output: Output, ignorePreviousResults: Bool, forcedExtractionIDs: [String], extractionDate: Date) async throws -> ExtractionJob {
// Argument validation should ensure correct values. Assert to catch problems in usage in tests.
assert(snapshotURL.pathExtension == "evosnapshot", "Snapshot URL must be a directory with 'evosnapshot' extension.")
verbosePrint("Using local snapshot\n'\(snapshotURL.relativePath)'")
let sourceSnapshot = try await Snapshot.makeSnapshot(snapshotURL: snapshotURL, destURL: output.snapshotURL, ignorePreviousResults: ignorePreviousResults, extractionDate: extractionDate)
let jobMetadata = JobMetadata(commit: sourceSnapshot.branchInfo?.commit.sha, extractionDate: sourceSnapshot.snapshotDate)
// Always use sourceSnapshot, its values are used in tests
return ExtractionJob(output: output, snapshot: sourceSnapshot, proposalSpecs: sourceSnapshot.proposalSpecs, previousResults: sourceSnapshot.previousResults, forcedExtractionIDs: forcedExtractionIDs, jobMetadata: jobMetadata)
}
private static func makeFilesExtractionJob(fileURLs: [URL], output: Output, ignorePreviousResults: Bool, forcedExtractionIDs: [String], extractionDate: Date) throws -> ExtractionJob {
// Argument validation should ensure correct values. Assert to catch problems in usage in tests.
assert(ignorePreviousResults == true && forcedExtractionIDs.isEmpty, "Extraction from a file URLs always ignores previous results and performs a full extraction")
let proposalSpecs = fileURLs
.sorted(using: SortDescriptor(\URL.lastPathComponent, order: .forward))
.enumerated()
.map { ProposalSpec(url: $1, sha: "", sortIndex: $0) }
let jobMetadata = JobMetadata(commit: "", extractionDate: extractionDate)
let snapshot: Snapshot?
if case let .snapshot(destURL) = output {
snapshot = Snapshot(sourceURL: nil, destURL: destURL, proposalListing: nil, directoryContents: [], proposalSpecs: [], previousResults: nil, expectedResults: nil, branchInfo: nil, snapshotDate: extractionDate)
} else {
snapshot = nil
}
return ExtractionJob(output: output, snapshot: snapshot, proposalSpecs: proposalSpecs, previousResults: nil, forcedExtractionIDs: forcedExtractionIDs, jobMetadata: jobMetadata)
}
private static func makePullRequestExtractionJob(pullRequestID: Int, output: Output, ignorePreviousResults: Bool, forcedExtractionIDs: [String], extractionDate: Date) async throws -> ExtractionJob {
// Argument validation should ensure correct values. Assert to catch problems in usage in tests.
assert(ignorePreviousResults == true && forcedExtractionIDs.isEmpty, "Extraction from a pull request always ignores previous results and performs a full extraction")
let proposalContentItems = try await GitHubFetcher.fetchPullRequestProposalList(for: pullRequestID)
// The proposals/ directory may have subdirectories for proposals from specific workgroups.
// Proposals in those subdirectories are filtered out of this proposal specs array.
let proposalSpecs = proposalContentItems.enumerated().compactMap {
$1.proposalSpec(sortIndex: $0)
}
let jobMetadata = JobMetadata(commit: "", extractionDate: extractionDate)
let snapshot: Snapshot?
if case let .snapshot(destURL) = output {
snapshot = Snapshot(sourceURL: nil, destURL: destURL, proposalListing: nil, directoryContents: [], proposalSpecs: [], previousResults: nil, expectedResults: nil, branchInfo: nil, snapshotDate: extractionDate)
} else {
snapshot = nil
}
return ExtractionJob(output: output, snapshot: snapshot, proposalSpecs: proposalSpecs, previousResults: nil, forcedExtractionIDs: forcedExtractionIDs, jobMetadata: jobMetadata)
}
static func previousResults(from url: URL, ignorePreviousResults: Bool) async throws -> EvolutionMetadata? {
if ignorePreviousResults { return nil }
let data: Data?
if url.isFileURL {
data = try FileUtilities.data(from: url)
} else {
data = try await PreviousResultsFetcher.fetchPreviousResultsData(url: url)
}
guard let data else { return nil }
do {
let decoder = JSONDecoder()
let previousResults = try decoder.decode(EvolutionMetadata.self, from: data)
return previousResults.hasCurrentMetadataVersions ? previousResults : nil
} catch {
print("Unable to decode \(EvolutionMetadata.self) from:")
print(String(decoding: data, as: UTF8.self))
throw error
}
}
}
// MARK: - Comparison
extension ExtractionJob {
private func compareResultsToExpectedValuesIfPresent(_ results: EvolutionMetadata) {
guard let expectedResults = snapshot?.expectedResults else {
return
}
if results.proposals.count != expectedResults.proposals.count {
verbosePrint("Extracted proposal count \(results.proposals.count) does not match expected proposal count of \(expectedResults.proposals.count)")
}
var passingProposals = 0
var failingProposals = 0
for (actualResult, expectedResult) in zip(results.proposals, expectedResults.proposals) {
if actualResult == expectedResult {
passingProposals += 1
} else {
failingProposals += 1
}
}
verbosePrint("Comparing to snapshot expected results", terminator: "\n")
verbosePrint("Passing Proposals:", passingProposals, terminator: "\n")
verbosePrint("Failing Proposals:", failingProposals)
}
}
// MARK: - Output
extension ExtractionJob {
private func outputResults(_ results: EvolutionMetadata) throws {
switch output {
case .metadataJSON(let outputURL):
try ExtractionJob.writeEvolutionResultsAsJSON(results: results, outputURL: outputURL)
case .snapshot(let outputURL):
try writeSnapshot(results: results, outputURL: outputURL)
case .validationReport(let outputURL):
try ExtractionJob.writeValidationReport(results: results, outputURL: outputURL)
case .none:
return
}
}
private static func writeEvolutionResultsAsJSON(results: EvolutionMetadata, outputURL: URL) throws {
print("Writing file '\(outputURL.lastPathComponent)' to\n'\(outputURL.absoluteURL.path())'\n")
let jsonData = try results.jsonRepresentation
if outputURL.isStandardOutURL {
FileHandle.standardOutput.write(jsonData)
} else {
let directoryURL = outputURL.deletingLastPathComponent()
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
try jsonData.write(to: outputURL)
}
}
private func writeSnapshot(results: EvolutionMetadata, outputURL: URL) throws {
guard let snapshot else { fatalError("Cannot write snapshot. Snapshot is missing.") }
guard outputURL != URL.standardOutURL else { fatalError("Cannot write snapshot to stdout.") }
try snapshot.writeSnapshot(results: results, outputURL: outputURL)
}
private static func writeValidationReport(results: EvolutionMetadata, outputURL: URL) throws {
let report = results.validationReport
if outputURL.isStandardOutURL {
print(report)
} else {
let data = Data(report.utf8)
let directoryURL = outputURL.deletingLastPathComponent()
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
try data.write(to: outputURL)
}
if results.hasErrors {
try exitWithFailure()
}
}
}