Skip to content

Commit

Permalink
Replace regex with Bundler::LockfileParser
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Mar 4, 2024
1 parent 9bfda44 commit 3eaa47d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12180](https://github.com/rubocop/rubocop/pull/12180): Replace regex with `Bundler::LockfileParser`. ([@amomchilov][])
26 changes: 16 additions & 10 deletions lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,22 @@ def target_rails_version_from_bundler_lock_file

# @return [Float, nil] The Rails version as a `major.minor` Float.
def read_rails_version_from_bundler_lock_file
lock_file_path = bundler_lock_file_path
return nil unless lock_file_path

File.foreach(lock_file_path) do |line|
# If Rails (or one of its frameworks) is in Gemfile.lock or gems.lock, there should be
# a line like:
# railties (X.X.X)
result = line.match(/^\s+railties\s+\((\d+\.\d+)/)
return result.captures.first.to_f if result
end
return nil unless gem_versions_in_target

# Look for `railties` instead of `rails`, to support apps that only use a subset of `rails`
# See https://github.com/rubocop/rubocop/pull/11289
rails_version_in_target = gem_versions_in_target['railties']
return nil unless rails_version_in_target

gem_version_to_major_minor_float(rails_version_in_target)
end

# @param [Gem::Version] gem_version an object like `Gem::Version.new("7.1.2.3")`
# @return [Float] The major and minor version, like `7.1`
def gem_version_to_major_minor_float(gem_version)
segments = gem_version.canonical_segments
# segments.fetch(0).to_f + (segments.fetch(1, 0.0).to_f / 10)
Float("#{segments.fetch(0)}.#{segments.fetch(1, 0)}")
end

# @returns [Hash{String => Gem::Version}] The locked gem versions, keyed by the gems' names.
Expand Down

0 comments on commit 3eaa47d

Please sign in to comment.