Skip to content

Instantly share code, notes, and snippets.

@dorentus
Last active March 3, 2016 06:38
Show Gist options
  • Save dorentus/53545344d52ee0bc84f3 to your computer and use it in GitHub Desktop.
Save dorentus/53545344d52ee0bc84f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "tmpdir"
version = ARGV[0].to_s
abort("Usage: swift-install <VERSION> <DESTINATION>") if version.empty?
dest = ARGV[1].to_s
abort("Usage: swift-install <VERSION> <DESTINATION>") if dest.empty?
dest = File.expand_path dest, Dir.pwd
Dir.mkdir dest unless Dir.exists? dest
case RUBY_PLATFORM
when /darwin/
url = "https://swift.org/builds/development/xcode/swift-#{version}/swift-#{version}-osx.pkg"
is_osx = true
when /linux/
url = "https://swift.org/builds/development/ubuntu1404/swift-#{version}/swift-#{version}-ubuntu14.04.tar.gz"
is_osx = false
else
about("unsupported platform")
end
def process(url, dir)
Dir.chdir(dir) do
ok = system "wget #{url}"
abort("File Not Found: #{url}") unless ok
file = Dir.entries(dir).select { |e| File.file? e }[0]
yield file
end
end
Dir.mktmpdir("swift-install") do |dir|
process(url, dir) do |file|
if is_osx
subdir = Dir.mkdir("unpacked")
Dir.chdir("unpacked") do
system "xar -xf ../#{file}"
payload = Dir.glob("**/Payload")[0]
system "tar -C #{dest} -zxf #{payload}"
end
else
system "tar -C #{dest} -xf #{file} --strip 1"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment