Method: Thor::Actions#gsub_file

Defined in:
lib/thor/actions/file_manipulation.rb

#gsub_file(path, flag, *args, &block) ⇒ Object

Run a regular expression replacement on a file.

Parameters

path<String>

path of the file to be changed

flag<Regexp|String>

the regexp or string to be replaced

replacement<String>

the replacement, can be also given as a block

config<Hash>

give :verbose => false to not log the status, and :force => true, to force the replacement regardless of runner behavior.

Example

gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'

gsub_file 'README', /rake/, :green do |match|
  match << " no more. Use thor!"
end


291
292
293
294
295
296
297
298
299
300
# File 'lib/thor/actions/file_manipulation.rb', line 291

def gsub_file(path, flag, *args, &block)
  config = args.last.is_a?(Hash) ? args.pop : {}

  return unless behavior == :invoke || config.fetch(:force, false)

  path = File.expand_path(path, destination_root)
  say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)

  actually_gsub_file(path, flag, args, false, &block) unless options[:pretend]
end