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