@@ -264,9 +264,12 @@ def inject_into_module(path, module_name, *args, &block)
264264 def gsub_file! ( path , flag , *args , &block )
265265 config = args . last . is_a? ( Hash ) ? args . pop : { }
266266
267- config [ :error_on_no_change ] = true
267+ return unless behavior == :invoke || config . fetch ( :force , false )
268+
269+ path = File . expand_path ( path , destination_root )
270+ say_status :gsub , relative_to_original_destination_root ( path ) , config . fetch ( :verbose , true )
268271
269- gsub_file ( path , flag , * args , config , &block )
272+ actually_gsub_file ( path , flag , args , true , &block ) unless options [ :pretend ]
270273 end
271274
272275 # Run a regular expression replacement on a file.
@@ -275,8 +278,7 @@ def gsub_file!(path, flag, *args, &block)
275278 # path<String>:: path of the file to be changed
276279 # flag<Regexp|String>:: the regexp or string to be replaced
277280 # replacement<String>:: the replacement, can be also given as a block
278- # config<Hash>:: give :verbose => false to not log the status,
279- # :error_on_no_change => true to raise an error if the file does not change, and
281+ # config<Hash>:: give :verbose => false to not log the status, and
280282 # :force => true, to force the replacement regardless of runner behavior.
281283 #
282284 # ==== Example
@@ -295,16 +297,7 @@ def gsub_file(path, flag, *args, &block)
295297 path = File . expand_path ( path , destination_root )
296298 say_status :gsub , relative_to_original_destination_root ( path ) , config . fetch ( :verbose , true )
297299
298- unless options [ :pretend ]
299- content = File . binread ( path )
300- success = content . gsub! ( flag , *args , &block )
301-
302- if success . nil? && config . fetch ( :error_on_no_change , false )
303- raise Thor ::Error , "The content of #{ path } did not change"
304- end
305-
306- File . open ( path , "wb" ) { |file | file . write ( content ) }
307- end
300+ actually_gsub_file ( path , flag , args , false , &block ) unless options [ :pretend ]
308301 end
309302
310303 # Uncomment all lines matching a given regex. It will leave the space
@@ -391,6 +384,17 @@ def with_output_buffer(buf = "".dup) #:nodoc:
391384 self . output_buffer = old_buffer
392385 end
393386
387+ def actually_gsub_file ( path , flag , args , error_on_no_change , &block )
388+ content = File . binread ( path )
389+ success = content . gsub! ( flag , *args , &block )
390+
391+ if success . nil? && error_on_no_change
392+ raise Thor ::Error , "The content of #{ path } did not change"
393+ end
394+
395+ File . open ( path , "wb" ) { |file | file . write ( content ) }
396+ end
397+
394398 # Thor::Actions#capture depends on what kind of buffer is used in ERB.
395399 # Thus CapturableERB fixes ERB to use String buffer.
396400 class CapturableERB < ERB
0 commit comments