Add match: / match_first: kwargs to file.append and new file.before command#132
Draft
schneems wants to merge 6 commits into
Draft
Add match: / match_first: kwargs to file.append and new file.before command#132schneems wants to merge 6 commits into
schneems wants to merge 6 commits into
Conversation
1096f8a to
2b27d51
Compare
Locating the insertion point by line number (file.append Gemfile#5) is
brittle -- the number drifts when the target file changes. These kwargs
locate the line by matching its content instead:
:::>> file.append("Gemfile", match: "gem 'rails'")
gem 'pg'
match: raises if the target is not unique (safety check).
match_first: uses the first occurrence when duplicates are expected.
New command that inserts content before a matched line. Supports the
same match: / match_first: kwargs as file.append:
:::>> file.before("index.js", match: "// Cleanup after server close")
pool.end().then(() => { console.log('PG pool closed') })
2b27d51 to
d907684
Compare
Pure class methods with explicit arguments -- no ivars, no implicit state. resolve_match_line returns a 1-indexed line number from a match string. insert_contents_at_line handles insertion at any line including after the last line.
Both file.append and file.before now share one Args class that handles filename#line_number parsing, match:/match_first: validation, and mutual exclusivity checks. file.before now supports bare (prepend to head) and #line_number modes in addition to match.
Both AppendRunner and BeforeRunner now delegate to FileUtil.resolve_match_line and FileUtil.insert_contents_at_line instead of duplicating match resolution and insertion logic. Fixes file.append + match: to insert AFTER the matched line (was inserting before, same as file.before). "append" now means append.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Appending content at a specific line number (
file.append Gemfile#5) breakswhen the target file changes unexpectedly -- the line number drifts and content
lands in the wrong place. This happened in practice with the Node.js Getting
Started tutorial (see heroku/nodejs-getting-started#403).
This adds
match:andmatch_first:kwargs so the insertion point is locatedby matching a placeholder comment or code pattern instead of a line number:
match:raises if the target string is not unique in the file (safety check).match_first:uses the first occurrence when duplicates are expected.Also adds a new
file.beforecommand with the same kwargs.Closes #121