file_edit.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. # Author:: Nuo Yan (<nuo@opscode.com>)
  3. # Copyright:: Copyright (c) 2009 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. class Chef
  18. class Util
  19. class FileEdit
  20. private
  21. attr_accessor :original_pathname, :contents, :file_edited
  22. public
  23. def initialize(filepath)
  24. @original_pathname = filepath
  25. @file_edited = false
  26. raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname
  27. raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0
  28. end
  29. #search the file line by line and match each line with the given regex
  30. #if matched, replace the whole line with newline.
  31. def search_file_replace_line(regex, newline)
  32. search_match(regex, newline, 'r', 1)
  33. end
  34. #search the file line by line and match each line with the given regex
  35. #if matched, replace the match (all occurances) with the replace parameter
  36. def search_file_replace(regex, replace)
  37. search_match(regex, replace, 'r', 2)
  38. end
  39. #search the file line by line and match each line with the given regex
  40. #if matched, delete the line
  41. def search_file_delete_line(regex)
  42. search_match(regex, " ", 'd', 1)
  43. end
  44. #search the file line by line and match each line with the given regex
  45. #if matched, delete the match (all occurances) from the line
  46. def search_file_delete(regex)
  47. search_match(regex, " ", 'd', 2)
  48. end
  49. #search the file line by line and match each line with the given regex
  50. #if matched, insert newline after each matching line
  51. def insert_line_after_match(regex, newline)
  52. search_match(regex, newline, 'i', 1)
  53. end
  54. #search the file line by line and match each line with the given regex
  55. #if not matched, insert newline at the end of the file
  56. def insert_line_if_no_match(regex, newline)
  57. search_match(regex, newline, 'i', 2)
  58. end
  59. #Make a copy of old_file and write new file out (only if file changed)
  60. def write_file
  61. # file_edited is false when there was no match in the whole file and thus no contents have changed.
  62. if file_edited
  63. backup_pathname = original_pathname + ".old"
  64. FileUtils.cp(original_pathname, backup_pathname, :preserve => true)
  65. File.open(original_pathname, "w") do |newfile|
  66. contents.each do |line|
  67. newfile.puts(line)
  68. end
  69. newfile.flush
  70. end
  71. end
  72. self.file_edited = false
  73. end
  74. private
  75. #helper method to do the match, replace, delete, and insert operations
  76. #command is the switch of delete, replace, and insert ('d', 'r', 'i')
  77. #method is to control operation on whole line or only the match (1 for line, 2 for match)
  78. def search_match(regex, replace, command, method)
  79. #convert regex to a Regexp object (if not already is one) and store it in exp.
  80. exp = Regexp.new(regex)
  81. #loop through contents and do the appropriate operation depending on 'command' and 'method'
  82. new_contents = []
  83. contents.each do |line|
  84. if line.match(exp)
  85. self.file_edited = true
  86. case
  87. when command == 'r'
  88. new_contents << ((method == 1) ? replace : line.gsub!(exp, replace))
  89. when command == 'd'
  90. if method == 2
  91. new_contents << line.gsub!(exp, "")
  92. end
  93. when command == 'i'
  94. new_contents << line
  95. new_contents << replace unless method == 2
  96. end
  97. else
  98. new_contents << line
  99. end
  100. end
  101. if command == 'i' && method == 2 && ! file_edited
  102. new_contents << replace
  103. self.file_edited = true
  104. end
  105. self.contents = new_contents
  106. end
  107. end
  108. end
  109. end