Linux 上的一个 Swift 脚本示例

作者:Joe,原文链接,原文日期:2016/06/27
译者:ahfepj;校对:saitjr;定稿:Cee

如果你在推特上关注了我们(@iachievedit)你就知道我们不仅做了很多 Swift 开源项目,还有做了一些操作系统和架构。我们决定做这个项目没有特别的原因,但是看着各个版本的 git 仓库最终整合一个工具感觉真是太棒了。

我们很高兴最终 Swift 能够取代 Linux 上的其他脚本语言(比如名字读起来像 Bython 和 Buby 的两门语言),对我们来说是时候写脚本了(用 Swift)。

这里有一些注意事项:

  • 我们使用了一个 Stringsubscript 扩展,由于某些原因它可能不会是核心语言的一部分。
  • 我们的版本里的 exec 在其他脚本语言里是反引号(output = `ls`),我们需要执行命令并读取输出。
  • 当然还可以做更多优化和精简。

掌握了这些之后,下面就是 swiftrevs.swift

import Glibc

extension String {
  subscript (r: CountableClosedRange<Int>) -> String {
    get {
      let startIndex = self.characters.index(self.characters.startIndex, offsetBy:r.lowerBound)
      let endIndex   = self.characters.index(self.characters.startIndex, offsetBy:r.upperBound)
      return self[startIndex..<endIndex]
    }
  }
}

func exec(_ command:String) -> String {
  let BUFSIZE = 128
  let pipe = popen(command, "r")
  var buf  = [CChar](repeating:CChar(0), count:BUFSIZE)
  var output:String = ""
  while fgets(&buf, Int32(BUFSIZE), pipe) != nil {
    output = String(cString:buf)
  }
  return output
}

func gitRevision() -> String {
  return exec("/usr/bin/git rev-parse HEAD")[0...9]
}

func gitFetchURL() -> String {
  return exec("/usr/bin/git remote show origin -n|grep Fetch| echo -n `cut --characters=14-`")
}

func gitBranch() -> String {
  return exec("/usr/bin/git branch | echo -n `cut --characters=2-`")
}

let dirs = ["swift", "llvm", "clang", "lldb", "compiler-rt", "cmark", "llbuild", "swiftpm", "swift-corelibs-xctest", "swift-corelibs-foundation", "swift-integration-tests", "swift-corelibs-libdispatch"]

for dir in dirs {
  let cwd     = String(cString:getcwd(nil, 0))
  let rc      = chdir(dir) // pushd
  guard rc == 0 else {
    continue
  }
  let fetch  = gitFetchURL()
  let rev    = gitRevision()
  let branch = gitBranch()
  print("\(dir),\(fetch),\(branch),\(rev)")
  chdir(cwd) // popd
}

我们按照如下方法来使用:

bash
# swift swiftrevs.swift
swift,https://github.com/apple/swift.git,master,cf3fdff04
llvm,https://github.com/apple/swift-llvm.git,stable,8d0086ac3
clang,https://github.com/apple/swift-clang.git,stable,460d629e8
lldb,https://github.com/apple/swift-lldb.git,master,da120cf91
compiler-rt,https://github.com/apple/swift-compiler-rt.git,stable,9daa1b32c
cmark,https://github.com/apple/swift-cmark.git,master,5af77f3c1
llbuild,https://github.com/apple/swift-llbuild.git,master,7aadcf936
swiftpm,https://github.com/apple/swift-package-manager.git,master,423c2a1c8
swift-corelibs-xctest,https://github.com/apple/swift-corelibs-xctest.git,master,03905f564
swift-corelibs-foundation,https://github.com/apple/swift-corelibs-foundation.git,master,4c15543f8
swift-integration-tests,https://github.com/apple/swift-integration-tests.git,master,4eda8055a
swift-corelibs-libdispatch,https://github.com/apple/swift-corelibs-libdispatch.git,master,e922531c2

当然,这都是在满足构建 Swift 的仓库目录下运行的,如果你有兴趣学习为 X86 或者 ARM 设备构建 Swift ,请查阅我们的 package-swift 仓库。其中不仅包含 swiftrevs.swift ,还有几个能无痛构建 Swift(和 REPL、Foundation、Swift 包管理器)的脚本。

本文由 SwiftGG 翻译组翻译,已经获得作者翻译授权,最新文章请访问 http://swift.gg

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 10,441评论 0 6
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 UI下拉刷新模糊效果A...
    袁俊亮技术博客阅读 14,099评论 9 105
  • “她好像要结婚了呢!” “谁啊?” “林溪。” “喔!是吗?挺好的。” “牧烨,你是不是还在爱着林溪啊?” 牧烨随...
    丁走心阅读 2,507评论 0 4
  • 不习惯玩微信,也不喜欢玩微博,但是喜欢玩QQ。每天习惯了打开动态给大家点赞,不仅知道大家的动态,跟上潮流;也为了...
    九揺阅读 1,170评论 0 0
  • 端午回家待了不到2天,没有见到多少人,少了很多熟悉的脸,可我的记忆怎么总是停留在许多年前充满着欢声笑语的那个时候呢...
    属猫的鱼阅读 3,315评论 6 3