Skip to content

Commit

Permalink
Implement extract_gem_files method
Browse files Browse the repository at this point in the history
- Implemented the extract_gem_files method which now handles the fallback logic for gem detection previously in the find_gem_files method.
  • Loading branch information
shinokaro committed Jun 24, 2024
1 parent f1516df commit 4a2689c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,38 @@ EOF
gems
end

def self.extract_gem_files(features)
require_relative "../lib/ocran/gem_spec_queryable"
features_from_gems = []
gems = {}

# Include Gems that are loaded
Gem.loaded_specs.each { |name, spec| gems[name] ||= spec }
# Fall back to gem detection (loaded_specs are not population on
# all Ruby versions)
features.each do |feature|
# Detect load path unless absolute
if feature.relative?
load_path = find_load_path($LOAD_PATH, feature)
next if load_path.nil? # Could be enumerator.so
feature = feature.expand_path(load_path)
end
# Skip if found in known Gem dir
if gems.find { |_gem, spec| feature.subpath?(spec.gem_dir) }
features_from_gems << feature
next
end
if (spec = GemSpecQueryable.find_spec(feature))
gems[spec.name] ||= spec
features_from_gems << feature
else
Ocran.warn "Failed to load gemspec for #{feature}"
end
end

return features_from_gems, gems
end

# Searches for features that are loaded from gems, then produces a
# list of files included in those gems' manifests. Also returns a
# list of original features that caused those gems to be
Expand All @@ -408,30 +440,9 @@ EOF
gems = Ocran.scanning_gemfile

if defined?(Gem)
require_relative "../lib/ocran/gem_spec_queryable"
# Include Gems that are loaded
Gem.loaded_specs.each { |gemname, spec| gems[gemname] ||= spec }
# Fall back to gem detection (loaded_specs are not population on
# all Ruby versions)
features.each do |feature|
# Detect load path unless absolute
if feature.relative?
load_path = find_load_path($LOAD_PATH, feature)
next if load_path.nil? # Could be enumerator.so
feature = feature.expand_path(load_path)
end
# Skip if found in known Gem dir
if gems.find { |gem, spec| feature.subpath?(spec.gem_dir) }
features_from_gems << feature
next
end
if (spec = GemSpecQueryable.find_spec(feature))
gems[spec.name] ||= spec
features_from_gems << feature
else
Ocran.warn "Failed to load gemspec for #{feature}"
end
end
features_from_gems, detected_gems = Ocran.extract_gem_files(features)
# Prioritize the spec detected from Bundler.
gems = detected_gems.merge(gems)

gems.each do |gemname, spec|
# In an environment executed from `bundle exec`, `bundler`'s `spec.gem_dir`
Expand Down

0 comments on commit 4a2689c

Please sign in to comment.