当以下情况时,使用 clutch
构建和启动脚本更容易进行 Swift 脚本编写:
对于每个脚本,clutch
在本地“巢”包中创建一个匹配的“对等”可执行文件
HOME/git/Build
中)。#!/usr/bin/env clutch
(如果 clutch 在 PATH 中)。name.{Nest}.swift
clutch
根据需要创建和/或构建对等可执行文件Build/Package.swift
将更新为可执行产品 name
。Build/Sources/name/main.swift
(或 name.swift
) 将被创建或更新以匹配脚本文件。Build
)clutch
还可以按名称(从任何地方)运行对等文件,并在巢中管理对等文件。
在 macOS 12+ 和 Linux (Ubuntu LTS/最新版) 上测试通过
假设 ~/git
,PATH 包含 ~/bin
以及 swift
和 git
(并且您可以接受 Sources/clutch 和 nests/simple/Nest)...
# Build clutch and install to `~/bin` on `PATH` (for `/usr/bin/env`)
git clone https://github.com/swift-clutch/clutch.git
cd clutch && swift build
cp .build/debug/clutch ~/bin
# Create a sample nest package
cp -rf nests/simple/Nest ~/git/Nest
# or: mkdir ~/git/Nest && cd "$_" && swift package init --type library
# Create a hello script (anywhere)
cat > hello <<EOF
#!/usr/bin/env clutch
let name = CommandLine.arguments[1...].first ?? "World"
print("Hello \(name)")
EOF
chmod +x hello
./hello friend # builds, runs `~/git/Nest/Sources/hello/main.swift`
# Use clutch directly to run or manage peers or nests
clutch hello # run by name from anywhere (use `hello.{Nest}` if not default)
clutch cat-hello # output peer source (`clutch cat-hello > hi.swift`)
clutch path-hello # echo peer path (`vi "$(clutch path-hello)"`)
clutch peers-Nest # list peers known in nest `Nest`
clutch dir-Nest # emit location of nest `Nest`
安装 clutch,创建一个巢包,并在任何地方编写一个 Swift 脚本文件。
当您调用脚本时,clutch 在确保其巢包对等文件 (~/git/Nest/Sources/peer/peer.swift
) 已创建、更新和/或构建后运行它。巢包有一个巢库(具有巢名称)用于通用代码和依赖项。
.
之前)。Nest
。#!/usr/bin/env clutch
(最佳,如果 clutch 在您的 PATH 中)#!/path/to/clutch
(当 clutch 已安装但不在 PATH 中时)巢对等文件在 {nest}/Sources/{peer}
中将在首次印象时创建。对等文件名是 main.swift
,或者如果它包含 @main
,则为 {peer}.swift
。
Package.swift
将更新为对等产品和目标声明
.executable(name: "{peer}", targets: [ "{peer}" ]),
.executableTarget(name: "{peer}", dependencies: ["{nest}"]),
该包至少包含巢库和每个关联脚本的可执行目标。
// in Package.swift (e.g., as created by `swift package --init`)
products: [
// peer product created for each script, using the script name {peer}
.executable(name: "{peer}", targets: [ "{peer}" ]),
.library(name: "{nest}", targets: ["{nest}"]),
],
targets: [
// peer executable created for each script
.executableTarget(name: "{peer}", dependencies: ["{nest}"]),
.target(
name: "{nest}",
dependencies: [ ... ]
巢目录名称必须是库模块的名称。
有关示例巢包,请参阅 nests 或使用 swift package init --type library
。
默认情况下,clutch 使用 -c debug --quiet
构建(以避免延迟和噪音),巢包名为 Nest
,并且它位于 $HOME/git/Nest
。
您可以配置巢位置、输出或构建标志。
配置很少需要(主要只是 CLUTCH_NEST_RELPATH
直接更改 git 父目录)。
要配置,请设置环境变量
CLUTCH_NEST_NAME
: 在 $HOME/{relative-path}/{nest-name}
中查找巢CLUTCH_NEST_RELPATH
: 从 HOME 的相对路径(默认为 git
)CLUTCH_NEST_BASE
: 在 $CLUTCH_NEST_BASE/{nest-name}
中查找巢,而不是在默认位置CLUTCH_NEST_PATH
: 巢目录的完整路径(忽略其他变量)CLUTCH_LOG
: 任何值都将 clutch 构建步骤记录到标准错误CLUTCH_BUILD
: @{arg0}@{arg1}..
, 或 {release} {loud | verbose}
release
、loud
和 verbose
生成适当的构建标志。@
分隔的参数将传递给 swift build 命令。clutch name{.Nest} # Run peer by name
clutch [peers|path]-Nest # Emit Nest peers or location
clutch [cat|path]-name{.Nest} # Emit peer code or location
直接使用 clutch 按文件名或对等名称运行脚本
clutch name.swift # Create/build/run `name` from default nest
clutch name # Run by name
使用 clutch 列出巢中的对等文件,并查找或复制对等源文件
clutch peers-Data # List peers in the `Data` nest
clutch path-name # Echo path to source file for peer `name`
clutch cat-init.Data # Output code from peer `init` in Data nest
@main
和 Package.swift
操作可能会失败。swift build
会保留旧的可执行文件,导致无害但没有必要的空操作构建。@main
检测非常简单(并且不针对更新执行)。Package.swift
编辑也是一个简单的扫描。products:
和 targets:
(后者带有 2 个前导空格)target:
很常见;请避免在其前使用 2 个空格。CLUTCH_PRODUCT
或 CLUTCH_TARGET
Sources/peer
和 Package.swift
中对等文件的两行swift
命令可以正常工作。#!/usr/bin/env swift
直接运行swift script.swift
执行相同的操作,没有 #!
hash-bang 行swift -e 'statement{; statement}'
运行代码片段generateCode | swift -
从输入流运行代码mint install swift-nest/clutch