用 Swift 编写的简单 shell 执行器。
要在 SwiftPM 项目中使用 SwiftShell
库,请将以下行添加到 Package.swift
文件中的依赖项
.package(url: "https://github.com/taji-taji/swift-shell.git", from: "1.0.0")
在您的可执行目标中包含 "SwiftShell"
作为依赖项
.target(name: "<target>", dependencies: [
.product(name: "SwiftShell", package: "swift-shell"),
]),
最后,将 import SwiftShell
添加到您的源代码中。
import SwiftShell
let shell = Shell()
do {
// Shell is implemented with `callAsFunction`.
let output = try shell("ls", arguments: ["-l", "-a"])
print(output)
} catch {
print(error)
}
import SwiftShell
let shell = Shell()
Task {
async let output1 = shell("brew install awesome-tool")
async let output2 = shell("bundle install")
let result = try await (output1, output2)
print(result.0)
print(result.1)
}
使用一些环境变量执行。
try shell("brew install git",
additionalEnvironment: ["HOMEBREW_NO_AUTO_UPDATE": "1"])