Class: Chef::TidySubstitutions

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/tidy_substitutions.rb

Defined Under Namespace

Classes: NoSubstitutionFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil, tidy_common) ⇒ TidySubstitutions

Returns a new instance of TidySubstitutions.



12
13
14
15
16
# File 'lib/chef/tidy_substitutions.rb', line 12

def initialize(file_path = nil, tidy_common)
  @file_path = file_path
  @tidy = tidy_common
  @backup_path = tidy_common.backup_path
end

Instance Attribute Details

#backup_pathObject

Returns the value of attribute backup_path.



10
11
12
# File 'lib/chef/tidy_substitutions.rb', line 10

def backup_path
  @backup_path
end

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/chef/tidy_substitutions.rb', line 10

def data
  @data
end

#file_pathObject

Returns the value of attribute file_path.



10
11
12
# File 'lib/chef/tidy_substitutions.rb', line 10

def file_path
  @file_path
end

Instance Method Details

#boiler_plateObject



25
26
27
28
29
# File 'lib/chef/tidy_substitutions.rb', line 25

def boiler_plate
  bp = ::File.join(File.dirname(__FILE__), "../../conf/substitutions.json.example")
  @tidy.ui.stdout.puts "INFO: Creating boiler plate gsub file: 'substitutions.json'"
  FileUtils.cp(bp, ::File.join(Dir.pwd, "substitutions.json"))
end

#load_dataObject



18
19
20
21
22
23
# File 'lib/chef/tidy_substitutions.rb', line 18

def load_data
  @tidy.ui.stdout.puts "INFO: Loading substitutions from #{file_path}"
  @data = @tidy.json_file_to_hash(@file_path, symbolize_names: false)
rescue Errno::ENOENT
  raise NoSubstitutionFile, file_path
end

#revertObject



31
# File 'lib/chef/tidy_substitutions.rb', line 31

def revert; end

#run_substitutionsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/tidy_substitutions.rb', line 55

def run_substitutions
  load_data
  @data.keys.each do |entry|
    @data[entry].keys.each do |glob|
      @tidy.ui.stdout.puts "INFO: Running substitutions for #{entry} -> #{glob}"
      Dir[::File.join(@backup_path, glob)].each do |file|
        @data[entry][glob].each do |substitution|
          search = Regexp.new(substitution["pattern"])
          replace = substitution["replace"].dup
          replace.gsub!(/\!COOKBOOK_VERSION\!/) { |_m| "'" + @tidy.cookbook_version_from_path(file) + "'" }
          sub_in_file(file, search, replace)
        end
      end
    end
  end
end

#sub_in_file(path, search, replace) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/tidy_substitutions.rb', line 33

def sub_in_file(path, search, replace)
  temp_file = Tempfile.new("tidy")
  begin
    File.open(path, "r") do |file|
      file.each_line do |line|
        if line.match(search)
          temp_file.puts replace
          @tidy.ui.stdout.puts "INFO:  ++ #{path}"
        else
          temp_file.puts line
        end
      end
    end
    temp_file.close
    FileUtils.cp(path, "#{path}.orig") unless ::File.exist?("#{path}.orig")
    FileUtils.mv(temp_file.path, path)
  ensure
    temp_file.close
    temp_file.unlink
  end
end