$ show mxcl/swift-sh
swift-sh is a CLI tool that enables Swift scripts to declare and fetch third-party dependencies via special comments in import statements. It parses a script's imports (e.g., `import Foo // @mxcl ~> 1.0`), generates a Swift Package Manager project with those dependencies, builds it, and runs the resulting executable. The architecture is modular with separate targets for script parsing (Script), command handling (Command), and utility functions (Utility). The tool supports both file-based and stdin scripts, local dependencies, and provides subcommands for ejecting to a full package and editing in Xcode (macOS only). It is a pragmatic solution that bridges the gap between ad-hoc scripting and SwiftPM's package structure.
The LLM identified 3 files worth highlighting for reuse:
A robust Swift extension on Process that provides synchronous execution with output capture, error handling, and thread-safe reading. It handles both stdout and stderr, supports tee-ing, and uses DispatchQueue for safe data accumulation. This is a self-contained, reusable component that can be extracted into any Swift project for reliable subprocess management.
The core data model and parser for import dependency specifications. It defines ImportSpecification with import name, dependency name (GitHub, URL, SCP, local), and version constraint (upToNextMajor, exact, ref, latest). Includes Codable conformance and a packageLine property for generating SwiftPM manifest entries. The parsing logic is well-tested and demonstrates a clean approach to extracting structured data from comments.
Provides a safe execv wrapper that replaces the current process with a new executable. Includes a CStringArray helper for converting Swift strings to C string arrays and a POSIXError for descriptive error reporting. This is a minimal, reusable utility for process replacement in Swift scripts.