summaryrefslogtreecommitdiff
path: root/bin/with_rubygems
blob: 59365e40cebd272c15158914b448682eab1133c8 (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
#!/usr/bin/env ruby
# frozen_string_literal: true

require "pathname"
require_relative "../spec/support/helpers"

class RubygemsVersionManager
  include Spec::Helpers
end

rubygems_version_manager = RubygemsVersionManager.new

version = ENV.delete("RGV")
rubygems_path = Pathname.new(version).expand_path
unless rubygems_path.directory?
  rubygems_path = Pathname.new("tmp/rubygems").expand_path
  unless rubygems_path.directory?
    rubygems_path.parent.mkpath
    rubygems_version_manager.sys_exec!("git clone https://github.com/rubygems/rubygems.git #{rubygems_path}")
  end
  Dir.chdir(rubygems_path) do
    rubygems_version_manager.sys_exec!("git remote update")
    version = "v#{version}" if version =~ /\A\d/
    rubygems_version_manager.sys_exec!("git checkout #{version} --quiet")
  end
end

rubygems_lib = rubygems_path + "lib"
ENV["RUBYOPT"] = %(-I#{rubygems_lib} #{ENV["RUBYOPT"]})

if $0 != __FILE__
  ARGV.unshift($0)
elsif cmd = ARGV.first
  possible_dirs = [
    Pathname.new(__FILE__) + "..",
    Pathname.new(__FILE__) + "../../exe",
    rubygems_path + "bin",
  ]
  cmd = possible_dirs.map do |dir|
    dir.join(cmd).expand_path
  end.find(&:file?)
  ARGV[0] = cmd.to_s if cmd
end

exec(*ARGV)