[前][次][番号順一覧][スレッド一覧]

ruby-changes:72944

From: Nobuyoshi <ko1@a...>
Date: Wed, 17 Aug 2022 02:00:52 +0900 (JST)
Subject: [ruby-changes:72944] cc443f6cde (master): Refactor `RbInstall::Specs::FileCollector`

https://git.ruby-lang.org/ruby.git/commit/?id=cc443f6cde

From cc443f6cde287944e00ab5d9b6ad868b3d9fc9db Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 17 Aug 2022 00:48:11 +0900
Subject: 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/`.
---
 tool/rbinstall.rb | 107 +++++++++++++++++++++---------------------------------
 1 file 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 https://github.com/ruby/ruby/blob/trunk/tool/rbinstall.rb#L729
 
   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) https://github.com/ruby/ruby/blob/trunk/tool/rbinstall.rb#L958
   }
   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
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]