brew/Library/Homebrew/patch.rb

164 lines
3.6 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "resource"
require "erb"
2020-08-17 20:06:08 +02:00
# Helper module for creating patches.
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
2023-02-10 23:15:40 -05:00
raise ArgumentError, "Unexpected value #{strip.inspect} for strip"
end
end
end
2020-08-17 20:06:08 +02:00
# An abstract class representing a patch embedded into a formula.
class EmbeddedPatch
attr_writer :owner
attr_reader :strip
def initialize(strip)
@strip = strip
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
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
2020-10-20 12:03:48 +02:00
sig { returns(String) }
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
2020-08-17 20:06:08 +02:00
# A patch at the `__END__` of a formula file.
class DATAPatch < EmbeddedPatch
attr_accessor :path
def initialize(strip)
super
@path = nil
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def contents
data = +""
path.open("rb") do |f|
loop do
line = f.gets
break if line.nil? || /^__END__$/.match?(line)
end
while (line = f.gets)
data << line
end
end
data.freeze
end
end
2020-08-17 20:06:08 +02:00
# A string containing a patch.
class StringPatch < EmbeddedPatch
def initialize(strip, str)
super(strip)
@str = str
end
def contents
@str
end
end
# A file containing a patch.
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
2024-07-14 11:42:22 -04:00
@resource = Resource::Patch.new(&block)
2014-07-28 16:23:42 -05:00
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
2014-07-28 16:23:42 -05:00
def external?
true
end
def owner=(owner)
resource.owner = owner
resource.version(resource.checksum&.hexdigest || 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
2023-03-15 14:29:15 -07:00
if children.length != 1 || !children.fetch(0).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
2023-03-15 14:29:15 -07:00
patch_files << children.fetch(0).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
Utils.safe_popen_write("patch", "-g", "0", "-f", "-#{strip}") do |p|
File.foreach(patch_file) do |line|
data = line.gsub("@@HOMEBREW_PREFIX@@", HOMEBREW_PREFIX)
p.write(data)
end
end
end
end
end
2020-07-26 11:53:05 +02:00
rescue ErrorDuringExecution => e
onoe e
2020-07-27 11:50:40 +02:00
f = resource.owner.owner
2020-07-26 11:53:05 +02:00
cmd, *args = e.cmd
raise BuildError.new(f, cmd, args, ENV.to_hash)
end
2014-03-13 19:51:24 -05:00
2020-10-20 12:03:48 +02:00
sig { returns(String) }
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