txt_file_mutator

Kristian Mandrup, [email protected]

Contains utility functions for doing simple file mutations/transformations such as:

  • Inserting content BEFORE a line with some specific content (before-marker) Has option (default=true) to only insert if no pre-existing same content before the before-marker (to disallow multiple executions to have stacking effects!)

    • insert_before [path]

    TextFileMutator.insert_after [path], [find-txt], [insert txt]

    TextFileMutator.comment_line ‘my/path/file.txt’, ‘# before this’, ‘insert this!’

  • Inserting content AFTER a line with some specific content (functions same as insert_before)

    • insert_after

    TextFileMutator.insert_after [path], [find-txt], [insert txt]

    TextFileMutator.comment_line ‘my/path/file.txt’, ‘# after this’, ‘insert this!’

  • Commenting a line with some specific content

    • comment_line

    TextFileMutator.comment_line [path], [txt]

    TextFileMutator.comment_line ‘my/path/file.txt’, ‘comment this’

  • Comment a config.gem statement for a specific gem

    • comment_config_gem

    TextFileMutator.comment_config_gem [path], [txt]

    TextFileMutator.comment_config_gem ‘my/path/file.txt’, ‘my-gem’

  • Comment a gem ‘my-gem’ statement

    • comment_rails_3_config_gem

  • Remove a line

    • remove_line

    TextFileMutator.remove_line [file_path], [txt]

    TextFileMutator.remove_line ‘my/path/file.txt’, ‘# remove this line’

  • Remove a require statement

    • remove_require

    TextFileMutator.remove_require [file_path], [require string]

    TextFileMutator.remove_require ‘my/path/file.txt’, ‘rails/generators’

  • Replace line TextFileMutator.replace_line [file_path], [old-string] [replace-string]

    Replaces each old-string found with replace-string

  • Remove duplicate lines TextFileMutator.remove_duplicate_lines [file_path]

    TextFileMutator.remove_duplicate_lines ‘my/path/file.txt’

  • Append line TextFileMutator.append_line [file_path], [content]

    TextFileMutator.append_line ‘/my/path/file.txt’, “this line…”

Investigation functions

  • has_content? TextFileMutator.has_content? file_content, content

    # returns true if ‘blip’ occurs at least once in the string TextFileMutator.has_content? file(‘my_file.txt).read, ’blip’

  • has_content_before?

    TextFileMutator.has_content_before? file_content, content, before

    # returns true if ‘blip’ occurs at least once before ‘# before TextFileMutator.has_multiple_content_before? file(’my_file.txt).read, ‘blip’, ‘# before me’ me’

  • has_multiple_content_before?

    TextFileMutator.has_multiple_content_before? file_content, content, before

    # returns true if ‘blip’ occurs at least 2 times before the string ‘# before’ TextFileMutator.has_multiple_content_before? file(‘my_file.txt).read, ’blip’, ‘# before me’ me’

  • has_content_after? ‘Reverse’ of has_content_before?

  • has_multiple_content_after? ‘Reverse’ of has_multiple_content_before?

(more to come in later versions…!)