diff --git a/lib/thor/actions/file_manipulation.rb b/lib/thor/actions/file_manipulation.rb index 0bccba41..88ef97be 100644 --- a/lib/thor/actions/file_manipulation.rb +++ b/lib/thor/actions/file_manipulation.rb @@ -264,9 +264,12 @@ def inject_into_module(path, module_name, *args, &block) def gsub_file!(path, flag, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} - config[:error_on_no_change] = true + 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) - gsub_file(path, flag, *args, config, &block) + actually_gsub_file(path, flag, args, true, &block) unless options[:pretend] end # Run a regular expression replacement on a file. @@ -275,8 +278,7 @@ def gsub_file!(path, flag, *args, &block) # path:: path of the file to be changed # flag:: the regexp or string to be replaced # replacement:: the replacement, can be also given as a block - # config:: give :verbose => false to not log the status, - # :error_on_no_change => true to raise an error if the file does not change, and + # config:: give :verbose => false to not log the status, and # :force => true, to force the replacement regardless of runner behavior. # # ==== Example @@ -295,16 +297,7 @@ def gsub_file(path, flag, *args, &block) path = File.expand_path(path, destination_root) say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true) - unless options[:pretend] - content = File.binread(path) - success = content.gsub!(flag, *args, &block) - - if success.nil? && config.fetch(:error_on_no_change, false) - raise Thor::Error, "The content of #{path} did not change" - end - - File.open(path, "wb") { |file| file.write(content) } - end + actually_gsub_file(path, flag, args, false, &block) unless options[:pretend] end # Uncomment all lines matching a given regex. It will leave the space @@ -391,6 +384,17 @@ def with_output_buffer(buf = "".dup) #:nodoc: self.output_buffer = old_buffer end + def actually_gsub_file(path, flag, args, error_on_no_change, &block) + content = File.binread(path) + success = content.gsub!(flag, *args, &block) + + if success.nil? && error_on_no_change + raise Thor::Error, "The content of #{path} did not change" + end + + File.open(path, "wb") { |file| file.write(content) } + end + # Thor::Actions#capture depends on what kind of buffer is used in ERB. # Thus CapturableERB fixes ERB to use String buffer. class CapturableERB < ERB diff --git a/spec/actions/file_manipulation_spec.rb b/spec/actions/file_manipulation_spec.rb index 8f647821..1333b994 100644 --- a/spec/actions/file_manipulation_spec.rb +++ b/spec/actions/file_manipulation_spec.rb @@ -421,21 +421,6 @@ def file action :gsub_file, "doc/README", "___start___", "START" expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n") end - - context "with error_on_no_change" do - it "replaces the content in the file" do - action :gsub_file, "doc/README", "__start__", "START", error_on_no_change: true - expect(File.binread(file)).to eq("START\nREADME\n__end__\n") - end - - it "raises if the file contents did not change" do - expect do - action :gsub_file, "doc/README", "___start___", "START", error_on_no_change: true - end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") - - expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n") - end - end end context "with revoke behavior" do