Skip to content

Commit

Permalink
Improve GemSpecQueryable module
Browse files Browse the repository at this point in the history
- Renamed the parameter of the find_gem_path method to a name that
  indicates it is a file path.
- Renamed find_gemspec_path method to find_spec_file and changed its
  parameter name to clearly indicate it accepts a file path. Updated
  the method to handle path strings as input.
- Implemented the find_spec method which retrieves the corresponding
  Gem::Specification for a given path.
- Updated Ocran.find_gem_files method to utilize find_spec for better
  functionality and clarity.
  • Loading branch information
shinokaro committed Jun 23, 2024
1 parent b89c9b5 commit f1516df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
3 changes: 1 addition & 2 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ EOF
features_from_gems << feature
next
end
spec_path = GemSpecQueryable.find_gemspec_path(feature)
if spec_path && (spec = Gem::Specification.load(spec_path))
if (spec = GemSpecQueryable.find_spec(feature))
gems[spec.name] ||= spec
features_from_gems << feature
else
Expand Down
31 changes: 28 additions & 3 deletions lib/ocran/gem_spec_queryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@ module GemSpecQueryable
GEM_NON_FILE_RE = /(#{GEM_EXTRA_RE}|#{GEM_SCRIPT_RE})/

class << self
def find_gem_path(feature)
# find_gem_path method searches for the path of the gem containing the
# specified path. The 'path' argument is a file or directory path.
# It checks each gem's installation path in Gem.path to see if the
# specified path is a subpath. Returns the gem's path if found, or
# nil if not found.
def find_gem_path(path)
return unless defined?(Gem)

Gem.path.find { |path| Pathname(feature).subpath?(path) }
Gem.path.find { |gem_path| Pathname(path).subpath?(gem_path) }
end

def find_gemspec_path(feature)
# find_spec_file method searches for the path of the gemspec file of
# the gem containing the specified path. The 'path' argument is a file
# or directory path. It searches within the "gems" directory in each
# directory listed in Gem.path to check if the specified path is a
# subpath. If the gemspec file exists, it returns its path; otherwise,
# it returns nil.
def find_spec_file(path)
return unless defined?(Gem)

feature = Pathname(path)
Gem.path.each do |gem_path|
gems_dir = File.join(gem_path, "gems")
next unless feature.subpath?(gems_dir)
Expand All @@ -38,6 +50,19 @@ def find_gemspec_path(feature)
end
nil
end

# find_spec method searches and returns a Gem::Specification object
# based on the specified path. Internally, it uses find_spec_file to
# obtain the path to the gemspec file, and if that file exists, it
# calls Gem::Specification.load to load the gem's specifications.
# Returns the loaded Gem::Specification object, or nil if the gemspec
# file does not exist.
def find_spec(path)
return unless defined?(Gem)

spec_file = find_spec_file(path)
spec_file && Gem::Specification.load(spec_file)
end
end

def gem_root = Pathname(gem_dir)
Expand Down

0 comments on commit f1516df

Please sign in to comment.