Module: PDQTest::Upgrade

Defined in:
lib/pdqtest/upgrade.rb

Constant Summary collapse

GEMFILE =
'Gemfile'
GEM_REGEXP =
/gem ('|")([-\w]+)('|").*$/
GEM_ATTRIB_REGEXP =
/^\s*:\w+/
GEMS =
{
  'pdqtest'         => {
    'line'  => "gem 'pdqtest', '#{PDQTest::VERSION}'",
    'added' => false,
  },
  'puppet-strings'  => {
    'line'  => "gem 'puppet-strings', :git => 'https://github.com/puppetlabs/puppet-strings'",
    'added' => false,
  },
}

Class Method Summary collapse

Class Method Details

.upgradeObject

upgrade a module to the latest version of PDQTest



20
21
22
23
24
25
26
27
28
29
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
56
57
58
59
60
# File 'lib/pdqtest/upgrade.rb', line 20

def self.upgrade()
  t_file = File.open("#{GEMFILE}.tmp","w")
  updating_gem = false
  File.open(GEMFILE, 'r') do |f|
    f.each_line{ |line|
      if line =~ GEM_REGEXP
        # a gem stanza
        processing_gem = $2
        if GEMS.keys.include?(processing_gem)
          # fixup one of our monitored gems as needed, mark
          # this as being a gem that is being updated so
          # that we can kill any multi-line attributes
          t_file.puts GEMS[processing_gem]['line']
          updating_gem = true
          GEMS[processing_gem]['added'] = true
        else
          # a gem we don't care about - write it out as-is
          t_file.puts line
          updating_gem = false
        end
      elsif updating_gem and line =~ GEM_ATTRIB_REGEXP
        # do nothing - remove the multi-line attributes
      else
        # anything else... (esp comments)
        t_file.puts line
      end
    }
  end

  # the code above will only UPGRADE existing gem lines, but if this is our
  # first run, there will be nothing to upgrade, so loop through the GEMS
  # for any that are not already added and append them
  GEMS.each { |name, opts|
    if ! opts['added']
      t_file.puts opts['line']
    end
  }

  t_file.close
  FileUtils.mv(t_file.path, GEMFILE)
end