Skip to content

Use Subprocess for process management #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.7.2"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.8.2"),
.package(url: "https://github.com/apple/swift-system", from: "1.4.2"),
.package(url: "https://github.com/swiftlang/swift-subprocess", revision: "afc1f734feb29c3a1ebbd97cc1fe943f8e5d80e5"),
// This dependency provides the correct version of the formatter so that you can run `swift run swiftformat Package.swift Plugins/ Sources/ Tests/`
.package(url: "https://github.com/nicklockwood/SwiftFormat", exact: "0.49.18"),
],
Expand Down Expand Up @@ -67,6 +68,7 @@ let package = Package(
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client"),
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "Subprocess", package: "swift-subprocess"),
],
swiftSettings: swiftSettings,
plugins: ["GenerateCommandModels"]
Expand Down
6 changes: 3 additions & 3 deletions Sources/LinuxPlatform/Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public struct Linux: Platform {
}

if requireSignatureValidation {
guard (try? self.runProgram("gpg", "--version", quiet: true)) != nil else {
guard (try? await self.runProgram("gpg", "--version", quiet: true)) != nil else {
var msg = "gpg is not installed. "
if let manager {
msg += """
Expand Down Expand Up @@ -321,7 +321,7 @@ public struct Linux: Platform {
}
return false
case "yum":
try self.runProgram("yum", "list", "installed", package, quiet: true)
try await self.runProgram("yum", "list", "installed", package, quiet: true)
return true
default:
return true
Expand Down Expand Up @@ -382,7 +382,7 @@ public struct Linux: Platform {
tmpDir / String(name)
}

try self.runProgram((tmpDir / "swiftly").string, "init")
try await self.runProgram((tmpDir / "swiftly").string, "init")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MacOSPlatform/MacOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public struct MacOS: Platform {
try await sys.tar(.directory(installDir)).extract(.verbose, .archive(payload)).run(self, quiet: false)
}

try self.runProgram((userHomeDir / ".swiftly/bin/swiftly").string, "init")
try await self.runProgram((userHomeDir / ".swiftly/bin/swiftly").string, "init")
}

public func uninstall(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, verbose: Bool)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftlyCore/ModeledCommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ extension Runnable {
newEnv = newValue
}

try p.runProgram([executable] + args, quiet: quiet, env: newEnv)
try await p.runProgram([executable] + args, quiet: quiet, env: newEnv)
}
}

Expand Down
171 changes: 171 additions & 0 deletions Sources/SwiftlyCore/Platform+Process.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import Foundation
import Subprocess
#if os(macOS)
import System
#endif

import SystemPackage

extension Platform {
#if os(macOS) || os(Linux)
func proxyEnv(_ ctx: SwiftlyCoreContext, env: [String: String], toolchain: ToolchainVersion) async throws -> [String: String] {
var newEnv = env

let tcPath = try await self.findToolchainLocation(ctx, toolchain) / "usr/bin"
guard try await fs.exists(atPath: tcPath) else {
throw SwiftlyError(
message:
"Toolchain \(toolchain) could not be located in \(tcPath). You can try `swiftly uninstall \(toolchain)` to uninstall it and then `swiftly install \(toolchain)` to install it again."
)
}

var pathComponents = (newEnv["PATH"] ?? "").split(separator: ":").map { String($0) }

// The toolchain goes to the beginning of the PATH
pathComponents.removeAll(where: { $0 == tcPath.string })
pathComponents = [tcPath.string] + pathComponents

// Remove swiftly bin directory from the PATH entirely
let swiftlyBinDir = self.swiftlyBinDir(ctx)
pathComponents.removeAll(where: { $0 == swiftlyBinDir.string })

newEnv["PATH"] = String(pathComponents.joined(separator: ":"))

return newEnv
}

/// Proxy the invocation of the provided command to the chosen toolchain.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func proxy(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, _ command: String, _ arguments: [String], _ env: [String: String] = [:]) async throws {
let tcPath = (try await self.findToolchainLocation(ctx, toolchain)) / "usr/bin"

let commandTcPath = tcPath / command
let commandToRun = if try await fs.exists(atPath: commandTcPath) {
commandTcPath.string
} else {
command
}

var newEnv = try await self.proxyEnv(ctx, env: ProcessInfo.processInfo.environment, toolchain: toolchain)
for (key, value) in env {
newEnv[key] = value
}

#if os(macOS)
// On macOS, we try to set SDKROOT if its empty for tools like clang++ that need it to
// find standard libraries that aren't in the toolchain, like libc++. Here we
// use xcrun to tell us what the default sdk root should be.
if newEnv["SDKROOT"] == nil {
newEnv["SDKROOT"] = (try? await self.runProgramOutput("/usr/bin/xcrun", "--show-sdk-path"))?.replacingOccurrences(of: "\n", with: "")
}
#endif

try await self.runProgram([commandToRun] + arguments, env: newEnv)
}

/// Proxy the invocation of the provided command to the chosen toolchain and capture the output.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func proxyOutput(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, _ command: String, _ arguments: [String]) async throws -> String? {
let tcPath = (try await self.findToolchainLocation(ctx, toolchain)) / "usr/bin"

let commandTcPath = tcPath / command
let commandToRun = if try await fs.exists(atPath: commandTcPath) {
commandTcPath.string
} else {
command
}

return try await self.runProgramOutput(commandToRun, arguments, env: self.proxyEnv(ctx, env: ProcessInfo.processInfo.environment, toolchain: toolchain))
}

/// Run a program.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func runProgram(_ args: String..., quiet: Bool = false, env: [String: String]? = nil)
async throws
{
try await self.runProgram([String](args), quiet: quiet, env: env)
}

/// Run a program.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func runProgram(_ args: [String], quiet: Bool = false, env: [String: String]? = nil)
async throws
{
if !quiet {
let result = try await run(
.path("/usr/bin/env"),
arguments: .init(args),
environment: env != nil ? .inherit.updating(env ?? [:]) : .inherit,
input: .fileDescriptor(.standardInput, closeAfterSpawningProcess: false),
output: .fileDescriptor(.standardOutput, closeAfterSpawningProcess: false),
error: .fileDescriptor(.standardError, closeAfterSpawningProcess: false),
)

if case let .exited(code) = result.terminationStatus, code != 0 {
throw RunProgramError(exitCode: code, program: args.first!, arguments: Array(args.dropFirst()))
}
} else {
let result = try await run(
.path("/usr/bin/env"),
arguments: .init(args),
environment: env != nil ? .inherit.updating(env ?? [:]) : .inherit,
output: .discarded,
error: .discarded,
)

if case let .exited(code) = result.terminationStatus, code != 0 {
throw RunProgramError(exitCode: code, program: args.first!, arguments: Array(args.dropFirst()))
}
}

// TODO: handle exits with a signal
}

/// Run a program and capture its output.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func runProgramOutput(_ program: String, _ args: String..., env: [String: String]? = nil)
async throws -> String?
{
try await self.runProgramOutput(program, [String](args), env: env)
}

/// Run a program and capture its output.
///
/// In the case where the command exit with a non-zero exit code a RunProgramError is thrown with
/// the exit code and program information.
///
public func runProgramOutput(_ program: String, _ args: [String], env: [String: String]? = nil)
async throws -> String?
{
let result = try await run(
.path("/usr/bin/env"),
arguments: .init([program] + args),
environment: env != nil ? .inherit.updating(env ?? [:]) : .inherit,
output: .string(limit: 10 * 1024 * 1024, encoding: UTF8.self),
error: .fileDescriptor(.standardError, closeAfterSpawningProcess: false)
)

if case let .exited(code) = result.terminationStatus, code != 0 {
throw RunProgramError(exitCode: code, program: args.first!, arguments: Array(args.dropFirst()))
}

return result.standardOutput
}

#endif
}
Loading