aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/requirements/ruby_requirement.rb
blob: a9ec8c42d27803ab0dc9acc342d6b10dfe953cbd (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
class RubyRequirement < Requirement
  fatal true
  default_formula "ruby"

  def initialize(tags)
    @version = tags.shift if /(\d\.)+\d/ =~ tags.first
    raise "RubyRequirement requires a version!" unless @version
    super
  end

  satisfy(build_env: false) { new_enough_ruby }

  env do
    ENV.prepend_path "PATH", new_enough_ruby.dirname
  end

  def message
    s = "Ruby >= #{@version} is required to install this formula."
    s += super
    s
  end

  def inspect
    "#<#{self.class.name}: #{name.inspect} #{tags.inspect} version=#{@version.inspect}>"
  end

  def display_s
    if @version
      "#{name} >= #{@version}"
    else
      name
    end
  end

  private

  def new_enough_ruby
    rubies.detect { |ruby| new_enough?(ruby) }
  end

  def rubies
    rubies = which_all("ruby")
    ruby_formula = Formula["ruby"]
    rubies.unshift ruby_formula.bin/"ruby" if ruby_formula&.installed?
    rubies.uniq
  end

  def new_enough?(ruby)
    version = Utils.popen_read(ruby, "-e", "print RUBY_VERSION").strip
    version =~ /^\d+\.\d+/ && Version.create(version) >= min_version
  end

  def min_version
    @min_version ||= Version.create(@version)
  end
end