Skip to content

Commit

Permalink
Allow inject_into_file to deal with double injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 30, 2009
1 parent 966aada commit d054e24
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
37 changes: 23 additions & 14 deletions lib/thor/actions/inject_into_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
20 changes: 18 additions & 2 deletions spec/actions/inject_into_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d054e24

Please sign in to comment.