Created
January 2, 2010 03:02
-
-
Save luislavena/267357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 547c3a7792f01c680c8324d929537c4190b53563 Mon Sep 17 00:00:00 2001 | |
From: Luis Lavena <luislavena@gmail.com> | |
Date: Fri, 1 Jan 2010 22:49:36 -0300 | |
Subject: [PATCH 2/3] Ensure specs use proper file reading meachanism. | |
--- | |
spec/actions/file_manipulation_spec.rb | 20 ++++++++++---------- | |
1 files changed, 10 insertions(+), 10 deletions(-) | |
diff --git a/spec/actions/file_manipulation_spec.rb b/spec/actions/file_manipulation_spec.rb | |
index 0ccd71a..79632aa 100644 | |
--- a/spec/actions/file_manipulation_spec.rb | |
+++ b/spec/actions/file_manipulation_spec.rb | |
@@ -179,18 +179,18 @@ describe Thor::Actions do | |
describe "#gsub_file" do | |
it "replaces the content in the file" do | |
action :gsub_file, "doc/README", "__start__", "START" | |
- File.open(file).read.must == "START\nREADME\n__end__\n" | |
+ File.binread(file).must == "START\nREADME\n__end__\n" | |
end | |
it "does not replace if pretending" do | |
runner(:pretend => true) | |
action :gsub_file, "doc/README", "__start__", "START" | |
- File.open(file).read.must == "__start__\nREADME\n__end__\n" | |
+ File.binread(file).must == "__start__\nREADME\n__end__\n" | |
end | |
it "accepts a block" do | |
action(:gsub_file, "doc/README", "__start__"){ |match| match.gsub('__', '').upcase } | |
- File.open(file).read.must == "START\nREADME\n__end__\n" | |
+ File.binread(file).must == "START\nREADME\n__end__\n" | |
end | |
it "logs status" do | |
@@ -205,12 +205,12 @@ describe Thor::Actions do | |
describe "#append_file" do | |
it "appends content to the file" do | |
action :append_file, "doc/README", "END\n" | |
- File.open(file).read.must == "__start__\nREADME\n__end__\nEND\n" | |
+ File.binread(file).must == "__start__\nREADME\n__end__\nEND\n" | |
end | |
it "accepts a block" do | |
action(:append_file, "doc/README"){ "END\n" } | |
- File.open(file).read.must == "__start__\nREADME\n__end__\nEND\n" | |
+ File.binread(file).must == "__start__\nREADME\n__end__\nEND\n" | |
end | |
it "logs status" do | |
@@ -221,12 +221,12 @@ describe Thor::Actions do | |
describe "#prepend_file" do | |
it "prepends content to the file" do | |
action :prepend_file, "doc/README", "START\n" | |
- File.open(file).read.must == "START\n__start__\nREADME\n__end__\n" | |
+ File.binread(file).must == "START\n__start__\nREADME\n__end__\n" | |
end | |
it "accepts a block" do | |
action(:prepend_file, "doc/README"){ "START\n" } | |
- File.open(file).read.must == "START\n__start__\nREADME\n__end__\n" | |
+ File.binread(file).must == "START\n__start__\nREADME\n__end__\n" | |
end | |
it "logs status" do | |
@@ -241,12 +241,12 @@ describe Thor::Actions do | |
it "appends content to a class" do | |
action :inject_into_class, "application.rb", Application, " filter_parameters :password\n" | |
- File.open(file).read.must == "class Application < Base\n filter_parameters :password\nend\n" | |
+ File.binread(file).must == "class Application < Base\n filter_parameters :password\nend\n" | |
end | |
it "accepts a block" do | |
action(:inject_into_class, "application.rb", Application){ " filter_parameters :password\n" } | |
- File.open(file).read.must == "class Application < Base\n filter_parameters :password\nend\n" | |
+ File.binread(file).must == "class Application < Base\n filter_parameters :password\nend\n" | |
end | |
it "logs status" do | |
@@ -255,7 +255,7 @@ describe Thor::Actions do | |
it "does not append if class name does not match" do | |
action :inject_into_class, "application.rb", "App", " filter_parameters :password\n" | |
- File.open(file).read.must == "class Application < Base\nend\n" | |
+ File.binread(file).must == "class Application < Base\nend\n" | |
end | |
end | |
end | |
-- | |
1.6.4.msysgit.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 958963650bf77c23a7998d2ae2f70e93449d94b2 Mon Sep 17 00:00:00 2001 | |
From: Luis Lavena <luislavena@gmail.com> | |
Date: Fri, 1 Jan 2010 23:48:17 -0300 | |
Subject: [PATCH 3/3] Expand root to work on OS with drive letters | |
--- | |
spec/actions_spec.rb | 5 +++-- | |
1 files changed, 3 insertions(+), 2 deletions(-) | |
diff --git a/spec/actions_spec.rb b/spec/actions_spec.rb | |
index 2935a3b..90f86db 100644 | |
--- a/spec/actions_spec.rb | |
+++ b/spec/actions_spec.rb | |
@@ -55,9 +55,10 @@ describe Thor::Actions do | |
end | |
it "does not use the current directory if one is given" do | |
+ root = File.expand_path("/") | |
base = MyCounter.new([1]) | |
- base.destination_root = "/" | |
- base.destination_root.must == "/" | |
+ base.destination_root = root | |
+ base.destination_root.must == root | |
end | |
it "uses the current directory if none is given" do | |
-- | |
1.6.4.msysgit.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment