Skip to content

Commit b30ef02

Browse files
Write snapshot to temp directory and then replace into place
Part of #18
1 parent f1312ec commit b30ef02

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Sources/EvolutionMetadataExtraction/ExtractionJob.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public struct ExtractionJob: Sendable {
4040
let toolVersion: String
4141
let extractionDate: Date
4242
let output: Output
43-
let temporaryProposalsDirectory: URL?
44-
43+
let temporarySnapshotDirectory: URL?
44+
var temporaryProposalsDirectory: URL? { temporarySnapshotDirectory?.appending(component: "proposals") }
45+
4546
private init(source: Source, output: Output, branchInfo: GitHubBranch? = nil, proposalListing: [GitHubContentItem]?, proposalSpecs: [ProposalSpec], previousResults: [Proposal], expectedResults: EvolutionMetadata?, forcedExtractionIDs: [String], toolVersion: String, extractionDate: Date = Date()) {
4647
self.source = source
4748
self.branchInfo = branchInfo
@@ -53,9 +54,9 @@ public struct ExtractionJob: Sendable {
5354
self.toolVersion = toolVersion
5455
self.extractionDate = extractionDate
5556
self.output = output
56-
self.temporaryProposalsDirectory = switch output {
57+
self.temporarySnapshotDirectory = switch output {
5758
case .snapshot:
58-
FileManager.default.temporaryDirectory.appending(component: "proposals")
59+
FileManager.default.temporaryDirectory.appending(component: UUID().uuidString)
5960
default:
6061
nil
6162
}
@@ -239,35 +240,39 @@ public struct ExtractionJob: Sendable {
239240

240241
private func writeSnapshot(results: EvolutionMetadata, outputURL: URL) throws {
241242

242-
guard let temporaryProposalsDirectory else {
243-
fatalError("When snapshot command is being run temporaryProposalsDirectory should never be nil")
243+
guard let temporarySnapshotDirectory, let temporaryProposalsDirectory else {
244+
fatalError("When snapshot command is being run temporarySnapshotDirectory should never be nil")
245+
}
246+
247+
guard FileManager.default.fileExists(atPath: temporaryProposalsDirectory.path(percentEncoded: false)) else {
248+
fatalError("Temporary proposals directory should already be created during metadata extraction")
244249
}
245250

246251
print("Writing snapshot '\(outputURL.lastPathComponent)' to '\(outputURL.absoluteURL.path())'\n")
247252
let encoder = JSONEncoder()
248253
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
249254

250-
try FileManager.default.createDirectory(at: outputURL, withIntermediateDirectories: true)
251-
252-
let proposalsDirectory = outputURL.appending(component: "proposals")
253-
_ = try FileManager.default.replaceItemAt(proposalsDirectory, withItemAt: temporaryProposalsDirectory, options: [.usingNewMetadataOnly])
254-
255+
try FileManager.default.createDirectory(at: temporarySnapshotDirectory, withIntermediateDirectories: true)
256+
255257
if let branchInfo {
256258
let branchInfoData = try encoder.encode(branchInfo)
257-
let branchInfoURL = outputURL.appending(component: "source-info.json")
259+
let branchInfoURL = temporarySnapshotDirectory.appending(component: "source-info.json")
258260
try branchInfoData.write(to: branchInfoURL)
259261
}
260262

261263
if let proposalListing, !proposalListing.isEmpty {
262264
let proposalListingData = try encoder.encode(proposalListing)
263-
let proposalListingURL = outputURL.appending(component: "proposal-listing.json")
265+
let proposalListingURL = temporarySnapshotDirectory.appending(component: "proposal-listing.json")
264266
try proposalListingData.write(to: proposalListingURL)
265267
}
266268

267269
let extractionResultsData = try encoder.encode(results)
268270
let adjustedResultstData = JSONRewriter.applyRewritersToJSONData(rewriters: [JSONRewriter.prettyPrintVersions], data: extractionResultsData)
269-
let expectedResultsURL = outputURL.appending(component: "expected-results.json")
271+
let expectedResultsURL = temporarySnapshotDirectory.appending(component: "expected-results.json")
270272
try adjustedResultstData.write(to: expectedResultsURL)
271273

274+
try FileManager.default.createDirectory(at: outputURL, withIntermediateDirectories: true)
275+
_ = try FileManager.default.replaceItemAt(outputURL, withItemAt: temporarySnapshotDirectory, options: [.usingNewMetadataOnly])
276+
272277
}
273278
}

0 commit comments

Comments
 (0)