Decouple DATA patches from the executing script

This commit is contained in:
Jack Nagel 2014-07-29 16:06:06 -05:00
parent e0c9717717
commit 3f12ddbccd
6 changed files with 65 additions and 21 deletions

View File

@ -1,14 +1,9 @@
# This script is loaded by formula_installer as a separate instance.
# Rationale: Formula can use __END__, Formula can change ENV
# Thrown exceptions are propogated back to the parent process over a pipe
STD_TRAP = trap("INT") { exit! 130 } # no backtrace thanks
at_exit do
# the whole of everything must be run in at_exit because the formula has to
# be the run script as __END__ must work for *that* formula.
main
end
at_exit { main }
require 'global'
require 'cxxstdlib'

View File

@ -212,6 +212,8 @@ class Formula
# any e.g. configure options for this package
def options; [] end
# Deprecated
DATA = :DATA
def patches; {} end
# rarely, you don't want your library symlinked into the main prefix
@ -577,6 +579,8 @@ class Formula
active_spec.add_legacy_patches(patches)
return if patchlist.empty?
active_spec.patches.grep(DATAPatch).each { |p| p.path = path }
active_spec.patches.select(&:external?).each do |patch|
patch.verify_download_integrity(patch.fetch)
end

View File

@ -483,8 +483,6 @@ class FormulaInstaller
# 1. formulae can modify ENV, so we must ensure that each
# installation has a pristine ENV when it starts, forking now is
# the easiest way to do this
# 2. formulae have access to __END__ the only way to allow this is
# to make the formula script the executed script
read, write = IO.pipe
# I'm guessing this is not a good way to do this, but I'm no UNIX guru
ENV['HOMEBREW_ERROR_PIPE'] = write.to_i.to_s

View File

@ -5,13 +5,17 @@ require 'erb'
module Patch
def self.create(strip, io, &block)
case strip
when :DATA, IO, StringIO
when :DATA
DATAPatch.new(:p1)
when IO, StringIO
IOPatch.new(strip, :p1)
when String
IOPatch.new(StringIO.new(strip), :p1)
when Symbol
case io
when :DATA, IO, StringIO
when :DATA
DATAPatch.new(strip)
when IO, StringIO
IOPatch.new(io, strip)
when String
IOPatch.new(StringIO.new(io), strip)
@ -29,16 +33,15 @@ module Patch
case list
when Hash
list
when Array, String, IO
when Array, String, :DATA
{ :p1 => list }
else
{}
end.each_pair do |strip, urls|
urls = [urls] unless Array === urls
urls.each do |url|
Array(urls).each do |url|
case url
when IO
patch = IOPatch.new(url, strip)
when :DATA
patch = DATAPatch.new(strip)
else
patch = LegacyPatch.new(strip, url)
end
@ -64,9 +67,7 @@ class IOPatch
end
def apply
@io = DATA if @io == :DATA
data = @io.read
data.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) }
raise ErrorDuringExecution, "Applying DATA patch failed" unless $?.success?
ensure
@ -75,11 +76,35 @@ class IOPatch
@io = nil
end
def contents
@io.read
end
def inspect
"#<#{self.class.name}: #{strip.inspect}>"
end
end
class DATAPatch < IOPatch
attr_accessor :path
def initialize(strip)
@strip = strip
@path = nil
end
def contents
data = ""
path.open("rb") do |f|
begin
line = f.gets
end until line.nil? || /^__END__$/ === line
data << line while line = f.gets
end
data
end
end
class ExternalPatch
attr_reader :resource, :strip

View File

@ -36,13 +36,13 @@ class PatchTests < Homebrew::TestCase
def test_create_DATA
patch = Patch.create(:p0, :DATA)
assert_kind_of IOPatch, patch
assert_kind_of DATAPatch, patch
assert_equal :p0, patch.strip
end
def test_create_DATA_without_strip
patch = Patch.create(:DATA, nil)
assert_kind_of IOPatch, patch
assert_kind_of DATAPatch, patch
assert_equal :p1, patch.strip
end

View File

@ -8,7 +8,7 @@ class PatchingTests < Homebrew::TestCase
PATCH_A_CONTENTS = File.read "#{TEST_DIRECTORY}/patches/noop-a.diff"
PATCH_B_CONTENTS = File.read "#{TEST_DIRECTORY}/patches/noop-b.diff"
def formula(&block)
def formula(*args, &block)
super do
url "file://#{TEST_DIRECTORY}/tarballs/testball-0.1.tbz"
sha1 "482e737739d946b7c8cbaf127d9ee9c148b999f5"
@ -138,4 +138,26 @@ class PatchingTests < Homebrew::TestCase
end.brew { assert_patched 'libexec/noop' }
end
end
def test_patch_DATA_constant
shutup do
formula("test", Pathname.new(__FILE__).expand_path) do
def patches
Formula::DATA
end
end.brew { assert_patched "libexec/noop" }
end
end
end
__END__
diff --git a/libexec/NOOP b/libexec/NOOP
index bfdda4c..e08d8f4 100755
--- a/libexec/NOOP
+++ b/libexec/NOOP
@@ -1,2 +1,2 @@
#!/bin/bash
-echo NOOP
\ No newline at end of file
+echo ABCD
\ No newline at end of file