Class: Chef::Util::FileEdit
Instance Method Summary collapse
-
#initialize(filepath) ⇒ FileEdit
constructor
A new instance of FileEdit.
-
#insert_line_after_match(regex, newline) ⇒ Object
search the file line by line and match each line with the given regex if matched, insert newline after each matching line.
-
#search_file_delete(regex) ⇒ Object
search the file line by line and match each line with the given regex if matched, delete the match (all occurances) from the line.
-
#search_file_delete_line(regex) ⇒ Object
search the file line by line and match each line with the given regex if matched, delete the line.
-
#search_file_replace(regex, replace) ⇒ Object
search the file line by line and match each line with the given regex if matched, replace the match (all occurances) with the replace parameter.
-
#search_file_replace_line(regex, newline) ⇒ Object
search the file line by line and match each line with the given regex if matched, replace the whole line with newline.
-
#write_file ⇒ Object
Make a copy of old_file and write new file out (only if file changed).
Constructor Details
#initialize(filepath) ⇒ FileEdit
Returns a new instance of FileEdit.
31 32 33 34 35 36 37 |
# File 'lib/chef/util/file_edit.rb', line 31 def initialize(filepath) @original_pathname = filepath @file_edited = false raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0 end |
Instance Method Details
#insert_line_after_match(regex, newline) ⇒ Object
search the file line by line and match each line with the given regex if matched, insert newline after each matching line
65 66 67 |
# File 'lib/chef/util/file_edit.rb', line 65 def insert_line_after_match(regex, newline) search_match(regex, newline, 'i', 0) end |
#search_file_delete(regex) ⇒ Object
search the file line by line and match each line with the given regex if matched, delete the match (all occurances) from the line
59 60 61 |
# File 'lib/chef/util/file_edit.rb', line 59 def search_file_delete(regex) search_match(regex, " ", 'd', 2) end |
#search_file_delete_line(regex) ⇒ Object
search the file line by line and match each line with the given regex if matched, delete the line
53 54 55 |
# File 'lib/chef/util/file_edit.rb', line 53 def search_file_delete_line(regex) search_match(regex, " ", 'd', 1) end |
#search_file_replace(regex, replace) ⇒ Object
search the file line by line and match each line with the given regex if matched, replace the match (all occurances) with the replace parameter
47 48 49 |
# File 'lib/chef/util/file_edit.rb', line 47 def search_file_replace(regex, replace) search_match(regex, replace, 'r', 2) end |
#search_file_replace_line(regex, newline) ⇒ Object
search the file line by line and match each line with the given regex if matched, replace the whole line with newline.
41 42 43 |
# File 'lib/chef/util/file_edit.rb', line 41 def search_file_replace_line(regex, newline) search_match(regex, newline, 'r', 1) end |
#write_file ⇒ Object
Make a copy of old_file and write new file out (only if file changed)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/util/file_edit.rb', line 70 def write_file # file_edited is false when there was no match in the whole file and thus no contents have changed. if file_edited backup_pathname = original_pathname + ".old" FileUtils.cp(original_pathname, backup_pathname, :preserve => true) File.open(original_pathname, "w") do |newfile| contents.each do |line| newfile.puts(line) end newfile.flush end end self.file_edited = false end |