SPX 是一个 Swift 脚本运行工具。它能方便地在名为 SPX
的相对目录下运行 Swift 包。
SPX 与 Sh 配合使用效果更佳,可以从 Swift 脚本中运行 shell 命令并处理 shell 输出。
要查看使用 SPX、Sh 和 ShXcrun 的完整示例,请访问 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 目前可以通过 Homebrew、Mint 或手动安装获得。
使用 Homebrew 安装
brew install danielsincere/tap/spx
或者,如果使用 Brewfile
,请将以下行添加到其中
tap "danielsincere/tap"
brew "spx"
使用 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
然后您可以运行 spx
或 spx --list
或 spx -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.
spx -i ios
这将创建一个 SPX
目录,其中包含一个从 SVG 生成应用程序图标的脚本 (spx appicon
) 和一个推送到 App Store 的脚本 (spx appstore
)。 在 templates/ios/SPX/README.md 中了解更多信息
demos
文件夹中有一个示例项目
VaporDemo
是一个 Vapor 应用程序。 这个例子很短,但仍然有意义。 从 demos/VaporDemo
目录运行 spx docker
将为此小型 vapor 应用程序构建 docker 容器。
屏幕截图。 这个 PR 展示了使用 Sh & SPX 进行脚本编写的强大功能。 它使用 CoreGraphics
和 AVFoundation
以及 Sh
来截取屏幕截图,并对其进行处理以用于 App Store 并发布到网站。 0xOpenBytes/ios-base#14
Demo iOS 项目 https://github.com/DanielSincere/SwishExampleiOSProject
Swish 是此工具之前的名称。
Swish
目录重命名为 SPX
brew untap fullqueuedeveloper/swish
brew tap DanielSincere/tap
brew install spx