summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-08-17 00:48:11 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-08-17 00:58:56 +0900
commitcc443f6cde287944e00ab5d9b6ad868b3d9fc9db (patch)
treeed2fadfd68c22cd6d75c5d7446ce6a365599e576
parent5528648a913cee8a0b3c8b0bc59764e3c361c404 (diff)
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/`.
-rwxr-xr-xtool/rbinstall.rb107
1 files changed, 41 insertions, 66 deletions
diff --git a/tool/rbinstall.rb b/tool/rbinstall.rb
index e6d0f592f5..68c96bee85 100755
--- a/tool/rbinstall.rb
+++ b/tool/rbinstall.rb
@@ -729,86 +729,60 @@ 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"
- 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/, "")
- # 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])
- end
+ def ruby_libraries
+ Dir.glob("lib/**/*.rb", base: "#{srcdir}/ext/#{relative_base}")
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"
+ class Lib < self
+ def skip_install?(files)
+ files.empty?
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)
+ def ruby_libraries
+ gemname = File.basename(gemspec, ".gemspec")
+ base = relative_base || gemname
+ # for lib/net/net-smtp.gemspec
+ if m = /.*(?=-(.*)\z)/.match(gemname)
+ base = File.join(base, *m.to_a.select {|n| !base.include?(n)})
+ end
+ 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
- []
- end
- end
- def relative_base
- /\/#{Regexp.escape(type)}\/(.*?)\z/ =~ @base_dir
- $1
- end
+ case gemname
+ when "net-http"
+ files << "lib/net/https.rb"
+ when "optparse"
+ files << "lib/optionparser.rb"
+ end
- def remove_prefix(prefix, string)
- string.sub(/\A#{Regexp.escape(prefix)}/, "")
+ files
+ end
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