Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 2e95e9a51f4e5d7491d63bf530bc510f26ba43c3 Mon Sep 17 00:00:00 2001
- From: Luis Lavena <luislavena@gmail.com>
- Date: Fri, 1 Jan 2010 22:43:53 -0300
- Subject: [PATCH 1/3] Ensure file reading and writing is performed in binary mode.
- ---
- lib/thor/actions.rb | 1 +
- lib/thor/actions/create_file.rb | 4 ++--
- lib/thor/actions/file_manipulation.rb | 8 ++++----
- lib/thor/actions/inject_into_file.rb | 2 +-
- lib/thor/core_ext/file_binary_read.rb | 9 +++++++++
- lib/thor/shell/color.rb | 2 +-
- lib/thor/util.rb | 2 +-
- 7 files changed, 19 insertions(+), 9 deletions(-)
- create mode 100644 lib/thor/core_ext/file_binary_read.rb
- diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb
- index 727e366..da98444 100644
- --- a/lib/thor/actions.rb
- +++ b/lib/thor/actions.rb
- @@ -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
- diff --git a/lib/thor/actions/create_file.rb b/lib/thor/actions/create_file.rb
- index a3d9296..6e0eeb4 100644
- --- a/lib/thor/actions/create_file.rb
- +++ b/lib/thor/actions/create_file.rb
- @@ -42,7 +42,7 @@ class Thor
- # 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 @@ class Thor
- 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
- diff --git a/lib/thor/actions/file_manipulation.rb b/lib/thor/actions/file_manipulation.rb
- index 8a45c83..44d6836 100644
- --- a/lib/thor/actions/file_manipulation.rb
- +++ b/lib/thor/actions/file_manipulation.rb
- @@ -23,7 +23,7 @@ class Thor
- 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 @@ class Thor
- #
- 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 @@ class Thor
- 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 @@ class Thor
- 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
- diff --git a/lib/thor/actions/inject_into_file.rb b/lib/thor/actions/inject_into_file.rb
- index 0636ec6..350ab73 100644
- --- a/lib/thor/actions/inject_into_file.rb
- +++ b/lib/thor/actions/inject_into_file.rb
- @@ -90,7 +90,7 @@ class Thor
- #
- 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
- diff --git a/lib/thor/core_ext/file_binary_read.rb b/lib/thor/core_ext/file_binary_read.rb
- new file mode 100644
- index 0000000..d6af7e4
- --- /dev/null
- +++ b/lib/thor/core_ext/file_binary_read.rb
- @@ -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
- diff --git a/lib/thor/shell/color.rb b/lib/thor/shell/color.rb
- index 24704f7..b2bc66d 100644
- --- a/lib/thor/shell/color.rb
- +++ b/lib/thor/shell/color.rb
- @@ -63,7 +63,7 @@ class Thor
- #
- 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|
- diff --git a/lib/thor/util.rb b/lib/thor/util.rb
- index ebae0a3..7c16ea2 100644
- --- a/lib/thor/util.rb
- +++ b/lib/thor/util.rb
- @@ -155,7 +155,7 @@ class Thor
- # 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)
- --
- 1.6.4.msysgit.0
Add Comment
Please, Sign In to add comment