-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package build step to SPM docs generation (#614)
Final part of jazzy+SPM support: this runs `swift build` as part of generating docs for SPM modules. This makes sure that the build manifest (a) exists and (b) is consistent with the latest changes to the source code, same as Xcode path. (No need to do a `clean` so is v. cheap if the package has already been built.) First commit is a pure refactor of all the popen-ish code because I couldn't stand duplicating it again. Accounts for most of the changed LOC. Second commit adds a `Module` initializer that runs `swift build` before going to SourceKit. I tentatively kept the existing SPM initializer to keep semantics for existing users (SourceDocs uses it, didn't look further).
- Loading branch information
Showing
10 changed files
with
178 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// Exec.swift | ||
// SourceKittenFramework | ||
// | ||
// Copyright © 2019 SourceKitten. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Namespace for utilities to execute a child process. | ||
enum Exec { | ||
/// How to handle stderr output from the child process. | ||
enum Stderr { | ||
/// Treat stderr same as parent process. | ||
case inherit | ||
/// Send stderr to /dev/null. | ||
case discard | ||
/// Merge stderr with stdout. | ||
case merge | ||
} | ||
|
||
/// The result of running the child process. | ||
struct Results { | ||
/// The process's exit status. | ||
let terminationStatus: Int32 | ||
/// The data from stdout and optionally stderr. | ||
let data: Data | ||
/// The `data` reinterpreted as a string with whitespace trimmed; `nil` for the empty string. | ||
var string: String? { | ||
let encoded = String(data: data, encoding: .utf8) ?? "" | ||
let trimmed = encoded.trimmingCharacters(in: .whitespacesAndNewlines) | ||
return trimmed.isEmpty ? nil : trimmed | ||
} | ||
} | ||
|
||
/** | ||
Run a command with arguments and return its output and exit status. | ||
- parameter command: Absolute path of the command to run. | ||
- parameter arguments: Arguments to pass to the command. | ||
- parameter currentDirectory: Current directory for the command. By default | ||
the parent process's current directory. | ||
- parameter stderr: What to do with stderr output from the command. By default | ||
whatever the parent process does. | ||
*/ | ||
static func run(_ command: String, | ||
_ arguments: String..., | ||
currentDirectory: String = FileManager.default.currentDirectoryPath, | ||
stderr: Stderr = .inherit) -> Results { | ||
return run(command, arguments, currentDirectory: currentDirectory, stderr: stderr) | ||
} | ||
|
||
/** | ||
Run a command with arguments and return its output and exit status. | ||
- parameter command: Absolute path of the command to run. | ||
- parameter arguments: Arguments to pass to the command. | ||
- parameter currentDirectory: Current directory for the command. By default | ||
the parent process's current directory. | ||
- parameter stderr: What to do with stderr output from the command. By default | ||
whatever the parent process does. | ||
*/ | ||
static func run(_ command: String, | ||
_ arguments: [String] = [], | ||
currentDirectory: String = FileManager.default.currentDirectoryPath, | ||
stderr: Stderr = .inherit) -> Results { | ||
let process = Process() | ||
process.arguments = arguments | ||
|
||
let pipe = Pipe() | ||
process.standardOutput = pipe | ||
|
||
switch stderr { | ||
case .discard: | ||
// FileHandle.nullDevice does not work here, as it consists of an invalid file descriptor, | ||
// causing process.launch() to abort with an EBADF. | ||
process.standardError = FileHandle(forWritingAtPath: "/dev/null")! | ||
case .merge: | ||
process.standardError = pipe | ||
case .inherit: | ||
break | ||
} | ||
|
||
do { | ||
#if canImport(Darwin) | ||
if #available(macOS 10.13, *) { | ||
process.executableURL = URL(fileURLWithPath: command) | ||
process.currentDirectoryURL = URL(fileURLWithPath: currentDirectory) | ||
try process.run() | ||
} else { | ||
process.launchPath = command | ||
process.currentDirectoryPath = currentDirectory | ||
process.launch() | ||
} | ||
#elseif compiler(>=5) | ||
process.executableURL = URL(fileURLWithPath: command) | ||
process.currentDirectoryURL = URL(fileURLWithPath: currentDirectory) | ||
try process.run() | ||
#else | ||
process.launchPath = command | ||
process.currentDirectoryPath = currentDirectory | ||
process.launch() | ||
#endif | ||
} catch { | ||
return Results(terminationStatus: -1, data: Data()) | ||
} | ||
|
||
let file = pipe.fileHandleForReading | ||
let data = file.readDataToEndOfFile() | ||
process.waitUntilExit() | ||
return Results(terminationStatus: process.terminationStatus, data: data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.