rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 553 forks source link

Check for duplicate content in relevant section when inserting into files #735

Closed excid3 closed 3 years ago

excid3 commented 4 years ago

I ran into some unexpected behavior today with insert_into_file. I was trying to insert , null: false into a migration after some text, but nothing happened.

  t.belongs_to :recipient, null: false
  t.string :type
insert_into_file path, ", null: false", after: "t.string :type"

This does nothing because , null: false exists in the file (anywhere).

Using after: I would expect insert_into_file to only care if the string exists after the matching line, not before it.

This PR would change the behavior to only look through the before or after portions of the file content to make it more intuitive.

rafaelfranca commented 2 years ago

I'll have to revert this PR since it changes the behavior when the regexp contains capture. split returns the capture inside the array of split strings, so we can't know with the current implementation what is the after and before the regexp.