Class: Bundle::Patch::GemfileUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/bundle/patch/gemfile_updater.rb

Class Method Summary collapse

Class Method Details

.update(gemfile_path:, advisories:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bundle/patch/gemfile_updater.rb', line 6

def self.update(gemfile_path:, advisories:)
  contents = File.read(gemfile_path)
  updated = false

  advisories.each do |adv|
    name = adv["name"]
    min_safe_version = adv["required_version"]
    next unless min_safe_version

    # This regex matches lines like: gem 'somegem', '1.2.3'
    regex = /^(\s*gem\s+["']#{Regexp.escape(name)}["']\s*,\s*)["'][^"']*["'](.*)$/

    contents.gsub!(regex) do
      updated = true
      "#{$1}\"#{min_safe_version}\"#{$2}"
    end
  end

  if updated
    File.write(gemfile_path, contents)
    puts "📝 Updated Gemfile with patched versions"
  else
    puts "✅ No existing Gemfile entries needed updating"
  end
end