Linux 上的一个 Swift 脚本示例
如果你在推特上关注了我们( @iachievedit )你就知道 我们不仅做了很多 Swift 开源项目 ,还有做了一些操作系统和架构。我们决定做这个项目没有特别的原因,但是看着各个版本的 git 仓库最终整合一个工具感觉真是太棒了。
我们很高兴最终 Swift 能够取代 Linux 上的其他脚本语言(比如名字读起来像 Bython 和 Buby 的两门语言),对我们来说是时候写脚本了(用 Swift)。
这里有一些注意事项:
1.我们使用了一个 String 的 subscript 扩展,由于某些原因它可能不会是核心语言的一部分。
2.我们的版本里的 exec 在其他脚本语言里是反引号( output = `ls` ),我们需要执行命令并读取输出。
3.当然还可以做更多优化和精简。
掌握了这些之后,下面就是 swiftrevs.swift :
import Glibcextension String { subscript (r: CountableClosedRange
我们按照如下方法来使用:
# swift swiftrevs.swiftswift,https://github.com/apple/swift.git,master,cf3fdff04llvm,https://github.com/apple/swift-llvm.git,stable,8d0086ac3clang,https://github.com/apple/swift-clang.git,stable,460d629e8lldb,https://github.com/apple/swift-lldb.git,master,da120cf91compiler-rt,https://github.com/apple/swift-compiler-rt.git,stable,9daa1b32ccmark,https://github.com/apple/swift-cmark.git,master,5af77f3c1llbuild,https://github.com/apple/swift-llbuild.git,master,7aadcf936swiftpm,https://github.com/apple/swift-package-manager.git,master,423c2a1c8swift-corelibs-xctest,https://github.com/apple/swift-corelibs-xctest.git,master,03905f564swift-corelibs-foundation,https://github.com/apple/swift-corelibs-foundation.git,master,4c15543f8swift-integration-tests,https://github.com/apple/swift-integration-tests.git,master,4eda8055aswift-corelibs-libdispatch,https://github.com/apple/swift-corelibs-libdispatch.git,master,e922531c2
当然,这都是在满足构建 Swift 的仓库目录下运行的,如果你有兴趣学习为 X86 或者 ARM 设备构建 Swift ,请查阅我们的 package-swift 仓库。其中不仅包含 swiftrevs.swift ,还有几个能无痛构建 Swift(和 REPL、Foundation、Swift 包管理器)的脚本。
- 赞