aboutsummaryrefslogtreecommitdiffstats
path: root/Rakefile
blob: 4f7695105475f7f228c758fea28b91c6fb1c6f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require "fileutils"
require "json"
require "tmpdir"
require "shellwords"

begin
  $config_file = File.expand_path(ENV["CONFIG"] || "config.json")
  $config = JSON.parse(File.read($config_file))
rescue
  puts "Failed to load config: #{$config_file}"
  puts "You can specify config file with CONFIG environment variable"
  raise
end

def save_config
  File.write($config_file, JSON.pretty_generate($config))
end

def retriable
  yield
rescue
  puts "An error occurred: #{$!}"
  puts "Current directory: #{`pwd`}"
  print "Press a key to retry:"
  $stdin.getc
  retry
end

namespace :compiler do
  RUBY_MIRROR = "https://cache.ruby-lang.org/pub/ruby"
  desc "Install a ruby"
  task :ruby, [:version] do |t, args|
    version = args[:version]
    id = "ruby-#{version}"
    if version =~ /^(1\.1[a-d]|1\.[02-9]|2\..)/
      url = "#{RUBY_MIRROR}/#{$1}/ruby-#{version}.tar.gz"
    elsif version =~ /^0\./
      url = "#{RUBY_MIRROR}/1.0/ruby-#{version}.tar.gz"
    else
      raise ArgumentError, "unknown ruby"
    end

    destdir = $config["datadir"] + "/env/ruby/#{id}"
    raise ArgumentError, "already installed?" if Dir.exist?(destdir.to_s)
    prefix = "/opt"

    Dir.mktmpdir { |dir|
      FileUtils.chdir(dir) {
        system("curl -o archive.tar.gz #{Shellwords.escape(url)}") or raise("failed to download")
        system("tar xf archive.tar.gz") or raise("failed to extract")
        FileUtils.chdir("ruby-#{version}") {
          to_be_applied = []
          patch_ccnames = ["ruby/#{version.split("-").join("/")}", "ruby/#{version.split("-")[0]}", "ruby"]
          patch_ccnames.each { |patch_ccname|
            rvm_patchsets_path = File.expand_path("../vendor/rvm/patchsets/#{patch_ccname}/default", __FILE__)
            if File.exist?(rvm_patchsets_path)
              patches = File.read(rvm_patchsets_path).lines.map(&:chomp)
              puts "RVM patchset found (#{patch_ccname})... #{patches.join(" ")}"
              to_be_applied += patches
            end
          }
          to_be_applied.uniq.each { |patch|
            patch_path = patch_ccnames
              .flat_map { |pp| ["patch", "diff"].map { |ext| File.expand_path("../vendor/rvm/patches/#{pp}/#{patch}.#{ext}", __FILE__) } }
              .find(&File.method(:exist?))
            puts "applying... #{patch}"
            patch_path and system("patch -R -N -p1 --dry-run <#{patch_path} || patch -N -p1 <#{patch_path}") or
              raise("failed to apply patch")
          }
          retriable {
            system("./configure --prefix=#{prefix} --enable-shared --disable-install-doc") or raise("failed to configure")
            system("make -j6") or raise("failed to make")
            system("make install DESTDIR=#{destdir}") or raise("failed to install")
          }

          ($config["compilers"]["ruby"] ||= {})[id] = {
            version: `LD_LIBRARY_PATH=#{destdir}#{prefix}/lib #{destdir}#{prefix}/bin/ruby -v`.lines.first.chomp,
            commandline: ["#{prefix}/bin/ruby", "{}"]
          }
          save_config
        }
      }
    }
  end

  PHPS = {
    "7.0.3" => "http://jp2.php.net/distributions/php-7.0.3.tar.gz",
    "5.6.17" => "http://jp2.php.net/distributions/php-5.6.17.tar.gz",
  }
  desc "Install a php"
  task :php, [:version] do |t, args|
    version = args[:version]
    id = "ruby-#{version}"
    url = PHPS[version] or raise(ArgumentError, "unknown php")
    name = url.split("/").last

    destdir = $config["datadir"] + "/env/php/#{id}"
    raise ArgumentError, "already installed?" if Dir.exist?(destdir.to_s)
    prefix = "/opt"

    Dir.mktmpdir { |dir|
      FileUtils.chdir(dir) {
        system("curl -O #{Shellwords.escape(url)}") or raise("failed to download")
        system("tar xf #{Shellwords.escape(name)}") or raise("failed to extract")
        FileUtils.chdir(name.split(".tar.gz").first) {
          retriable {
            system("./configure --prefix=#{prefix} --enable-shared") or raise("failed to configure")
            system("make -j6") or raise("failed to make")
            system("make install INSTALL_ROOT=#{destdir.to_s}") or raise("failed to install")
          }

          ($config["compilers"]["php"] ||= {})[id] = {
            version: `LD_LIBRARY_PATH=#{destdir}#{prefix}/lib #{destdir}#{prefix}/bin/php -v`.lines.first.chomp,
            commandline: ["#{prefix}/bin/php", "{}"]
          }
          save_config
        }
      }
    }
  end
end