Scripting in Swift felt like a hassle until I automated the process of creating/building/running scattered scripts with common dependencies. Now it's a small pleasure, as I start from templates, build delays are hardly noticeable, and git repos are consolidated.
A "nest" (e.g.,
HOME/git/Nest) is a package with a common library and "peer" executable's for each associated script.
#!/usr/bin/env clutchon the first line of
clutchcreates, updates, and/or builds the nest peer.
Here's the quickstart from the README...
# Build clutch and put on PATH (for `/usr/bin/env`) git clone https://github.com/swift-clutch/clutch.git cd clutch && swift build cp .build/debug/clutch ~/bin # Grab a sample nest package and try hello-world cp -rf nests/simple/Nest ~/git/Nest cat > hello <<EOF #!/usr/bin/env clutch let you = CommandLine.arguments[1...].first ?? "World" print("Hello \(you)") EOF chmod +x hello ./hello friend # builds, runs `~/git/Nest/Sources/hello/main.swift`
clutch also helps manage peers and nests; it can list peers in a nest, emit peer source as a starting-point for a new script, etc. The nest location and script mapping are configurable by name and environment variable. You can have nests for builds, voice, video, etc. It works back at least to 5.6 on Linux and macOS 12.
clutch hello # run by name from anywhere clutch hello.Nest # specify nest if not the default clutch cat-hello # output peer source (`clutch cat-start > newscript.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`
For details, see the README.
The closest alternative is https://github.com/mxcl/swift-sh, which parses import comments and manages a private package for each script.
For those who find the
clutch code too cluttered by configuration, error, testing, and system-isolation hoo-ha,
clatch is a 1-file executable to just create/build/run nest peers in
I hope this tiny tool saves you time and makes scripting in Swift easier. Feedback welcome!