Simplify internal representation of patches

- remove support for IO objects, since we no longer access ::DATA
   directly
 - since we don't need to support IO objects, use a separate class for
   string patches and stop wrapping strings in StringIO ojects
This commit is contained in:
Jack Nagel 2014-07-29 16:06:07 -05:00
parent 3f12ddbccd
commit a55e196f5f
4 changed files with 30 additions and 42 deletions

View File

@ -671,8 +671,8 @@ class Formula
specs.each { |spec| spec.option(name, description) }
end
def patch strip=:p1, io=nil, &block
specs.each { |spec| spec.patch(strip, io, &block) }
def patch strip=:p1, src=nil, &block
specs.each { |spec| spec.patch(strip, src, &block) }
end
def plist_options options

View File

@ -1,24 +1,19 @@
require 'resource'
require 'stringio'
require 'erb'
module Patch
def self.create(strip, io, &block)
def self.create(strip, src, &block)
case strip
when :DATA
DATAPatch.new(:p1)
when IO, StringIO
IOPatch.new(strip, :p1)
when String
IOPatch.new(StringIO.new(strip), :p1)
StringPatch.new(:p1, strip)
when Symbol
case io
case src
when :DATA
DATAPatch.new(strip)
when IO, StringIO
IOPatch.new(io, strip)
when String
IOPatch.new(StringIO.new(io), strip)
StringPatch.new(strip, src)
else
ExternalPatch.new(strip, &block)
end
@ -53,31 +48,26 @@ module Patch
end
end
class IOPatch
class EmbeddedPatch
attr_writer :owner
attr_reader :strip
def initialize(strip)
@strip = strip
end
def external?
false
end
def initialize(io, strip)
@io = io
@strip = strip
def contents
raise NotImplementedError
end
def apply
data = contents.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX)
IO.popen("/usr/bin/patch -g 0 -f -#{strip}", "w") { |p| p.write(data) }
raise ErrorDuringExecution, "Applying DATA patch failed" unless $?.success?
ensure
# IO and StringIO cannot be marshaled, so remove the reference
# in case we are indirectly referenced by an exception later.
@io = nil
end
def contents
@io.read
end
def inspect
@ -85,11 +75,11 @@ class IOPatch
end
end
class DATAPatch < IOPatch
class DATAPatch < EmbeddedPatch
attr_accessor :path
def initialize(strip)
@strip = strip
super
@path = nil
end
@ -105,6 +95,17 @@ class DATAPatch < IOPatch
end
end
class StringPatch < EmbeddedPatch
def initialize(strip, str)
super(strip)
@str = str
end
def contents
@str
end
end
class ExternalPatch
attr_reader :resource, :strip

View File

@ -90,8 +90,8 @@ class SoftwareSpec
dependency_collector.requirements
end
def patch strip=:p1, io=nil, &block
patches << Patch.create(strip, io, &block)
def patch strip=:p1, src=nil, &block
patches << Patch.create(strip, src, &block)
end
def add_legacy_patches(list)

View File

@ -9,28 +9,15 @@ class PatchTests < Homebrew::TestCase
assert_equal :p2, patch.strip
end
def test_create_io
patch = Patch.create(:p0, StringIO.new("foo"))
assert_kind_of IOPatch, patch
refute_predicate patch, :external?
assert_equal :p0, patch.strip
end
def test_create_io_without_strip
patch = Patch.create(StringIO.new("foo"), nil)
assert_kind_of IOPatch, patch
assert_equal :p1, patch.strip
end
def test_create_string
patch = Patch.create(:p0, "foo")
assert_kind_of IOPatch, patch
assert_kind_of StringPatch, patch
assert_equal :p0, patch.strip
end
def test_create_string_without_strip
patch = Patch.create("foo", nil)
assert_kind_of IOPatch, patch
assert_kind_of StringPatch, patch
assert_equal :p1, patch.strip
end