2020-12-08 04:38:25 -05:00
|
|
|
require 'fileutils'
|
2015-01-15 23:52:59 -05:00
|
|
|
require 'rubygems'
|
|
|
|
require 'rubygems/package'
|
|
|
|
|
2016-07-02 17:01:04 -04:00
|
|
|
# This library is used by "make extract-gems" to
|
|
|
|
# unpack bundled gem files.
|
|
|
|
|
2022-07-30 11:04:16 -04:00
|
|
|
module BundledGem
|
|
|
|
module_function
|
|
|
|
|
2022-07-30 08:08:00 -04:00
|
|
|
def unpack(file, *rest)
|
|
|
|
pkg = Gem::Package.new(file)
|
|
|
|
prepare_test(pkg.spec, *rest) {|dir| pkg.extract_files(dir)}
|
|
|
|
puts "Unpacked #{file}"
|
2022-07-17 03:05:16 -04:00
|
|
|
end
|
2022-07-30 08:08:00 -04:00
|
|
|
|
|
|
|
def copy(path, *rest)
|
2022-08-04 04:30:03 -04:00
|
|
|
path, n = File.split(path)
|
|
|
|
spec = Dir.chdir(path) {Gem::Specification.load(n)} or raise "Cannot load #{path}"
|
2022-07-30 08:08:00 -04:00
|
|
|
prepare_test(spec, *rest) do |dir|
|
|
|
|
FileUtils.rm_rf(dir)
|
|
|
|
files = spec.files.reject {|f| f.start_with?(".git")}
|
|
|
|
dirs = files.map {|f| File.dirname(f) if f.include?("/")}.uniq
|
|
|
|
FileUtils.mkdir_p(dirs.map {|d| d ? "#{dir}/#{d}" : dir}.sort_by {|d| d.count("/")})
|
|
|
|
files.each do |f|
|
|
|
|
File.copy_stream(File.join(path, f), File.join(dir, f))
|
|
|
|
end
|
2022-07-17 03:05:16 -04:00
|
|
|
end
|
2022-07-30 08:08:00 -04:00
|
|
|
puts "Copied #{path}"
|
2022-07-13 05:58:32 -04:00
|
|
|
end
|
2022-07-17 03:05:16 -04:00
|
|
|
|
2022-07-30 08:08:00 -04:00
|
|
|
def prepare_test(spec, dir = ".")
|
|
|
|
target = spec.full_name
|
|
|
|
Gem.ensure_gem_subdirectories(dir)
|
|
|
|
gem_dir = File.join(dir, "gems", target)
|
|
|
|
yield gem_dir
|
|
|
|
spec_dir = spec.extensions.empty? ? "specifications" : File.join("gems", target)
|
|
|
|
File.binwrite(File.join(dir, spec_dir, "#{target}.gemspec"), spec.to_ruby)
|
|
|
|
unless spec.extensions.empty?
|
|
|
|
spec.dependencies.clear
|
|
|
|
File.binwrite(File.join(dir, spec_dir, ".bundled.#{target}.gemspec"), spec.to_ruby)
|
|
|
|
end
|
|
|
|
if spec.bindir and spec.executables
|
|
|
|
bindir = File.join(dir, "bin")
|
|
|
|
Dir.mkdir(bindir) rescue nil
|
|
|
|
spec.executables.each do |exe|
|
|
|
|
File.open(File.join(bindir, exe), "wb", 0o777) {|f|
|
|
|
|
f.print "#!ruby\n",
|
|
|
|
%[load File.realpath("../gems/#{target}/#{spec.bindir}/#{exe}", __dir__)\n]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
FileUtils.rm_rf(Dir.glob("#{gem_dir}/.git*"))
|
|
|
|
end
|
2015-01-15 23:52:59 -05:00
|
|
|
end
|