Class: Bundler::Audit::Fix::Patcher
- Inherits:
-
Object
- Object
- Bundler::Audit::Fix::Patcher
- Defined in:
- lib/bundler/audit/fix/patcher.rb
Overview
Patcher is a class for updating gem version specifications in Gemfile.
Instance Attribute Summary collapse
-
#bundled_gems ⇒ Object
readonly
Returns the value of attribute bundled_gems.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#gemfile_path ⇒ Object
readonly
Returns the value of attribute gemfile_path.
-
#locked_gems ⇒ Object
readonly
Returns the value of attribute locked_gems.
-
#lockfile_path ⇒ Object
readonly
Returns the value of attribute lockfile_path.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Instance Method Summary collapse
-
#initialize(root, report, gemfile_lock = 'Gemfile.lock', config_file_path = '.bundler-audit.yml') ⇒ Patcher
constructor
A new instance of Patcher.
-
#patch ⇒ Object
Write patched versions to Gemfile and return gems list to update.
Constructor Details
#initialize(root, report, gemfile_lock = 'Gemfile.lock', config_file_path = '.bundler-audit.yml') ⇒ Patcher
Returns a new instance of Patcher.
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.(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_gems ⇒ Object (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 |
#config ⇒ Object (readonly)
Returns the value of attribute config.
30 31 32 |
# File 'lib/bundler/audit/fix/patcher.rb', line 30 def config @config end |
#gemfile_path ⇒ Object (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_gems ⇒ Object (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_path ⇒ Object (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 |
#report ⇒ Object (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
#patch ⇒ Object
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 |