class Bundler::Patch::UpdateSpec

Attributes

patched_versions[RW]
regexes[RW]
target_dir[RW]
target_file[RW]

Public Class Methods

new(target_file: '', target_dir: Dir.pwd, regexes: [/.*/], patched_versions: []) click to toggle source
# File lib/bundler/patch/updater.rb, line 5
def initialize(target_file: '',
               target_dir: Dir.pwd,
               regexes: [/.*/],
               patched_versions: [])
  @target_file = target_file
  @target_dir = target_dir
  @regexes = regexes
  @patched_versions = patched_versions
end

Public Instance Methods

calc_new_version(old_version) click to toggle source
# File lib/bundler/patch/updater.rb, line 19
def calc_new_version(old_version)
  old = old_version
  all = @patched_versions.dup
  return old_version if all.include?(old)

  all << old
  all.sort!
  all.delete_if { |v| v.split(/\./).first != old.split(/\./).first } # strip non-matching major revs
  all[all.index(old) + 1]
end
file_replace() { |match, re| ... } click to toggle source
# File lib/bundler/patch/updater.rb, line 30
def file_replace
  filename = target_path_fn
  unless File.exist?(filename)
    puts "Cannot find #{filename}"
    return
  end

  guts = File.read(filename)
  any_changes = false
  [@regexes].flatten.each do |re|
    any_changes = guts.gsub!(re) do |match|
      if block_given?
        yield match, re
      else
        update_to_new_version(match, re)
      end
    end || any_changes
  end

  if any_changes
    File.open(filename, 'w') { |f| f.print guts }
    verbose_puts "Updated #{filename}"
  else
    verbose_puts "No changes for #{filename}"
  end
end
Also aliased as: update
target_path_fn() click to toggle source
# File lib/bundler/patch/updater.rb, line 15
def target_path_fn
  File.expand_path(File.join(@target_dir, @target_file))
end
update()
Alias for: file_replace
update_to_new_version(match, re) click to toggle source
# File lib/bundler/patch/updater.rb, line 57
def update_to_new_version(match, re)
  current_version = match.scan(re).join
  new_version = calc_new_version(current_version)
  if new_version
    match.sub(current_version, new_version).tap { |s| puts "Updating to #{s}" }
  else
    match
  end
end
verbose_puts(text) click to toggle source
# File lib/bundler/patch/updater.rb, line 69
def verbose_puts(text)
  puts text if @verbose
end