diff --git a/lib/thor/actions/inject_into_file.rb b/lib/thor/actions/inject_into_file.rb index 66dd1f5fc..e0a561e13 100644 --- a/lib/thor/actions/inject_into_file.rb +++ b/lib/thor/actions/inject_into_file.rb @@ -16,11 +16,11 @@ module Actions # # ==== Examples # - # inject_into_file "config/environment.rb", "config.gem thor", :after => "Rails::Initializer.run do |config|\n" + # inject_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n" # # inject_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do # gems = ask "Which gems would you like to add?" - # gems.split(" ").map{ |gem| " config.gem #{gem}" }.join("\n") + # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n") # end # def inject_into_file(destination, *args, &block) @@ -35,30 +35,39 @@ def inject_into_file(destination, *args, &block) end class InjectIntoFile < EmptyDirectory #:nodoc: - attr_reader :flag, :replacement + attr_reader :replacement def initialize(base, destination, data, config) super(base, destination, { :verbose => true }.merge(config)) + @replacement = data.is_a?(Proc) ? data.call : data + end - data = data.call if data.is_a?(Proc) + def invoke! + say_status :inject, config[:verbose] - @replacement = if @config.key?(:after) - @flag = @config.delete(:after) - @flag + data + flag = if @config.key?(:after) + content = '\0' + replacement + @config[:after] else - @flag = @config.delete(:before) - data + @flag + content = replacement + '\0' + @config[:before] end - end - def invoke! - say_status :inject, config[:verbose] - replace!(flag, replacement) + replace!(flag, content) end def revoke! say_status :deinject, config[:verbose] - replace!(replacement, flag) + + flag = if @config.key?(:after) + content = '\1\2' + /(#{Regexp.escape(@config[:after])})(.*)(#{Regexp.escape(replacement)})/m + else + content = '\2\3' + /(#{Regexp.escape(replacement)})(.*)(#{Regexp.escape(@config[:before])})/m + end + + replace!(flag, content) end protected diff --git a/spec/actions/inject_into_file_spec.rb b/spec/actions/inject_into_file_spec.rb index 2fd900d59..07853d7a7 100644 --- a/spec/actions/inject_into_file_spec.rb +++ b/spec/actions/inject_into_file_spec.rb @@ -66,12 +66,28 @@ def file end it "deinjects the destination file before injection" do - invoke! "doc/README", "\nmore content", :before => "__start__" - revoke! "doc/README", "\nmore content", :before => "__start__" + invoke! "doc/README", "more content\n", :before => "__start__" + revoke! "doc/README", "more content\n", :before => "__start__" File.read(file).must == "__start__\nREADME\n__end__\n" end + it "deinjects even with double after injection" do + invoke! "doc/README", "\nmore content", :after => "__start__" + invoke! "doc/README", "\nanother stuff", :after => "__start__" + revoke! "doc/README", "\nmore content", :after => "__start__" + + File.read(file).must == "__start__\nanother stuff\nREADME\n__end__\n" + end + + it "deinjects even with double before injection" do + invoke! "doc/README", "more content\n", :before => "__start__" + invoke! "doc/README", "another stuff\n", :before => "__start__" + revoke! "doc/README", "more content\n", :before => "__start__" + + File.read(file).must == "another stuff\n__start__\nREADME\n__end__\n" + end + it "shows progress information to the user" do invoke!("doc/README", "\nmore content", :after => "__start__") revoke!("doc/README", "\nmore content", :after => "__start__").must == " deinject doc/README\n"