Refactor `RbInstall::Specs::FileCollector`

- Split into `Ext` and `Lib` classes.
- `Ext#files` should not include built extension libraries.
- `Ext#files` should include scripts under its own `lib`.
- `Lib#files` should be prefixed with `lib/`.
This commit is contained in:
Nobuyoshi Nakada 2022-08-17 00:48:11 +09:00
Родитель 5528648a91
Коммит cc443f6cde
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
1 изменённых файлов: 43 добавлений и 68 удалений

Просмотреть файл

@ -729,87 +729,61 @@ module RbInstall
module Specs
class FileCollector
def initialize(gemspec)
def self.for(srcdir, type, gemspec)
relative_base = (File.dirname(gemspec) if gemspec.include?("/"))
const_get(type.capitalize).new(gemspec, srcdir, relative_base)
end
attr_reader :gemspec, :srcdir, :relative_base
def initialize(gemspec, srcdir, relative_base)
@gemspec = gemspec
@base_dir = File.dirname(gemspec)
@srcdir = srcdir
@relative_base = relative_base
end
def collect
(ruby_libraries + built_libraries).sort
ruby_libraries.sort
end
def skip_install?(files)
case type
when "ext"
class Ext < self
def skip_install?(files)
# install ext only when it's configured
!File.exist?("#{$ext_build_dir}/#{relative_base}/Makefile")
when "lib"
end
def ruby_libraries
Dir.glob("lib/**/*.rb", base: "#{srcdir}/ext/#{relative_base}")
end
end
class Lib < self
def skip_install?(files)
files.empty?
end
end
private
def type
/\/(ext|lib)?\/.*?\z/ =~ @base_dir
$1
end
def ruby_libraries
case type
when "ext"
prefix = "#{$extout}/common/"
base = "#{prefix}#{relative_base}"
when "lib"
base = @base_dir
prefix = base.sub(/lib\/.*?\z/, "")
def ruby_libraries
gemname = File.basename(gemspec, ".gemspec")
base = relative_base || gemname
# for lib/net/net-smtp.gemspec
if m = File.basename(@gemspec, ".gemspec").match(/.*\-(.*)\z/)
base = "#{@base_dir}/#{m[1]}" unless remove_prefix(prefix, @base_dir).include?(m[1])
if m = /.*(?=-(.*)\z)/.match(gemname)
base = File.join(base, *m.to_a.select {|n| !base.include?(n)})
end
end
files = if base
Dir.glob("#{base}{.rb,/**/*.rb}").collect do |ruby_source|
remove_prefix(prefix, ruby_source)
end
else
[@gemspec[%r[(?:[^/]+/)?[^/]+(?=\.gemspec\z)]] + '.rb']
end
case File.basename(@gemspec, ".gemspec")
when "net-http"
files << "lib/net/https.rb"
when "optparse"
files << "lib/optionparser.rb"
end
files
end
def built_libraries
case type
when "ext"
prefix = "#{$extout}/#{CONFIG['arch']}/"
base = "#{prefix}#{relative_base}"
dlext = CONFIG['DLEXT']
Dir.glob("#{base}{.#{dlext},/**/*.#{dlext}}").collect do |built_library|
remove_prefix(prefix, built_library)
files = Dir.glob("lib/#{base}{.rb,/**/*.rb}", base: srcdir)
if !relative_base and files.empty? # no files at the toplevel
# pseudo gem like ruby2_keywords
files << "lib/#{gemname}.rb"
end
when "lib"
[]
else
[]
case gemname
when "net-http"
files << "lib/net/https.rb"
when "optparse"
files << "lib/optionparser.rb"
end
files
end
end
def relative_base
/\/#{Regexp.escape(type)}\/(.*?)\z/ =~ @base_dir
$1
end
def remove_prefix(prefix, string)
string.sub(/\A#{Regexp.escape(prefix)}/, "")
end
end
end
@ -984,9 +958,10 @@ def install_default_gem(dir, srcdir, bindir)
}
default_spec_dir = Gem.default_specifications_dir
gems = Dir.glob("#{srcdir}/#{dir}/**/*.gemspec").map {|src|
spec = load_gemspec(src)
file_collector = RbInstall::Specs::FileCollector.new(src)
base = "#{srcdir}/#{dir}"
gems = Dir.glob("**/*.gemspec", base: base).map {|src|
spec = load_gemspec("#{base}/#{src}")
file_collector = RbInstall::Specs::FileCollector.for(srcdir, dir, src)
files = file_collector.collect
if file_collector.skip_install?(files)
next