@@ -263,9 +263,12 @@ def inject_into_module(path, module_name, *args, &block)
263263 def gsub_file! ( path , flag , *args , &block )
264264 config = args . last . is_a? ( Hash ) ? args . pop : { }
265265
266- config [ :error_on_no_change ] = true
266+ return unless behavior == :invoke || config . fetch ( :force , false )
267+
268+ path = File . expand_path ( path , destination_root )
269+ say_status :gsub , relative_to_original_destination_root ( path ) , config . fetch ( :verbose , true )
267270
268- gsub_file ( path , flag , * args , config , &block )
271+ actually_gsub_file ( path , flag , args , true , &block ) unless options [ :pretend ]
269272 end
270273
271274 # Run a regular expression replacement on a file.
@@ -274,8 +277,7 @@ def gsub_file!(path, flag, *args, &block)
274277 # path<String>:: path of the file to be changed
275278 # flag<Regexp|String>:: the regexp or string to be replaced
276279 # replacement<String>:: the replacement, can be also given as a block
277- # config<Hash>:: give :verbose => false to not log the status,
278- # :error_on_no_change => true to raise an error if the file does not change, and
280+ # config<Hash>:: give :verbose => false to not log the status, and
279281 # :force => true, to force the replacement regardless of runner behavior.
280282 #
281283 # ==== Example
@@ -294,16 +296,7 @@ def gsub_file(path, flag, *args, &block)
294296 path = File . expand_path ( path , destination_root )
295297 say_status :gsub , relative_to_original_destination_root ( path ) , config . fetch ( :verbose , true )
296298
297- unless options [ :pretend ]
298- content = File . binread ( path )
299- success = content . gsub! ( flag , *args , &block )
300-
301- if success . nil? && config . fetch ( :error_on_no_change , false )
302- raise Thor ::Error , "The content of #{ path } did not change"
303- end
304-
305- File . open ( path , "wb" ) { |file | file . write ( content ) }
306- end
299+ actually_gsub_file ( path , flag , args , false , &block ) unless options [ :pretend ]
307300 end
308301
309302 # Uncomment all lines matching a given regex. Preserves indentation before
@@ -389,6 +382,17 @@ def with_output_buffer(buf = "".dup) #:nodoc:
389382 self . output_buffer = old_buffer
390383 end
391384
385+ def actually_gsub_file ( path , flag , args , error_on_no_change , &block )
386+ content = File . binread ( path )
387+ success = content . gsub! ( flag , *args , &block )
388+
389+ if success . nil? && error_on_no_change
390+ raise Thor ::Error , "The content of #{ path } did not change"
391+ end
392+
393+ File . open ( path , "wb" ) { |file | file . write ( content ) }
394+ end
395+
392396 # Thor::Actions#capture depends on what kind of buffer is used in ERB.
393397 # Thus CapturableERB fixes ERB to use String buffer.
394398 class CapturableERB < ERB
0 commit comments