一个允许你从你的 swift 代码中运行 shell 脚本的包。
你可以通过使用 swift package manager 将其包含在你的项目中。
import PackageDescription
let package = Package(
...
dependencies: [
.package(url: "https://github.com/m-housh/swift-shell-client.git", from: "0.1.0"),
...
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "ShellClient", package: "swift-shell-client"),
]
),
...
]
)
你可以通过 swift-dependencies 系统访问 shell 客户端。
import ShellClient
func echo() throws {
@Dependency(\.logger) var logger
@Dependency(\.shellClient) var shell
try shell.foreground(["echo", "Foo"])
// Or run in a background process, and capture the output.
let output = try shell.background(
["echo", "world"]
trimmingCharactersIn: .whitespacesAndNewlines
)
logger.info("Hello \(output)!")
}
func echoAsync() async throws {
@Dependency(\.logger) var logger
@Dependency(\.asyncShellClient) var shell
try await shell.foreground(["echo", "Foo"])
// Or run in a background process, and capture the output.
let output = try await shell.background(
["echo", "world"],
trimmingCharactersIn: .whitespacesAndNewlines
)
logger.info("Hello \(output)!")
}
try echo()
try await echoAsync()
我们使用 swift-log 以及 swift-log-format-and-pipe 来创建一个你可以访问的基本日志记录器。你也可以使用 Rainbow 来为终端输出彩色文本。
内置的日志记录器在发布版本中构建时将只记录消息而不带任何标签,在调试或测试环境中构建时将使用 shell-client
标签进行记录。
你可以通过使用库提供的以下方法创建一个带有标签的基本日志记录器。
import Rainbow
let logger = basicLogger(.showing(label: "log".red))
logger.info("blob")
// log ▸ blob
你可以在这里阅读完整的文档。