{"id":3106,"date":"2016-06-27T06:32:17","date_gmt":"2016-06-27T12:32:17","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=3106"},"modified":"2016-06-27T06:32:17","modified_gmt":"2016-06-27T12:32:17","slug":"an-example-of-scripting-with-swift-on-linux","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/an-example-of-scripting-with-swift-on-linux\/","title":{"rendered":"An Example of Scripting with Swift on Linux"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\" \/> <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/OS-Linux-blue.svg?style=flat\" alt=\"Linux\" \/><\/p>\n<p>If you follow us on Twitter (<a href=\"https:\/\/twitter.com\/iachievedit\">@iachievedit<\/a>) you know that <a href=\"http:\/\/swift-arm.ddns.net\">we build open source Swift a lot<\/a>, and for a number of different OSes and architectures.  There&#8217;s no rhyme or reason as to when we decide to start building, but it&#8217;s nice to keep track of the various <code>git<\/code> revisions of the repositories that went into a given artifact.<\/p>\n<p>Since we&#8217;re excited about Swift eventually displacing other scripting languages on Linux (their names rhyme with Bython and Buby), it felt like a good time to write up a script (in Swift) that did this for us.<\/p>\n<p>Some notes here:<\/p>\n<ul>\n<li>we&#8217;re using a `subscript` extension of `String` that will likely never be a part of the core language for reasons.\n<li>`exec` is our version of backticks (`output = &#96;ls&#96;`) in other scripting languages.  We want to execute a command and read its output.\n<li>there&#8217;s certainly more optimization and tightening that can be done.\n<\/ul>\n<p>With that out of the way, here is <code>swiftrevs.swift<\/code>:<\/p>\n<pre class=\"lang:swift\">\nimport Glibc\n\nextension String {\n  subscript (r: CountableClosedRange<Int>) -> String {\n    get {\n      let startIndex = self.characters.index(self.characters.startIndex, offsetBy:r.lowerBound)\n      let endIndex   = self.characters.index(self.characters.startIndex, offsetBy:r.upperBound)\n      return self[startIndex..<endIndex]\n    }\n  }\n}\n\nfunc exec(_ command:String) -> String {\n  let BUFSIZE = 128\n  let pipe = popen(command, \"r\")\n  var buf  = [CChar](repeating:CChar(0), count:BUFSIZE)\n  var output:String = \"\"\n  while fgets(&buf, Int32(BUFSIZE), pipe) != nil {\n    output = String(cString:buf)\n  }\n  return output\n}\n\nfunc gitRevision() -> String {\n  return exec(\"\/usr\/bin\/git rev-parse HEAD\")[0...9]\n}\n\nfunc gitFetchURL() -> String {\n  return exec(\"\/usr\/bin\/git remote show origin -n|grep Fetch| echo -n `cut --characters=14-`\")\n}\n\nfunc gitBranch() -> String {\n  return exec(\"\/usr\/bin\/git branch | echo -n `cut --characters=2-`\")\n}\n\nlet dirs = [\"swift\", \"llvm\", \"clang\", \"lldb\", \"compiler-rt\", \"cmark\", \"llbuild\", \"swiftpm\", \"swift-corelibs-xctest\", \"swift-corelibs-foundation\", \"swift-integration-tests\", \"swift-corelibs-libdispatch\"]\n\nfor dir in dirs {\n  let cwd     = String(cString:getcwd(nil, 0))\n  let rc      = chdir(dir) \/\/ pushd\n  guard rc == 0 else {\n    continue\n  }\n  let fetch  = gitFetchURL()\n  let rev    = gitRevision()\n  let branch = gitBranch()\n  print(\"\\(dir),\\(fetch),\\(branch),\\(rev)\")\n  chdir(cwd) \/\/ popd\n}\n<\/pre>\n<p>We use this as follows:<\/p>\n<pre class=\"crayon:false\">\n# swift swiftrevs.swift\nswift,https:\/\/github.com\/apple\/swift.git,master,cf3fdff04\nllvm,https:\/\/github.com\/apple\/swift-llvm.git,stable,8d0086ac3\nclang,https:\/\/github.com\/apple\/swift-clang.git,stable,460d629e8\nlldb,https:\/\/github.com\/apple\/swift-lldb.git,master,da120cf91\ncompiler-rt,https:\/\/github.com\/apple\/swift-compiler-rt.git,stable,9daa1b32c\ncmark,https:\/\/github.com\/apple\/swift-cmark.git,master,5af77f3c1\nllbuild,https:\/\/github.com\/apple\/swift-llbuild.git,master,7aadcf936\nswiftpm,https:\/\/github.com\/apple\/swift-package-manager.git,master,423c2a1c8\nswift-corelibs-xctest,https:\/\/github.com\/apple\/swift-corelibs-xctest.git,master,03905f564\nswift-corelibs-foundation,https:\/\/github.com\/apple\/swift-corelibs-foundation.git,master,4c15543f8\nswift-integration-tests,https:\/\/github.com\/apple\/swift-integration-tests.git,master,4eda8055a\nswift-corelibs-libdispatch,https:\/\/github.com\/apple\/swift-corelibs-libdispatch.git,master,e922531c2\n<\/pre>\n<p>This, of course, is run in a directory that has all of the various repositories required to build Swift cloned.  If you&#8217;re interested in learning how to build Swift for X86 or certain ARM devices, check out our <a href=\"https:\/\/github.com\/iachievedit\/package-swift\">package-swift<\/a> repository.  It not only contains <code>swiftrevs.swift<\/code> but also a handful of scripts aimed at making building Swift (along with the REPL, Foundation, and Swift Package Manager) nearly painless.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you follow us on Twitter (@iachievedit) you know that we build open source Swift a lot, and for a number of different OSes and architectures. There&#8217;s no rhyme or reason as to when we decide to start building, but it&#8217;s nice to keep track of the various git revisions of the repositories that went [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3106"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=3106"}],"version-history":[{"count":12,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3106\/revisions"}],"predecessor-version":[{"id":3119,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3106\/revisions\/3119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=3106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=3106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=3106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}