|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import struct TSCBasic.ProcessResult |
| 14 | + |
| 15 | +/// Result of a process that prepares a target or updates the index store. To be shown in the build log. |
| 16 | +/// |
| 17 | +/// Abstracted over a `ProcessResult` to facilitate build systems that don't spawn a new process to prepare a target but |
| 18 | +/// prepare it from a build graph they have loaded in-process. |
| 19 | +public struct IndexProcessResult { |
| 20 | + /// A human-readable description of what the process was trying to achieve, like `Preparing MyTarget` |
| 21 | + public let taskDescription: String |
| 22 | + |
| 23 | + /// The command that was run to produce the result. |
| 24 | + public let command: String |
| 25 | + |
| 26 | + /// The output that the process produced. |
| 27 | + public let output: String |
| 28 | + |
| 29 | + /// Whether the process failed. |
| 30 | + public let failed: Bool |
| 31 | + |
| 32 | + /// The duration it took for the process to execute. |
| 33 | + public let duration: Duration |
| 34 | + |
| 35 | + public init(taskDescription: String, command: String, output: String, failed: Bool, duration: Duration) { |
| 36 | + self.taskDescription = taskDescription |
| 37 | + self.command = command |
| 38 | + self.output = output |
| 39 | + self.failed = failed |
| 40 | + self.duration = duration |
| 41 | + } |
| 42 | + |
| 43 | + public init(taskDescription: String, processResult: ProcessResult, start: ContinuousClock.Instant) { |
| 44 | + let stdout = (try? String(bytes: processResult.output.get(), encoding: .utf8)) ?? "<failed to decode stdout>" |
| 45 | + let stderr = (try? String(bytes: processResult.stderrOutput.get(), encoding: .utf8)) ?? "<failed to decode stderr>" |
| 46 | + var outputComponents: [String] = [] |
| 47 | + if !stdout.isEmpty { |
| 48 | + outputComponents.append( |
| 49 | + """ |
| 50 | + Stdout: |
| 51 | + \(stdout) |
| 52 | + """ |
| 53 | + ) |
| 54 | + } |
| 55 | + if !stderr.isEmpty { |
| 56 | + outputComponents.append( |
| 57 | + """ |
| 58 | + Stderr: |
| 59 | + \(stderr) |
| 60 | + """ |
| 61 | + ) |
| 62 | + } |
| 63 | + self.init( |
| 64 | + taskDescription: taskDescription, |
| 65 | + command: processResult.arguments.joined(separator: " "), |
| 66 | + output: outputComponents.joined(separator: "\n\n"), |
| 67 | + failed: processResult.exitStatus != .terminated(code: 0), |
| 68 | + duration: start.duration(to: .now) |
| 69 | + ) |
| 70 | + } |
| 71 | +} |
0 commit comments