Class: Chef::Util::FileEdit
- Inherits:
-
Object
- Object
- Chef::Util::FileEdit
- Defined in:
- lib/chef/util/file_edit.rb
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.
32 33 34 35 36 37 38 |
# File 'lib/chef/util/file_edit.rb', line 32 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
66 67 68 |
# File 'lib/chef/util/file_edit.rb', line 66 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
60 61 62 |
# File 'lib/chef/util/file_edit.rb', line 60 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
54 55 56 |
# File 'lib/chef/util/file_edit.rb', line 54 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
48 49 50 |
# File 'lib/chef/util/file_edit.rb', line 48 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.
42 43 44 |
# File 'lib/chef/util/file_edit.rb', line 42 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)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chef/util/file_edit.rb', line 71 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" File.copy(original_pathname, backup_pathname) Tempfile.open("w") do |newfile| contents.each do |line| newfile.puts(line) end newfile.flush FileUtils.mv(newfile.path, original_pathname) end end self.file_edited = false end |