Class: Bundler::Patch::UpdateSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/patch/updater.rb

Direct Known Subclasses

Gemfile, RubyVersion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_file: '', target_dir: Dir.pwd, regexes: [/.*/], patched_versions: []) ⇒ UpdateSpec

Returns a new instance of UpdateSpec.



5
6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#patched_versionsObject

Returns the value of attribute patched_versions.



3
4
5
# File 'lib/bundler/patch/updater.rb', line 3

def patched_versions
  @patched_versions
end

#regexesObject

Returns the value of attribute regexes.



3
4
5
# File 'lib/bundler/patch/updater.rb', line 3

def regexes
  @regexes
end

#target_dirObject

Returns the value of attribute target_dir.



3
4
5
# File 'lib/bundler/patch/updater.rb', line 3

def target_dir
  @target_dir
end

#target_fileObject

Returns the value of attribute target_file.



3
4
5
# File 'lib/bundler/patch/updater.rb', line 3

def target_file
  @target_file
end

Instance Method Details

#calc_new_version(old_version) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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_replaceObject Also known as: update



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#target_path_fnObject



15
16
17
# File 'lib/bundler/patch/updater.rb', line 15

def target_path_fn
  File.expand_path(File.join(@target_dir, @target_file))
end

#update_to_new_version(match, re) ⇒ Object



57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



69
70
71
# File 'lib/bundler/patch/updater.rb', line 69

def verbose_puts(text)
  puts text if @verbose
end