Class: RailsEngineToolkit::FileEditor
- Inherits:
-
Object
- Object
- RailsEngineToolkit::FileEditor
- Defined in:
- lib/rails_engine_toolkit/file_editor.rb
Class Method Summary collapse
- .copy_file(path_from, path_to) ⇒ Object
- .ensure_line_after_draw_block(path, line) ⇒ Object
- .insert_after_first_match(path, match_regex, text) ⇒ Object
- .remove_lines_matching(path, regex) ⇒ Object
Class Method Details
.copy_file(path_from, path_to) ⇒ Object
35 36 37 38 |
# File 'lib/rails_engine_toolkit/file_editor.rb', line 35 def self.copy_file(path_from, path_to) FileUtils.mkdir_p(path_to.dirname) FileUtils.cp(path_from, path_to) end |
.ensure_line_after_draw_block(path, line) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rails_engine_toolkit/file_editor.rb', line 23 def self.ensure_line_after_draw_block(path, line) return false unless path.exist? content = path.read return false if content.include?(line.strip) updated = content.sub(/Rails\.application\.routes\.draw do\s*\n/, "Rails.application.routes.draw do\n#{line}") changed = updated != content path.write(updated) if changed changed end |
.insert_after_first_match(path, match_regex, text) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rails_engine_toolkit/file_editor.rb', line 15 def self.insert_after_first_match(path, match_regex, text) content = path.read updated = content.sub(match_regex) { |m| "#{m}#{text}" } changed = updated != content path.write(updated) if changed changed end |
.remove_lines_matching(path, regex) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/rails_engine_toolkit/file_editor.rb', line 5 def self.remove_lines_matching(path, regex) return false unless path.exist? original = path.read updated = original.lines.grep_v(regex).join changed = original != updated path.write(updated) if changed changed end |