rails / thor

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

Ability to have `gsub_file` error if it didn't gsub anything #874

Open G-Rath opened 4 months ago

G-Rath commented 4 months ago

I recently discovered that gsub_file does not error when it does not match; this feels like a bit of a footgun to me.

I've worked around this using this utility function to ensure the file contents has changed:

def gsub_file!(path, flag, *args, &block)
  content = File.binread(path)

  gsub_file(path, flag, *args, &block)

  raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
end

However, I think it would be a good addition to thor itself - I'm thinking maybe gsub_file "", "", "", error_on_no_change: true and then an alias of gsub_file!?