class Chef::TidySubstitutions
Attributes
backup_path[RW]
data[RW]
file_path[RW]
Public Class Methods
new(file_path = nil, tidy_common)
click to toggle source
# 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
Public Instance Methods
boiler_plate()
click to toggle source
# 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_data()
click to toggle source
# 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
revert()
click to toggle source
# File lib/chef/tidy_substitutions.rb, line 31 def revert; end
run_substitutions()
click to toggle source
# 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)
click to toggle source
# 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