|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'tmpdir' |
| 4 | +require 'fileutils' |
| 5 | + |
| 6 | +def pipe(command) |
| 7 | + output = "" |
| 8 | + IO.popen(command) do |io| |
| 9 | + until io.eof? |
| 10 | + buffer = io.gets |
| 11 | + output << buffer |
| 12 | + puts buffer |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + output |
| 17 | +end |
| 18 | + |
| 19 | +workspace_dir = ARGV[0] |
| 20 | +output_dir = ARGV[1] |
| 21 | + |
| 22 | +vendor_url = "https://s3.amazonaws.com/#{ENV['S3_BUCKET_NAME'] ? ENV['S3_BUCKET_NAME'] : 'heroku-buildpack-ruby'}" |
| 23 | +full_version = ENV['VERSION'] |
| 24 | +full_name = "ruby-#{full_version}" |
| 25 | +version = full_version.split('-').first |
| 26 | +name = "ruby-#{version}" |
| 27 | +major_ruby = version.match(/\d\.\d/)[0] |
| 28 | +build = false |
| 29 | +build = true if ENV["BUILD"] |
| 30 | +debug = nil |
| 31 | +debug = true if ENV['DEBUG'] |
| 32 | +jobs = ENV['JOBS'] || 2 |
| 33 | +rubygems = ENV['RUBYGEMS_VERSION'] ? ENV['RUBYGEMS_VERSION'] : nil |
| 34 | +git_url = ENV["GIT_URL"] |
| 35 | + |
| 36 | +if git_url |
| 37 | + url, treeish = git_url.split('#', 2) |
| 38 | + full_name = "ruby" |
| 39 | + pipe "git clone #{url}" |
| 40 | +else |
| 41 | + pipe "curl http://ftp.ruby-lang.org/pub/ruby/#{major_ruby}/#{full_name}.tar.gz -s -o - | tar zxf -" |
| 42 | +end |
| 43 | + |
| 44 | +Dir.mktmpdir("ruby-vendor-") do |vendor_dir| |
| 45 | + Dir.chdir(vendor_dir) do |
| 46 | + `curl "#{vendor_url}/libyaml-0.1.5.tgz" -s -o - | tar zxf -` |
| 47 | + `curl "#{vendor_url}/libffi-3.0.10.tgz" -s -o - | tar zxf -` |
| 48 | + `curl http://production.cf.rubygems.org/rubygems/rubygems-#{rubygems}.tgz -s -o - | tar xzf -` if rubygems |
| 49 | + end |
| 50 | + |
| 51 | + prefix = "/app/vendor/#{name}" |
| 52 | + prefix = "/tmp/#{name}" if build |
| 53 | + |
| 54 | + puts "prefix: #{prefix}" |
| 55 | + |
| 56 | + Dir.chdir(full_name) do |
| 57 | + pipe "git checkout #{treeish}" if treeish |
| 58 | + |
| 59 | + if debug |
| 60 | + configure_env = "optflags=\"-O0\" debugflags=\"-g3 -ggdb\"" |
| 61 | + else |
| 62 | + configure_env = "debugflags=\"-g\"" |
| 63 | + end |
| 64 | + |
| 65 | + configure_opts = "--disable-install-doc --prefix #{prefix}" |
| 66 | + configure_opts += " --enable-load-relative" if major_ruby != "1.8" && version != "1.9.2" |
| 67 | + puts "configure env: #{configure_env}" |
| 68 | + puts "configure opts: #{configure_opts}" |
| 69 | + cmds = [ |
| 70 | + "#{configure_env} ./configure #{configure_opts}", |
| 71 | + "env CPATH=#{vendor_dir}/include:\\$CPATH CPPATH=#{vendor_dir}/include:\\$CPPATH LIBRARY_PATH=#{vendor_dir}/lib:\\$LIBRARY_PATH make -j#{jobs}", |
| 72 | + "make install" |
| 73 | + ] |
| 74 | + cmds.unshift("#{configure_env} autoconf") if git_url |
| 75 | + pipe(cmds.join(" && ")) |
| 76 | + end |
| 77 | + if rubygems |
| 78 | + Dir.chdir("#{vendor_dir}/rubygems-#{rubygems}") do |
| 79 | + pipe("#{prefix}/bin/ruby setup.rb") |
| 80 | + end |
| 81 | + gem_bin_file = "#{prefix}/bin/gem" |
| 82 | + gem = File.read(gem_bin_file) |
| 83 | + File.open(gem_bin_file, 'w') do |file| |
| 84 | + file.puts "#!/usr/bin/env ruby" |
| 85 | + lines = gem.split("\n") |
| 86 | + lines.shift |
| 87 | + lines.each {|line| file.puts line } |
| 88 | + end |
| 89 | + end |
| 90 | + pipe "ls #{prefix}" |
| 91 | + pipe("tar czPf #{output_dir}/#{name}.tgz #{prefix}/*") |
| 92 | +end |
0 commit comments