Port Homebrew::DevCmd::Extract

This commit is contained in:
Douglas Eichelberger 2024-03-21 08:24:37 -07:00
parent 2cc70549d8
commit 2f461b1b95
2 changed files with 195 additions and 189 deletions

View File

@ -1,6 +1,7 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "cli/parser" require "cli/parser"
require "utils/git" require "utils/git"
require "formulary" require "formulary"
@ -8,216 +9,220 @@ require "software_spec"
require "tap" require "tap"
module Homebrew module Homebrew
BOTTLE_BLOCK_REGEX = / bottle (?:do.+?end|:[a-z]+)\n\n/m module DevCmd
class Extract < AbstractCommand
BOTTLE_BLOCK_REGEX = / bottle (?:do.+?end|:[a-z]+)\n\n/m
sig { returns(CLI::Parser) } cmd_args do
def self.extract_args usage_banner "`extract` [`--version=`] [`--force`] <formula> <tap>"
Homebrew::CLI::Parser.new do description <<~EOS
usage_banner "`extract` [`--version=`] [`--force`] <formula> <tap>" Look through repository history to find the most recent version of <formula> and
description <<~EOS create a copy in <tap>. Specifically, the command will create the new
Look through repository history to find the most recent version of <formula> and formula file at <tap>`/Formula/`<formula>`@`<version>`.rb`. If the tap is not
create a copy in <tap>. Specifically, the command will create the new installed yet, attempt to install/clone the tap before continuing. To extract
formula file at <tap>`/Formula/`<formula>`@`<version>`.rb`. If the tap is not a formula from a tap that is not `homebrew/core` use its fully-qualified form of
installed yet, attempt to install/clone the tap before continuing. To extract <user>`/`<repo>`/`<formula>.
a formula from a tap that is not `homebrew/core` use its fully-qualified form of EOS
<user>`/`<repo>`/`<formula>. flag "--version=",
EOS description: "Extract the specified <version> of <formula> instead of the most recent."
flag "--version=", switch "-f", "--force",
description: "Extract the specified <version> of <formula> instead of the most recent." description: "Overwrite the destination formula if it already exists."
switch "-f", "--force",
description: "Overwrite the destination formula if it already exists."
named_args [:formula, :tap], number: 2, without_api: true named_args [:formula, :tap], number: 2, without_api: true
end end
end
def self.extract sig { override.void }
args = extract_args.parse def run
if (tap_with_name = args.named.first&.then { Tap.with_formula_name(_1) })
source_tap, name = tap_with_name
else
name = args.named.first.downcase
source_tap = CoreTap.instance
end
raise TapFormulaUnavailableError.new(source_tap, name) unless source_tap.installed?
if (tap_with_name = args.named.first&.then { Tap.with_formula_name(_1) }) destination_tap = Tap.fetch(args.named.second)
source_tap, name = tap_with_name unless Homebrew::EnvConfig.developer?
else odie "Cannot extract formula to homebrew/core!" if destination_tap.core_tap?
name = args.named.first.downcase odie "Cannot extract formula to homebrew/cask!" if destination_tap.core_cask_tap?
source_tap = CoreTap.instance odie "Cannot extract formula to the same tap!" if destination_tap == source_tap
end end
raise TapFormulaUnavailableError.new(source_tap, name) unless source_tap.installed? destination_tap.install unless destination_tap.installed?
destination_tap = Tap.fetch(args.named.second) repo = source_tap.path
unless Homebrew::EnvConfig.developer? pattern = if source_tap.core_tap?
odie "Cannot extract formula to homebrew/core!" if destination_tap.core_tap? [source_tap.new_formula_path(name), repo/"Formula/#{name}.rb"].uniq
odie "Cannot extract formula to homebrew/cask!" if destination_tap.core_cask_tap? else
odie "Cannot extract formula to the same tap!" if destination_tap == source_tap # A formula can technically live in the root directory of a tap or in any of its subdirectories
end [repo/"#{name}.rb", repo/"**/#{name}.rb"]
destination_tap.install unless destination_tap.installed?
repo = source_tap.path
pattern = if source_tap.core_tap?
[source_tap.new_formula_path(name), repo/"Formula/#{name}.rb"].uniq
else
# A formula can technically live in the root directory of a tap or in any of its subdirectories
[repo/"#{name}.rb", repo/"**/#{name}.rb"]
end
if args.version
ohai "Searching repository history"
version = args.version
version_segments = Gem::Version.new(version).segments if Gem::Version.correct?(version)
rev = T.let(nil, T.nilable(String))
test_formula = T.let(nil, T.nilable(Formula))
result = ""
loop do
rev = rev.nil? ? "HEAD" : "#{rev}~1"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern, before_commit: rev)
if rev.nil? && source_tap.shallow?
odie <<~EOS
Could not find #{name} but #{source_tap} is a shallow clone!
Try again after running:
git -C "#{source_tap.path}" fetch --unshallow
EOS
elsif rev.nil?
odie "Could not find #{name}! The formula or version may not have existed."
end end
file = repo/path if args.version
result = Utils::Git.last_revision_of_file(repo, file, before_commit: rev) ohai "Searching repository history"
if result.empty? version = args.version
odebug "Skipping revision #{rev} - file is empty at this revision" version_segments = Gem::Version.new(version).segments if Gem::Version.correct?(version)
next rev = T.let(nil, T.nilable(String))
end test_formula = T.let(nil, T.nilable(Formula))
result = ""
loop do
rev = rev.nil? ? "HEAD" : "#{rev}~1"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern, before_commit: rev)
if rev.nil? && source_tap.shallow?
odie <<~EOS
Could not find #{name} but #{source_tap} is a shallow clone!
Try again after running:
git -C "#{source_tap.path}" fetch --unshallow
EOS
elsif rev.nil?
odie "Could not find #{name}! The formula or version may not have existed."
end
test_formula = formula_at_revision(repo, name, file, rev) file = repo/path
break if test_formula.nil? || test_formula.version == version result = Utils::Git.last_revision_of_file(repo, file, before_commit: rev)
if result.empty?
odebug "Skipping revision #{rev} - file is empty at this revision"
next
end
if version_segments && Gem::Version.correct?(test_formula.version) test_formula = formula_at_revision(repo, name, file, rev)
test_formula_version_segments = Gem::Version.new(test_formula.version).segments break if test_formula.nil? || test_formula.version == version
if version_segments.length < test_formula_version_segments.length
odebug "Apply semantic versioning with #{test_formula_version_segments}" if version_segments && Gem::Version.correct?(test_formula.version)
break if version_segments == test_formula_version_segments.first(version_segments.length) test_formula_version_segments = Gem::Version.new(test_formula.version).segments
if version_segments.length < test_formula_version_segments.length
odebug "Apply semantic versioning with #{test_formula_version_segments}"
break if version_segments == test_formula_version_segments.first(version_segments.length)
end
end
odebug "Trying #{test_formula.version} from revision #{rev} against desired #{version}"
end
odie "Could not find #{name}! The formula or version may not have existed." if test_formula.nil?
else
# Search in the root directory of <repo> as well as recursively in all of its subdirectories
files = Dir[repo/"{,**/}"].filter_map do |dir|
Pathname.glob("#{dir}/#{name}.rb").find(&:file?)
end
if files.empty?
ohai "Searching repository history"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern)
odie "Could not find #{name}! The formula or version may not have existed." if rev.nil?
file = repo/path
version = T.must(formula_at_revision(repo, name, file, rev)).version
result = Utils::Git.last_revision_of_file(repo, file)
else
file = files.fetch(0).realpath
rev = T.let("HEAD", T.nilable(String))
version = Formulary.factory(file).version
result = File.read(file)
end end
end end
odebug "Trying #{test_formula.version} from revision #{rev} against desired #{version}" # The class name has to be renamed to match the new filename,
end # e.g. Foo version 1.2.3 becomes FooAT123 and resides in Foo@1.2.3.rb.
odie "Could not find #{name}! The formula or version may not have existed." if test_formula.nil? class_name = Formulary.class_s(name)
else
# Search in the root directory of <repo> as well as recursively in all of its subdirectories # Remove any existing version suffixes, as a new one will be added later
files = Dir[repo/"{,**/}"].filter_map do |dir| name.sub!(/\b@(.*)\z\b/i, "")
Pathname.glob("#{dir}/#{name}.rb").find(&:file?) versioned_name = Formulary.class_s("#{name}@#{version}")
result.sub!("class #{class_name} < Formula", "class #{versioned_name} < Formula")
# Remove bottle blocks, they won't work.
result.sub!(BOTTLE_BLOCK_REGEX, "")
path = destination_tap.path/"Formula/#{name}@#{version.to_s.downcase}.rb"
if path.exist?
unless args.force?
odie <<~EOS
Destination formula already exists: #{path}
To overwrite it and continue anyways, run:
brew extract --force --version=#{version} #{name} #{destination_tap.name}
EOS
end
odebug "Overwriting existing formula at #{path}"
path.delete
end
ohai "Writing formula for #{name} from revision #{rev} to:", path
path.dirname.mkpath
path.write result
end end
if files.empty? private
ohai "Searching repository history"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern) sig { params(repo: Pathname, name: String, file: Pathname, rev: String).returns(T.nilable(Formula)) }
odie "Could not find #{name}! The formula or version may not have existed." if rev.nil? def formula_at_revision(repo, name, file, rev)
file = repo/path return if rev.empty?
version = T.must(formula_at_revision(repo, name, file, rev)).version
result = Utils::Git.last_revision_of_file(repo, file) contents = Utils::Git.last_revision_of_file(repo, file, before_commit: rev)
else contents.gsub!("@url=", "url ")
file = files.fetch(0).realpath contents.gsub!("require 'brewkit'", "require 'formula'")
rev = T.let("HEAD", T.nilable(String)) contents.sub!(BOTTLE_BLOCK_REGEX, "")
version = Formulary.factory(file).version with_monkey_patch { Formulary.from_contents(name, file, contents, ignore_errors: true) }
result = File.read(file)
end end
end
# The class name has to be renamed to match the new filename, def with_monkey_patch
# e.g. Foo version 1.2.3 becomes FooAT123 and resides in Foo@1.2.3.rb. # Since `method_defined?` is not a supported type guard, the use of `alias_method` below is not typesafe:
class_name = Formulary.class_s(name) BottleSpecification.class_eval do
T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing)
define_method(:method_missing) do |*|
# do nothing
end
end
# Remove any existing version suffixes, as a new one will be added later Module.class_eval do
name.sub!(/\b@(.*)\z\b/i, "") T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing)
versioned_name = Formulary.class_s("#{name}@#{version}") define_method(:method_missing) do |*|
result.sub!("class #{class_name} < Formula", "class #{versioned_name} < Formula") # do nothing
end
end
# Remove bottle blocks, they won't work. Resource.class_eval do
result.sub!(BOTTLE_BLOCK_REGEX, "") T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing)
define_method(:method_missing) do |*|
# do nothing
end
end
path = destination_tap.path/"Formula/#{name}@#{version.to_s.downcase}.rb" DependencyCollector.class_eval do
if path.exist? if method_defined?(:parse_symbol_spec)
unless args.force? T.unsafe(self).alias_method :old_parse_symbol_spec,
odie <<~EOS :parse_symbol_spec
Destination formula already exists: #{path} end
To overwrite it and continue anyways, run: define_method(:parse_symbol_spec) do |*|
brew extract --force --version=#{version} #{name} #{destination_tap.name} # do nothing
EOS end
end end
odebug "Overwriting existing formula at #{path}"
path.delete
end
ohai "Writing formula for #{name} from revision #{rev} to:", path
path.dirname.mkpath
path.write result
end
# @private yield
sig { params(repo: Pathname, name: String, file: Pathname, rev: String).returns(T.nilable(Formula)) } ensure
def self.formula_at_revision(repo, name, file, rev) BottleSpecification.class_eval do
return if rev.empty? if method_defined?(:old_method_missing)
T.unsafe(self).alias_method :method_missing, :old_method_missing
undef :old_method_missing
end
end
contents = Utils::Git.last_revision_of_file(repo, file, before_commit: rev) Module.class_eval do
contents.gsub!("@url=", "url ") if method_defined?(:old_method_missing)
contents.gsub!("require 'brewkit'", "require 'formula'") T.unsafe(self).alias_method :method_missing, :old_method_missing
contents.sub!(BOTTLE_BLOCK_REGEX, "") undef :old_method_missing
with_monkey_patch { Formulary.from_contents(name, file, contents, ignore_errors: true) } end
end end
private_class_method def self.with_monkey_patch Resource.class_eval do
# Since `method_defined?` is not a supported type guard, the use of `alias_method` below is not typesafe: if method_defined?(:old_method_missing)
BottleSpecification.class_eval do T.unsafe(self).alias_method :method_missing, :old_method_missing
T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) undef :old_method_missing
define_method(:method_missing) do |*| end
# do nothing end
end
end
Module.class_eval do DependencyCollector.class_eval do
T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) if method_defined?(:old_parse_symbol_spec)
define_method(:method_missing) do |*| T.unsafe(self).alias_method :parse_symbol_spec, :old_parse_symbol_spec
# do nothing undef :old_parse_symbol_spec
end end
end end
Resource.class_eval do
T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing)
define_method(:method_missing) do |*|
# do nothing
end
end
DependencyCollector.class_eval do
T.unsafe(self).alias_method :old_parse_symbol_spec, :parse_symbol_spec if method_defined?(:parse_symbol_spec)
define_method(:parse_symbol_spec) do |*|
# do nothing
end
end
yield
ensure
BottleSpecification.class_eval do
if method_defined?(:old_method_missing)
T.unsafe(self).alias_method :method_missing, :old_method_missing
undef :old_method_missing
end
end
Module.class_eval do
if method_defined?(:old_method_missing)
T.unsafe(self).alias_method :method_missing, :old_method_missing
undef :old_method_missing
end
end
Resource.class_eval do
if method_defined?(:old_method_missing)
T.unsafe(self).alias_method :method_missing, :old_method_missing
undef :old_method_missing
end
end
DependencyCollector.class_eval do
if method_defined?(:old_parse_symbol_spec)
T.unsafe(self).alias_method :parse_symbol_spec, :old_parse_symbol_spec
undef :old_parse_symbol_spec
end end
end end
end end

View File

@ -1,9 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "dev-cmd/extract"
RSpec.describe "brew extract" do RSpec.describe Homebrew::DevCmd::Extract do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments", argv: ["foo", "bar"]
context "when extracting a formula" do context "when extracting a formula" do
let!(:target) do let!(:target) do