Guest User

Untitled

a guest
Apr 27th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. From 2e95e9a51f4e5d7491d63bf530bc510f26ba43c3 Mon Sep 17 00:00:00 2001
  2. From: Luis Lavena <luislavena@gmail.com>
  3. Date: Fri, 1 Jan 2010 22:43:53 -0300
  4. Subject: [PATCH 1/3] Ensure file reading and writing is performed in binary mode.
  5.  
  6. ---
  7. lib/thor/actions.rb | 1 +
  8. lib/thor/actions/create_file.rb | 4 ++--
  9. lib/thor/actions/file_manipulation.rb | 8 ++++----
  10. lib/thor/actions/inject_into_file.rb | 2 +-
  11. lib/thor/core_ext/file_binary_read.rb | 9 +++++++++
  12. lib/thor/shell/color.rb | 2 +-
  13. lib/thor/util.rb | 2 +-
  14. 7 files changed, 19 insertions(+), 9 deletions(-)
  15. create mode 100644 lib/thor/core_ext/file_binary_read.rb
  16.  
  17. diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb
  18. index 727e366..da98444 100644
  19. --- a/lib/thor/actions.rb
  20. +++ b/lib/thor/actions.rb
  21. @@ -1,4 +1,5 @@
  22. require 'fileutils'
  23. +require 'thor/core_ext/file_binary_read'
  24.  
  25. Dir[File.join(File.dirname(__FILE__), "actions", "*.rb")].each do |action|
  26. require action
  27. diff --git a/lib/thor/actions/create_file.rb b/lib/thor/actions/create_file.rb
  28. index a3d9296..6e0eeb4 100644
  29. --- a/lib/thor/actions/create_file.rb
  30. +++ b/lib/thor/actions/create_file.rb
  31. @@ -42,7 +42,7 @@ class Thor
  32. # Boolean:: true if it is identical, false otherwise.
  33. #
  34. def identical?
  35. - exists? && File.read(destination) == render
  36. + exists? && File.binread(destination) == render
  37. end
  38.  
  39. # Holds the content to be added to the file.
  40. @@ -58,7 +58,7 @@ class Thor
  41. def invoke!
  42. invoke_with_conflict_check do
  43. FileUtils.mkdir_p(File.dirname(destination))
  44. - File.open(destination, 'w'){ |f| f.write render }
  45. + File.open(destination, 'wb') { |f| f.write render }
  46. end
  47. given_destination
  48. end
  49. diff --git a/lib/thor/actions/file_manipulation.rb b/lib/thor/actions/file_manipulation.rb
  50. index 8a45c83..44d6836 100644
  51. --- a/lib/thor/actions/file_manipulation.rb
  52. +++ b/lib/thor/actions/file_manipulation.rb
  53. @@ -23,7 +23,7 @@ class Thor
  54. source = File.expand_path(find_in_source_paths(source.to_s))
  55.  
  56. create_file destination, nil, config do
  57. - content = File.read(source)
  58. + content = File.binread(source)
  59. content = block.call(content) if block
  60. content
  61. end
  62. @@ -48,7 +48,7 @@ class Thor
  63. #
  64. def get(source, destination=nil, config={}, &block)
  65. source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\//
  66. - render = open(source).read
  67. + render = File.binread(source)
  68.  
  69. destination ||= if block_given?
  70. block.arity == 1 ? block.call(render) : block.call
  71. @@ -80,7 +80,7 @@ class Thor
  72. context = instance_eval('binding')
  73.  
  74. create_file destination, nil, config do
  75. - content = ERB.new(::File.read(source), nil, '-').result(context)
  76. + content = ERB.new(::File.binread(source), nil, '-').result(context)
  77. content = block.call(content) if block
  78. content
  79. end
  80. @@ -193,7 +193,7 @@ class Thor
  81. say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
  82.  
  83. unless options[:pretend]
  84. - content = File.read(path)
  85. + content = File.binread(path)
  86. content.gsub!(flag, *args, &block)
  87. File.open(path, 'wb') { |file| file.write(content) }
  88. end
  89. diff --git a/lib/thor/actions/inject_into_file.rb b/lib/thor/actions/inject_into_file.rb
  90. index 0636ec6..350ab73 100644
  91. --- a/lib/thor/actions/inject_into_file.rb
  92. +++ b/lib/thor/actions/inject_into_file.rb
  93. @@ -90,7 +90,7 @@ class Thor
  94. #
  95. def replace!(regexp, string)
  96. unless base.options[:pretend]
  97. - content = File.read(destination)
  98. + content = File.binread(destination)
  99. content.gsub!(regexp, string)
  100. File.open(destination, 'wb') { |file| file.write(content) }
  101. end
  102. diff --git a/lib/thor/core_ext/file_binary_read.rb b/lib/thor/core_ext/file_binary_read.rb
  103. new file mode 100644
  104. index 0000000..d6af7e4
  105. --- /dev/null
  106. +++ b/lib/thor/core_ext/file_binary_read.rb
  107. @@ -0,0 +1,9 @@
  108. +class File #:nodoc:
  109. +
  110. + unless File.respond_to?(:binread)
  111. + def self.binread(file)
  112. + File.open(file, 'rb') { |f| f.read }
  113. + end
  114. + end
  115. +
  116. +end
  117. diff --git a/lib/thor/shell/color.rb b/lib/thor/shell/color.rb
  118. index 24704f7..b2bc66d 100644
  119. --- a/lib/thor/shell/color.rb
  120. +++ b/lib/thor/shell/color.rb
  121. @@ -63,7 +63,7 @@ class Thor
  122. #
  123. def show_diff(destination, content) #:nodoc:
  124. if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
  125. - actual = File.read(destination).to_s.split("\n")
  126. + actual = File.binread(destination).to_s.split("\n")
  127. content = content.to_s.split("\n")
  128.  
  129. Diff::LCS.sdiff(actual, content).each do |diff|
  130. diff --git a/lib/thor/util.rb b/lib/thor/util.rb
  131. index ebae0a3..7c16ea2 100644
  132. --- a/lib/thor/util.rb
  133. +++ b/lib/thor/util.rb
  134. @@ -155,7 +155,7 @@ class Thor
  135. # inside the sandbox to avoid namespacing conflicts.
  136. #
  137. def self.load_thorfile(path, content=nil)
  138. - content ||= File.read(path)
  139. + content ||= File.binread(path)
  140.  
  141. begin
  142. Thor::Sandbox.class_eval(content, path)
  143. --
  144. 1.6.4.msysgit.0
Add Comment
Please, Sign In to add comment