SPX

SPX 是一个 Swift 脚本运行工具。它能方便地在名为 SPX 的相对目录下运行 Swift 包。

SPX 与 Sh 配合使用效果更佳,可以从 Swift 脚本中运行 shell 命令并处理 shell 输出。

要查看使用 SPX、ShShXcrun 的完整示例,请访问 https://github.com/DanielSincere/SwishExampleiOSProject

动机

目前,在 Package.swift 中声明某些目标为开发依赖项或支持脚本,并没有很好的解决方案。那么,如果我们有一个包含 Swift 支持脚本的子目录呢? 比如 $PROJECT_ROOT/support-dir/Package.swift

但是,目前还没有很好的解决方案来在另一个目录中运行 Swift 包。 我们可以输入 swift run --package-path path/to/dir targetName,但这对于快速脚本来说太复杂了。 我们可以将其存储在 .sh 脚本文件中,但我更喜欢 Swift 工具不涉及 shell 脚本。

而且,如果我们在 Xcode 构建 iOS 项目期间运行此脚本,我们需要传递一个 SDK 标志,以便为 MacOS 构建 Swift 脚本。

这一切都是可以做到的。但它在多个项目中变得重复。

安装

SPX 目前可以通过 HomebrewMint 或手动安装获得。

Homebrew

使用 Homebrew 安装

brew install danielsincere/tap/spx

或者,如果使用 Brewfile,请将以下行添加到其中

tap "danielsincere/tap"
brew "spx"

Mint

使用 Mint 安装

mint install DanielSincere/SPX

手动

git clone https://github.com/DanielSincere/SPX.git
cd SPX
swift build -c release

然后将 .build/release/ 添加到您的 $PATH 中。

开始使用

spx --init simple

将在当前工作目录的 SPX 子目录中搭建一个新的 SPX 项目。 看起来是这样的。

$PWD
 |
 +- SPX
      |
      +- Package.swift
      +- .gitignore
      +- Sources/date/main.swift

然后您可以运行 spxspx --listspx -l 来查看当前的executable targets。 然后您可以运行 spx date 来运行名为 date 的简单示例脚本。

用法

常规使用

spx <target-name> [arguments...]

- <target-name>  The name of the `executableTarget` in the
                            `Package.swift` in the `scripts`
                            subdirectory of the current working
                            directory.
- [arguments...]  Arguments passed to the target

可用命令

spx
    list the available targets

spx --list
    list the available targets

spx --version
    show version and exit

spx --help
    show this message

spx --init <template-name>
    scaffold a new SPX scripts subdirectory in
    the current directory. The default scripts
    subdirectory is `SPX`.

spx --add <name>
    add a new script named <name> by
    creating a file at path `Sources/<name>/main.swift`,
    & a file at path `Sources/<Name>Lib/<Name>.swift`,
    & adding their targets to `Package.swift`

spx --build
    update & build the scripts package, as a convenience.

iOS 模板

spx -i ios

这将创建一个 SPX 目录,其中包含一个从 SVG 生成应用程序图标的脚本 (spx appicon) 和一个推送到 App Store 的脚本 (spx appstore)。 在 templates/ios/SPX/README.md 中了解更多信息

演示

demos 文件夹中有一个示例项目

从 Swish 升级

Swish 是此工具之前的名称。

  1. 将您的 Swish 目录重命名为 SPX
  2. Untap 之前的 Homebrew tap:brew untap fullqueuedeveloper/swish
  3. Tap 当前的 Homebrew tap:brew tap DanielSincere/tap
  4. 安装 SPX:brew install spx