Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow inject_into_file to deal with double injection.

  • Loading branch information...
commit d054e24ceaa020eeea5e030533f5ee49efda4ac0 1 parent 966aada
@josevalim josevalim authored
View
37 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
View
20 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"
Please sign in to comment.
Something went wrong with that request. Please try again.