Class: Chef::TidySubstitutions
- Inherits:
-
Object
- Object
- Chef::TidySubstitutions
- Defined in:
- lib/chef/tidy_substitutions.rb
Defined Under Namespace
Classes: NoSubstitutionFile
Instance Attribute Summary collapse
-
#backup_path ⇒ Object
Returns the value of attribute backup_path.
-
#data ⇒ Object
Returns the value of attribute data.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
Instance Method Summary collapse
- #boiler_plate ⇒ Object
-
#initialize(file_path = nil, tidy_common) ⇒ TidySubstitutions
constructor
A new instance of TidySubstitutions.
- #load_data ⇒ Object
- #revert ⇒ Object
- #run_substitutions ⇒ Object
- #sub_in_file(path, search, replace) ⇒ Object
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_path ⇒ Object
Returns the value of attribute backup_path.
10 11 12 |
# File 'lib/chef/tidy_substitutions.rb', line 10 def backup_path @backup_path end |
#data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/chef/tidy_substitutions.rb', line 10 def data @data end |
#file_path ⇒ Object
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_plate ⇒ Object
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_data ⇒ Object
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 |
#revert ⇒ Object
31 |
# File 'lib/chef/tidy_substitutions.rb', line 31 def revert; end |
#run_substitutions ⇒ Object
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 |