@@ -264,9 +264,12 @@ def inject_into_module(path, module_name, *args, &block)
264
264
def gsub_file! ( path , flag , *args , &block )
265
265
config = args . last . is_a? ( Hash ) ? args . pop : { }
266
266
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 )
268
271
269
- gsub_file ( path , flag , * args , config , &block )
272
+ actually_gsub_file ( path , flag , args , true , &block ) unless options [ :pretend ]
270
273
end
271
274
272
275
# Run a regular expression replacement on a file.
@@ -275,8 +278,7 @@ def gsub_file!(path, flag, *args, &block)
275
278
# path<String>:: path of the file to be changed
276
279
# flag<Regexp|String>:: the regexp or string to be replaced
277
280
# 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
280
282
# :force => true, to force the replacement regardless of runner behavior.
281
283
#
282
284
# ==== Example
@@ -295,16 +297,7 @@ def gsub_file(path, flag, *args, &block)
295
297
path = File . expand_path ( path , destination_root )
296
298
say_status :gsub , relative_to_original_destination_root ( path ) , config . fetch ( :verbose , true )
297
299
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 ]
308
301
end
309
302
310
303
# Uncomment all lines matching a given regex. It will leave the space
@@ -391,6 +384,17 @@ def with_output_buffer(buf = "".dup) #:nodoc:
391
384
self . output_buffer = old_buffer
392
385
end
393
386
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
+
394
398
# Thor::Actions#capture depends on what kind of buffer is used in ERB.
395
399
# Thus CapturableERB fixes ERB to use String buffer.
396
400
class CapturableERB < ERB
0 commit comments