Skip to content

Commit

Permalink
Refactor find_gem_files to include spec objects in its return value
Browse files Browse the repository at this point in the history
- Enhanced the return value of find_gem_files method by adding a collection of spec objects. This modification eliminates the dependency on the @gemspecs instance variable, which has now been removed and replaced with a regular variable.
- Removed the logic for extracting spec objects from find_gem_files method as it became unnecessary. This logic was primarily used to fetch spec objects for gems not indexed by Gem.loaded_specs and to avoid paths that Bundler's specs do not recognize. The responsibility for this logic has now been centralized in the build_exe method.
  • Loading branch information
shinokaro committed Jun 21, 2024
1 parent eb6d28d commit d160f49
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,6 @@ EOF
gem_files = []

gems.each do |gemname, spec|
if File.exist?(spec.spec_file) then
@gemspecs << Pathname(spec.spec_file)
else
spec_name = File.basename(spec.spec_file)
spec_path = File.dirname(spec.spec_file)
default_spec_file = spec_path + "/default/" + spec_name
if File.exist?(default_spec_file) then
@gemspecs << Pathname(default_spec_file)
Ocran.msg "Using default specification #{default_spec_file} for gem #{spec.full_name}"
end
end

# In an environment executed from `bundle exec`, `bundler`'s `spec.gem_dir`
# returns a path that does not exist.
unless File.directory?(spec.gem_dir)
Expand Down Expand Up @@ -485,7 +473,8 @@ EOF
gem_files = []
end
features_from_gems -= gem_files
return gem_files, features_from_gems
gemspecs = gems.values
return gem_files, features_from_gems, gemspecs
end

# For RubyInstaller environments supporting Ruby 2.4 and above,
Expand Down Expand Up @@ -515,16 +504,6 @@ EOF
# rubygems/core_ext/kernel_require.rb is evaled and thus missing in $LOADED_FEATURES, so we can't find it and need to add it manually
features.push(Pathname("rubygems/core_ext/kernel_require.rb"))

# Find gemspecs to include
if defined?(Gem)
# Since Bundler is integrated into RubyGems from Ruby 3.2 onwards,
# Bundler's loaded_from points to the root directory of the bundler gem.
# Here, we are only collecting gemspecs files.
@gemspecs = Gem.loaded_specs.map { |name, info| Pathname(info.loaded_from) }.reject(&:directory?)
else
@gemspecs = []
end

# The `RefinePathname` module is prepended to the `Pathname` class. This is done
# after the user script has finished executing and only the Ocran code is running,
# to avoid affecting the script environment.
Expand All @@ -537,7 +516,7 @@ EOF
Ocran.extend HostConfigHelper

# Find gems files and remove them from features
gem_files, features_from_gems = find_gem_files(features)
gem_files, features_from_gems, gemspecs = find_gem_files(features)
features -= features_from_gems

# Include encoding support files
Expand Down Expand Up @@ -594,14 +573,24 @@ EOF
builder.copy_to_bin(bindir / dll, dll)
end

# Add gemspec files
@gemspecs.uniq.each do |gemspec|
if gemspec.subpath?(exec_prefix)
builder.duplicate_to_exec_prefix(gemspec)
elsif (gem_path = GemSpecQueryable.find_gem_path(gemspec))
builder.duplicate_to_gem_home(gemspec, gem_path)
else
Ocran.fatal_error "Gem spec #{gemspec} does not exist in the Ruby installation. Don't know where to put it."
# Find gemspecs to include
if defined?(Gem)
# Add gemspec files
gemspecs.each do |spec|
spec_file = Pathname(spec.loaded_from)

# Since Bundler is integrated into RubyGems from Ruby 3.2 onwards,
# Bundler's loaded_from points to the root directory of the bundler gem.
# Here, we are only collecting gemspecs files.
next if spec_file.directory?

if spec_file.subpath?(exec_prefix)
builder.duplicate_to_exec_prefix(spec_file)
elsif (gem_path = GemSpecQueryable.find_gem_path(spec_file))
builder.duplicate_to_gem_home(spec_file, gem_path)
else
Ocran.fatal_error "Gem spec #{spec_file} does not exist in the Ruby installation. Don't know where to put it."
end
end
end

Expand Down

0 comments on commit d160f49

Please sign in to comment.