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) } specs.each { |spec| spec.option(name, description) }
end end
def patch strip=:p1, io=nil, &block def patch strip=:p1, src=nil, &block
specs.each { |spec| spec.patch(strip, io, &block) } specs.each { |spec| spec.patch(strip, src, &block) }
end end
def plist_options options def plist_options options

View File

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

View File

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

View File

@ -9,28 +9,15 @@ class PatchTests < Homebrew::TestCase
assert_equal :p2, patch.strip assert_equal :p2, patch.strip
end 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 def test_create_string
patch = Patch.create(:p0, "foo") patch = Patch.create(:p0, "foo")
assert_kind_of IOPatch, patch assert_kind_of StringPatch, patch
assert_equal :p0, patch.strip assert_equal :p0, patch.strip
end end
def test_create_string_without_strip def test_create_string_without_strip
patch = Patch.create("foo", nil) patch = Patch.create("foo", nil)
assert_kind_of IOPatch, patch assert_kind_of StringPatch, patch
assert_equal :p1, patch.strip assert_equal :p1, patch.strip
end end