Skip to content

Commit f07264a

Browse files
committed
SwiftPM 6.4 support - refactor error results
1 parent 6d56156 commit f07264a

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

Source/SourceKittenFramework/Exec.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ enum Exec {
2424
let trimmed = encoded.trimmingCharacters(in: .whitespacesAndNewlines)
2525
return trimmed.isEmpty ? nil : trimmed
2626
}
27+
28+
/// Save `data` to a new temporary file and return its path
29+
/// - parameter prefix: Prefix for the name of the temporary file
30+
func save(prefix: String) -> String {
31+
let file = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(prefix)-\(UUID().uuidString).log")
32+
_ = try? data.write(to: file)
33+
return file.path
34+
}
2735
}
2836

2937
/**

Source/SourceKittenFramework/Module.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ public struct Module {
8484
if results.terminationStatus != 0 {
8585
fputs("Could not successfully run `xcodebuild`.\n", stderr)
8686
fputs("Please check the build arguments.\n", stderr)
87-
let file = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("xcodebuild-\(NSUUID().uuidString).log")
88-
_ = try? results.data.write(to: file)
89-
fputs("Saved `xcodebuild` log file: \(file.path)\n", stderr)
87+
let path = results.save(prefix: "xcodebuild")
88+
fputs("Saved `xcodebuild` log file: \(path)\n", stderr)
9089
return nil
9190
}
9291
if let output = results.string,
@@ -108,9 +107,8 @@ public struct Module {
108107
guard let arguments = parseCompilerArguments(xcodebuildOutput: xcodeBuildOutput, language: .swift, moduleName: name) else {
109108
fputs("Could not parse compiler arguments from `xcodebuild` output.\n", stderr)
110109
fputs("Please confirm that `xcodebuild` is building a Swift module.\n", stderr)
111-
let file = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("xcodebuild-\(NSUUID().uuidString).log")
112-
_ = try? xcodeBuildOutput.data(using: .utf8)?.write(to: file)
113-
fputs("Saved `xcodebuild` log file: \(file.path)\n", stderr)
110+
let path = results.save(prefix: "xcodebuild")
111+
fputs("Saved `xcodebuild` log file: \(path)\n", stderr)
114112
return nil
115113
}
116114
guard let moduleName = moduleName(fromArguments: arguments) else {

Source/SourceKittenFramework/SwiftPM.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ enum SwiftPM {
6464
private static func runCheckedCommand(arguments: [String], inPath path: String, msgName: String) -> Exec.Results? {
6565
let results = Exec.run("/usr/bin/env", arguments, currentDirectory: path, stderr: .merge)
6666
guard results.terminationStatus == 0 else {
67-
let file = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("swift-build-\(UUID().uuidString).log")
68-
_ = try? results.data.write(to: file)
69-
fputs("Build failed, saved `\(msgName)` log file: \(file.path)\n", stderr)
67+
let path = results.save(prefix: "swift-build")
68+
fputs("Build failed, saved `\(msgName)` log file: \(path)\n", stderr)
7069
return nil
7170
}
7271
return results

0 commit comments

Comments
 (0)