clutch 🪺 从依赖巢运行 Swift 脚本

当以下情况时,使用 clutch 构建和启动脚本更容易进行 Swift 脚本编写:

对于每个脚本,clutch 在本地“巢”包中创建一个匹配的“对等”可执行文件

clutch 还可以按名称(从任何地方)运行对等文件,并在巢中管理对等文件。

快速入门

在 macOS 12+ 和 Linux (Ubuntu LTS/最新版) 上测试通过

假设 ~/git,PATH 包含 ~/bin 以及 swiftgit (并且您可以接受 Sources/clutchnests/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) 已创建、更新和/或构建后运行它。巢包有一个巢库(具有巢名称)用于通用代码和依赖项。

编写 name.{Nest.}swift: 当 clutch 在 PATH 中时使用 #!/usr/bin/env clutch

巢对等文件在 {nest}/Sources/{peer} 中将在首次印象时创建。对等文件名是 main.swift,或者如果它包含 @main,则为 {peer}.swift

Package.swift 将更新为对等产品和目标声明

巢包,例如,$HOME/git/{nest}/Package.swift

该包至少包含巢库和每个关联脚本的可执行目标。

// 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 运行和管理对等文件

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

限制(开发中)

  • 当声明不遵循预期格式时,@mainPackage.swift 操作可能会失败。
  • 当脚本编辑没有产生二进制更改时,swift build 会保留旧的可执行文件,导致无害但没有必要的空操作构建。
  • 脚本退出代码被转换为 1。
  • 有关已知错误和缺少的功能,请参阅 README-clutch

包状态

替代方案和相关库

开发