Skip to content

Instantly share code, notes, and snippets.

@headius

headius/.diff Secret

Created February 14, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/d705166abfd1b29c0ad0d14a27da2797 to your computer and use it in GitHub Desktop.
Save headius/d705166abfd1b29c0ad0d14a27da2797 to your computer and use it in GitHub Desktop.
commit 28c1f2247d420506c70acea8aeb54f6c83f159eb
Author: Samuel Giddins <segiddins@segiddins.me>
Date: Tue Jun 26 22:01:42 2018 -0700
Speed up globbing relative to given directories
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
index cb52cd11..f8975806 100644
--- a/lib/rubygems.rb
+++ b/lib/rubygems.rb
@@ -525,8 +525,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
end
def self.find_files_from_load_path glob # :nodoc:
+ glob_with_suffixes = "#{glob}#{Gem.suffix_pattern}"
$LOAD_PATH.map { |load_path|
- Dir["#{File.expand_path glob, load_path}#{Gem.suffix_pattern}"]
+ Gem::Util.glob_files_in_dir(glob_with_suffixes, load_path)
}.flatten.select { |file| File.file? file.untaint }
end
@@ -1118,8 +1119,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
path = "rubygems_plugin"
files = []
+ glob = "#{path}#{Gem.suffix_pattern}"
$LOAD_PATH.each do |load_path|
- globbed = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"]
+ globbed = Gem::Util.glob_files_in_dir(glob, load_path)
globbed.each do |load_path_file|
files << load_path_file if File.file?(load_path_file.untaint)
diff --git a/lib/rubygems/indexer.rb b/lib/rubygems/indexer.rb
index 2e59e790..5607bf3c 100644
--- a/lib/rubygems/indexer.rb
+++ b/lib/rubygems/indexer.rb
@@ -271,7 +271,7 @@ class Gem::Indexer
# List of gem file names to index.
def gem_file_list
- Dir[File.join(@dest_directory, "gems", '*.gem')]
+ Gem::Util.glob_files_in_dir("*.gem", File.join(@dest_directory, "gems"))
end
##
diff --git a/lib/rubygems/installer.rb b/lib/rubygems/installer.rb
index 3b13d233..fd808590 100644
--- a/lib/rubygems/installer.rb
+++ b/lib/rubygems/installer.rb
@@ -379,7 +379,7 @@ class Gem::Installer
@specs ||= begin
specs = []
- Dir[File.join(gem_home, "specifications", "*.gemspec")].each do |path|
+ Gem::Util.glob_files_in_dir("*.gemspec", File.join(gem_home, "specifications")).each do |path|
spec = Gem::Specification.load path.untaint
specs << spec if spec
end
diff --git a/lib/rubygems/request_set.rb b/lib/rubygems/request_set.rb
index 89f47616..ed99d292 100644
--- a/lib/rubygems/request_set.rb
+++ b/lib/rubygems/request_set.rb
@@ -417,7 +417,7 @@ class Gem::RequestSet
end
def specs_in dir
- Dir["#{dir}/specifications/*.gemspec"].map do |g|
+ Gem::Util.glob_files_in_dir("*.gemspec", File.join(dir, "specifications")).map do |g|
Gem::Specification.load g
end
end
diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb
index ffbf142f..2eb35407 100644
--- a/lib/rubygems/specification.rb
+++ b/lib/rubygems/specification.rb
@@ -761,14 +761,14 @@ class Gem::Specification < Gem::BasicSpecification
def self.each_gemspec(dirs) # :nodoc:
dirs.each do |dir|
- Dir[File.join(dir, "*.gemspec")].each do |path|
+ Gem::Util.glob_files_in_dir("*.gemspec", dir).each do |path|
yield path.untaint
end
end
end
def self.gemspec_stubs_in dir, pattern
- Dir[File.join(dir, pattern)].map { |path| yield path }.select(&:valid?)
+ Gem::Util.glob_files_in_dir(pattern, dir).map { |path| yield path }.select(&:valid?)
end
private_class_method :gemspec_stubs_in
diff --git a/lib/rubygems/util.rb b/lib/rubygems/util.rb
index e185c9c6..6dbcd4ba 100644
--- a/lib/rubygems/util.rb
+++ b/lib/rubygems/util.rb
@@ -116,4 +116,16 @@ module Gem::Util
end
end
+ ##
+ # Globs for files matching +pattern+ inside of +directory+,
+ # returning absolute paths to the matching files.
+
+ def self.glob_files_in_dir(glob, base_path)
+ if RUBY_VERSION >= "2.5"
+ Dir.glob(glob, base: base_path).map! {|f| File.join(base_path, f) }
+ else
+ Dir.glob(File.expand_path(glob, base_path))
+ end
+ end
+
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment