brew/Library/Homebrew/patch.rb

193 lines
3.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "resource"
require "erb"
module Patch
def self.create(strip, src, &block)
2014-07-10 12:38:09 -05:00
case strip
when :DATA
DATAPatch.new(:p1)
when String
StringPatch.new(:p1, strip)
when Symbol
case src
when :DATA
DATAPatch.new(strip)
when String
StringPatch.new(strip, src)
else
ExternalPatch.new(strip, &block)
end
when nil
raise ArgumentError, "nil value for strip"
else
raise ArgumentError, "unexpected value #{strip.inspect} for strip"
end
end
def self.normalize_legacy_patches(list)
patches = []
case list
when Hash
list
when Array, String, :DATA
{ p1: list }
else
{}
end.each_pair do |strip, urls|
Array(urls).each do |url|
2020-03-13 21:15:06 +00:00
patch = case url
when :DATA
2020-03-13 21:15:06 +00:00
DATAPatch.new(strip)
else
2020-03-13 21:15:06 +00:00
LegacyPatch.new(strip, url)
end
patches << patch
end
end
patches
end
end
class EmbeddedPatch
attr_writer :owner
attr_reader :strip
def initialize(strip)
@strip = strip
end
2014-07-28 16:23:42 -05:00
def external?
false
end
def contents; end
def apply
data = contents.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX)
args = %W[-g 0 -f -#{strip}]
2018-07-16 23:17:16 +02:00
Utils.safe_popen_write("patch", *args) { |p| p.write(data) }
end
2014-03-13 19:51:24 -05:00
def inspect
"#<#{self.class.name}: #{strip.inspect}>"
2014-03-13 19:51:24 -05:00
end
end
class DATAPatch < EmbeddedPatch
attr_accessor :path
def initialize(strip)
super
@path = nil
end
def contents
data = +""
path.open("rb") do |f|
loop do
line = f.gets
break if line.nil? || line =~ /^__END__$/
end
while line = f.gets
data << line
end
end
data.freeze
end
end
class StringPatch < EmbeddedPatch
def initialize(strip, str)
super(strip)
@str = str
end
def contents
@str
end
end
2014-07-28 16:23:42 -05:00
class ExternalPatch
2017-05-22 20:35:15 +01:00
extend Forwardable
attr_reader :resource, :strip
2017-05-22 20:35:15 +01:00
def_delegators :resource,
:url, :fetch, :patch_files, :verify_download_integrity,
:cached_download, :downloaded?, :clear_cache
2017-05-22 20:35:15 +01:00
def initialize(strip, &block)
@strip = strip
2018-01-21 08:29:38 -08:00
@resource = Resource::PatchResource.new(&block)
2014-07-28 16:23:42 -05:00
end
def external?
true
end
def owner=(owner)
resource.owner = owner
resource.version = resource.checksum || ERB::Util.url_encode(resource.url)
end
def apply
base_dir = Pathname.pwd
resource.unpack do
patch_dir = Pathname.pwd
if patch_files.empty?
children = patch_dir.children
2016-09-23 22:02:23 +02:00
if children.length != 1 || !children.first.file?
2017-10-15 02:28:32 +02:00
raise MissingApplyError, <<~EOS
There should be exactly one patch file in the staging directory unless
the "apply" method was used one or more times in the patch-do block.
EOS
end
2018-09-17 02:45:00 +02:00
2016-09-23 22:02:23 +02:00
patch_files << children.first.basename
end
dir = base_dir
dir /= resource.directory if resource.directory.present?
dir.cd do
patch_files.each do |patch_file|
ohai "Applying #{patch_file}"
patch_file = patch_dir/patch_file
2017-11-19 13:54:00 -08:00
safe_system "patch", "-g", "0", "-f", "-#{strip}", "-i", patch_file
end
end
end
end
2014-03-13 19:51:24 -05:00
def inspect
"#<#{self.class.name}: #{strip.inspect} #{url.inspect}>"
2014-03-13 19:51:24 -05:00
end
end
# Legacy patches have no checksum and are not cached.
class LegacyPatch < ExternalPatch
def initialize(strip, url)
odeprecated "legacy patches", "'patch do' blocks"
super(strip)
resource.url(url)
resource.download_strategy = CurlDownloadStrategy
end
def fetch
clear_cache
super
end
def verify_download_integrity(_fn)
# no-op
end
def apply
super
ensure
clear_cache
end
end