Permalink
Browse files

No need to read the file twice.

  • Loading branch information...
1 parent 07b1f3f commit d490d44fec555d61c73d467b6dfa83a4b203595a @josevalim josevalim committed Feb 2, 2010
Showing with 8 additions and 5 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +7 −5 lib/thor/actions/inject_into_file.rb
View
@@ -1,5 +1,6 @@
== 0.12, released 2010-01-02
+* inject_into_file does not add the same content twice, unless :force is set
* Removed rr in favor to rspec mock framework
* Improved output for thor -T
* [#7] Do not force white color on status
@@ -56,7 +56,7 @@ def invoke!
replacement + '\0'
end
- replace!(/#{flag}/, content) if config[:force] || !File.binread(destination).include?(replacement)
+ replace!(/#{flag}/, content, config[:force])
end
def revoke!
@@ -70,7 +70,7 @@ def revoke!
/(#{Regexp.escape(replacement)})(.*)(#{flag})/m
end
- replace!(regexp, content)
+ replace!(regexp, content, true)
end
protected
@@ -89,11 +89,13 @@ def say_status(behavior)
# Adds the content to the file.
#
- def replace!(regexp, string)
+ def replace!(regexp, string, force)
unless base.options[:pretend]
content = File.binread(destination)
- content.gsub!(regexp, string)
- File.open(destination, 'wb') { |file| file.write(content) }
+ if force || !content.include?(replacement)
+ content.gsub!(regexp, string)
+ File.open(destination, 'wb') { |file| file.write(content) }
+ end
end
end

0 comments on commit d490d44

Please sign in to comment.