Permalink
Browse files

Ensure file reading and writing is performed in binary mode.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
luislavena authored and josevalim committed Jan 2, 2010
1 parent 1f91d12 commit dd072c996186d691dae951911c9b07593aae29e7
View
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'thor/core_ext/file_binary_read'
Dir[File.join(File.dirname(__FILE__), "actions", "*.rb")].each do |action|
require action
@@ -42,7 +42,7 @@ def initialize(base, destination, data, config={})
# Boolean:: true if it is identical, false otherwise.
#
def identical?
- exists? && File.read(destination) == render
+ exists? && File.binread(destination) == render
end
# Holds the content to be added to the file.
@@ -58,7 +58,7 @@ def render
def invoke!
invoke_with_conflict_check do
FileUtils.mkdir_p(File.dirname(destination))
- File.open(destination, 'w'){ |f| f.write render }
+ File.open(destination, 'wb') { |f| f.write render }
end
given_destination
end
@@ -23,7 +23,7 @@ def copy_file(source, destination=nil, config={}, &block)
source = File.expand_path(find_in_source_paths(source.to_s))
create_file destination, nil, config do
- content = File.read(source)
+ content = File.binread(source)
content = block.call(content) if block
content
end
@@ -48,7 +48,7 @@ def copy_file(source, destination=nil, config={}, &block)
#
def get(source, destination=nil, config={}, &block)
source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\//
- render = open(source).read
+ render = File.binread(source)
destination ||= if block_given?
block.arity == 1 ? block.call(render) : block.call
@@ -80,7 +80,7 @@ def template(source, destination=nil, config={}, &block)
context = instance_eval('binding')
create_file destination, nil, config do
- content = ERB.new(::File.read(source), nil, '-').result(context)
+ content = ERB.new(::File.binread(source), nil, '-').result(context)
content = block.call(content) if block
content
end
@@ -193,7 +193,7 @@ def gsub_file(path, flag, *args, &block)
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
unless options[:pretend]
- content = File.read(path)
+ content = File.binread(path)
content.gsub!(flag, *args, &block)
File.open(path, 'wb') { |file| file.write(content) }
end
@@ -90,7 +90,7 @@ def say_status(behavior)
#
def replace!(regexp, string)
unless base.options[:pretend]
- content = File.read(destination)
+ content = File.binread(destination)
content.gsub!(regexp, string)
File.open(destination, 'wb') { |file| file.write(content) }
end
@@ -0,0 +1,9 @@
+class File #:nodoc:
+
+ unless File.respond_to?(:binread)
+ def self.binread(file)
+ File.open(file, 'rb') { |f| f.read }
+ end
+ end
+
+end
View
@@ -63,7 +63,7 @@ def set_color(string, color, bold=false)
#
def show_diff(destination, content) #:nodoc:
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
- actual = File.read(destination).to_s.split("\n")
+ actual = File.binread(destination).to_s.split("\n")
content = content.to_s.split("\n")
Diff::LCS.sdiff(actual, content).each do |diff|
View
@@ -155,7 +155,7 @@ def self.namespace_to_thor_class_and_task(namespace, raise_if_nil=true)
# inside the sandbox to avoid namespacing conflicts.
#
def self.load_thorfile(path, content=nil)
- content ||= File.read(path)
+ content ||= File.binread(path)
begin
Thor::Sandbox.class_eval(content, path)

0 comments on commit dd072c9

Please sign in to comment.