Skip to content

Commit

Permalink
Refactor file selection logic in Ocran.find_gem_files method
Browse files Browse the repository at this point in the history
- Simplify file selection logic using the `map` method for better readability and maintainability.
- Ensure `gem_root_files` is initialized only when necessary.
- Use `path.extname` for checking file extensions in the `:scripts` case, improving clarity.
- Improve error handling by providing specific messages for invalid file sets.
  • Loading branch information
shinokaro committed Jun 1, 2024
1 parent d3370de commit 9ae37d6
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -537,26 +537,29 @@ EOF
else
build_complete = nil
end
gem_root_files = nil
files = []

# Find the selected files
include.each do |set|
gem_root_files = unless (include & [:files, :extras, :scripts]).empty?
gem_root.find.select(&:file?)
end
files = include.map do |set|
case set
when :spec
files << spec.files.map { |file| Pathname(file) }
spec.files.map { |file| Pathname(file) }
when :loaded
files << features_from_gems.select { |feature| feature.subpath?(gem_root) }
features_from_gems.select { |feature| feature.subpath?(gem_root) }
when :files
gem_root_files ||= gem_root.find.select(&:file?)
files << gem_root_files.select { |path| path.relative_path_from(gem_root).to_posix !~ GEM_NON_FILE_RE }
files << build_complete if build_complete
selected_files = gem_root_files.select { |path| path.relative_path_from(gem_root).to_posix !~ GEM_NON_FILE_RE }
if build_complete
selected_files << build_complete
end
selected_files
when :extras
gem_root_files ||= gem_root.find.select(&:file?)
files << gem_root_files.select { |path| path.relative_path_from(gem_root).to_posix =~ GEM_EXTRA_RE }
gem_root_files.select { |path| path.relative_path_from(gem_root).to_posix =~ GEM_EXTRA_RE }
when :scripts
gem_root_files ||= gem_root.find.select(&:file?)
files << gem_root_files.select { |path| path.relative_path_from(gem_root).to_posix =~ GEM_SCRIPT_RE }
gem_root_files.select { |path| path.extname =~ GEM_SCRIPT_RE }
else
raise "Invalid file set: #{set}. Please specify a valid file set (:spec, :loaded, :files, :extras, :scripts)."
end
end

Expand Down

0 comments on commit 9ae37d6

Please sign in to comment.