Class: Bundler::Audit::Fix::Patcher

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/audit/fix/patcher.rb

Overview

Patcher is a class for updating gem version specifications in Gemfile.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, report, gemfile_lock = 'Gemfile.lock', config_file_path = '.bundler-audit.yml') ⇒ Patcher

Returns a new instance of Patcher.

Parameters:

  • root (String)

    The path to the project root.

  • report (Hash)

    Result of ::Bundler::Audit::Scanner#report.

  • gemfile_lock (String) (defaults to: 'Gemfile.lock')

    Path to Gemfile.lock.

  • config_file_path (String) (defaults to: '.bundler-audit.yml')

    Path to bundler-audit config file.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bundler/audit/fix/patcher.rb', line 43

def initialize(root, report, gemfile_lock = 'Gemfile.lock', config_file_path = '.bundler-audit.yml')
  root           = File.expand_path(root)
  gemfile        = gemfile_lock.sub(/\.lock$/, '')
  @gemfile_path  = File.join(root, gemfile)
  @lockfile_path = File.join(root, gemfile_lock)
  @report        = report

  unless File.file?(@gemfile_path)
    raise(Bundler::GemfileNotFound, "Could not find #{gemfile.inspect} in #{root.inspect}")
  end

  unless File.file?(@lockfile_path)
    raise(Bundler::GemfileLockNotFound, "Could not find #{gemfile_lock.inspect} in #{root.inspect}")
  end

  @bundled_gems = Bundler::Definition.build(@gemfile_path, nil, nil).dependencies
  @locked_gems = Bundler::LockfileParser.new(Bundler.read_file(@lockfile_path)).specs

  config_file_abs_path = File.absolute_path(config_file_path, root)
  @config = if File.exist?(config_file_abs_path)
              Configuration.load(config_file_abs_path)
            else
              Configuration.new
            end
end

Instance Attribute Details

#bundled_gemsObject (readonly)

Returns the value of attribute bundled_gems.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def bundled_gems
  @bundled_gems
end

#configObject (readonly)

Returns the value of attribute config.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def config
  @config
end

#gemfile_pathObject (readonly)

Returns the value of attribute gemfile_path.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def gemfile_path
  @gemfile_path
end

#locked_gemsObject (readonly)

Returns the value of attribute locked_gems.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def locked_gems
  @locked_gems
end

#lockfile_pathObject (readonly)

Returns the value of attribute lockfile_path.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def lockfile_path
  @lockfile_path
end

#reportObject (readonly)

Returns the value of attribute report.



30
31
32
# File 'lib/bundler/audit/fix/patcher.rb', line 30

def report
  @report
end

Instance Method Details

#patchObject

Write patched versions to Gemfile and return gems list to update.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bundler/audit/fix/patcher.rb', line 72

def patch
  patterns, gems_to_update = build_patterns
  gemfile = File.read(gemfile_path, encoding: 'utf-8')

  patterns.each do |pattern, replace_with|
    gemfile = gemfile.gsub(pattern, replace_with)
  end

  File.write(gemfile_path, gemfile)

  gems_to_update
end