Skip to content

Commit

Permalink
Ensure file reading and writing is performed in binary mode.
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
luislavena authored and josevalim committed Jan 2, 2010
1 parent 1f91d12 commit dd072c9
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/thor/actions.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/thor/actions/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/thor/actions/file_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/thor/actions/inject_into_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/thor/core_ext/file_binary_read.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/thor/shell/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion lib/thor/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd072c9

Please sign in to comment.