Merge pull request #16260 from abitrolly/formula-constructor
formula_creator: move initial CLI values into constructor
This commit is contained in:
commit
3f841cbc2b
@ -140,22 +140,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_formula(args:)
|
def create_formula(args:)
|
||||||
fc = FormulaCreator.new(args)
|
mode = if args.autotools?
|
||||||
fc.name = if args.set_name.blank?
|
|
||||||
stem = Pathname.new(args.named.first).stem.rpartition("=").last
|
|
||||||
print "Formula name [#{stem}]: "
|
|
||||||
__gets || stem
|
|
||||||
else
|
|
||||||
args.set_name
|
|
||||||
end
|
|
||||||
fc.version = args.set_version
|
|
||||||
fc.license = args.set_license
|
|
||||||
fc.tap = Tap.fetch(args.tap || "homebrew/core")
|
|
||||||
raise TapUnavailableError, fc.tap.name unless fc.tap.installed?
|
|
||||||
|
|
||||||
fc.url = args.named.first
|
|
||||||
|
|
||||||
fc.mode = if args.autotools?
|
|
||||||
:autotools
|
:autotools
|
||||||
elsif args.cmake?
|
elsif args.cmake?
|
||||||
:cmake
|
:cmake
|
||||||
@ -177,6 +162,26 @@ module Homebrew
|
|||||||
:rust
|
:rust
|
||||||
end
|
end
|
||||||
|
|
||||||
|
fc = FormulaCreator.new(
|
||||||
|
args.set_name,
|
||||||
|
args.set_version,
|
||||||
|
tap: args.tap,
|
||||||
|
url: args.named.first,
|
||||||
|
mode: mode,
|
||||||
|
license: args.set_license,
|
||||||
|
fetch: !args.no_fetch?,
|
||||||
|
head: args.HEAD?,
|
||||||
|
)
|
||||||
|
if fc.name.blank?
|
||||||
|
stem = Pathname.new(args.named.first).stem.rpartition("=").last
|
||||||
|
print "Formula name [#{stem}]: "
|
||||||
|
fc.name = __gets || stem
|
||||||
|
end
|
||||||
|
|
||||||
|
fc.parse_url
|
||||||
|
|
||||||
|
fc.verify
|
||||||
|
|
||||||
# Check for disallowed formula, or names that shadow aliases,
|
# Check for disallowed formula, or names that shadow aliases,
|
||||||
# unless --force is specified.
|
# unless --force is specified.
|
||||||
unless args.force?
|
unless args.force?
|
||||||
|
|||||||
@ -9,18 +9,32 @@ module Homebrew
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
class FormulaCreator
|
class FormulaCreator
|
||||||
attr_reader :args, :url, :sha256, :desc, :homepage
|
attr_accessor :name
|
||||||
attr_accessor :name, :version, :tap, :mode, :license
|
|
||||||
|
|
||||||
def initialize(args)
|
sig {
|
||||||
@args = args
|
params(name: T.nilable(String), version: T.nilable(String), tap: T.nilable(String), url: String,
|
||||||
|
mode: T.nilable(Symbol), license: T.nilable(String), fetch: T::Boolean, head: T::Boolean).void
|
||||||
|
}
|
||||||
|
def initialize(name, version, tap:, url:, mode:, license:, fetch:, head:)
|
||||||
|
@name = name
|
||||||
|
@version = Version.new(version) if version
|
||||||
|
@tap = Tap.fetch(tap || "homebrew/core")
|
||||||
|
@url = url
|
||||||
|
@mode = mode
|
||||||
|
@license = license
|
||||||
|
@fetch = fetch
|
||||||
|
@head = head
|
||||||
end
|
end
|
||||||
|
|
||||||
def url=(url)
|
sig { void }
|
||||||
@url = url
|
def verify
|
||||||
path = Pathname.new(url)
|
raise TapUnavailableError, @tap.name unless @tap.installed?
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_url
|
||||||
|
path = Pathname.new(@url)
|
||||||
if @name.nil?
|
if @name.nil?
|
||||||
case url
|
case @url
|
||||||
when %r{github\.com/(\S+)/(\S+)\.git}
|
when %r{github\.com/(\S+)/(\S+)\.git}
|
||||||
@user = Regexp.last_match(1)
|
@user = Regexp.last_match(1)
|
||||||
@name = Regexp.last_match(2)
|
@name = Regexp.last_match(2)
|
||||||
@ -34,19 +48,7 @@ module Homebrew
|
|||||||
@name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1]
|
@name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@version = if @version
|
@version = Version.detect(@url) if @version.nil?
|
||||||
Version.new(@version)
|
|
||||||
else
|
|
||||||
Version.detect(url)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch?
|
|
||||||
!args.no_fetch?
|
|
||||||
end
|
|
||||||
|
|
||||||
def head?
|
|
||||||
@head || args.HEAD?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_formula!
|
def write_formula!
|
||||||
@ -56,13 +58,13 @@ module Homebrew
|
|||||||
path = @tap.new_formula_path(@name)
|
path = @tap.new_formula_path(@name)
|
||||||
raise "#{path} already exists" if path.exist?
|
raise "#{path} already exists" if path.exist?
|
||||||
|
|
||||||
if version.nil? || version.null?
|
if @version.nil? || @version.null?
|
||||||
odie "Version cannot be determined from URL. Explicitly set the version with `--set-version` instead."
|
odie "Version cannot be determined from URL. Explicitly set the version with `--set-version` instead."
|
||||||
elsif fetch?
|
elsif @fetch
|
||||||
unless head?
|
unless @head
|
||||||
r = Resource.new
|
r = Resource.new
|
||||||
r.url(url)
|
r.url(@url)
|
||||||
r.version(version)
|
r.version(@version)
|
||||||
r.owner = self
|
r.owner = self
|
||||||
@sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy
|
@sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy
|
||||||
end
|
end
|
||||||
@ -92,53 +94,53 @@ module Homebrew
|
|||||||
# Documentation: https://docs.brew.sh/Formula-Cookbook
|
# Documentation: https://docs.brew.sh/Formula-Cookbook
|
||||||
# https://rubydoc.brew.sh/Formula
|
# https://rubydoc.brew.sh/Formula
|
||||||
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
|
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
|
||||||
<% if mode == :node %>
|
<% if @mode == :node %>
|
||||||
require "language/node"
|
require "language/node"
|
||||||
|
|
||||||
<% end %>
|
<% end %>
|
||||||
class #{Formulary.class_s(name)} < Formula
|
class #{Formulary.class_s(name)} < Formula
|
||||||
<% if mode == :python %>
|
<% if @mode == :python %>
|
||||||
include Language::Python::Virtualenv
|
include Language::Python::Virtualenv
|
||||||
|
|
||||||
<% end %>
|
<% end %>
|
||||||
desc "#{desc}"
|
desc "#{@desc}"
|
||||||
homepage "#{homepage}"
|
homepage "#{@homepage}"
|
||||||
<% unless head? %>
|
<% unless @head %>
|
||||||
url "#{url}"
|
url "#{@url}"
|
||||||
<% unless version.detected_from_url? %>
|
<% unless @version.detected_from_url? %>
|
||||||
version "#{version}"
|
version "#{@version}"
|
||||||
<% end %>
|
<% end %>
|
||||||
sha256 "#{sha256}"
|
sha256 "#{@sha256}"
|
||||||
<% end %>
|
<% end %>
|
||||||
license "#{license}"
|
license "#{@license}"
|
||||||
<% if head? %>
|
<% if @head %>
|
||||||
head "#{url}"
|
head "#{@url}"
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if mode == :cmake %>
|
<% if @mode == :cmake %>
|
||||||
depends_on "cmake" => :build
|
depends_on "cmake" => :build
|
||||||
<% elsif mode == :crystal %>
|
<% elsif @mode == :crystal %>
|
||||||
depends_on "crystal" => :build
|
depends_on "crystal" => :build
|
||||||
<% elsif mode == :go %>
|
<% elsif @mode == :go %>
|
||||||
depends_on "go" => :build
|
depends_on "go" => :build
|
||||||
<% elsif mode == :meson %>
|
<% elsif @mode == :meson %>
|
||||||
depends_on "meson" => :build
|
depends_on "meson" => :build
|
||||||
depends_on "ninja" => :build
|
depends_on "ninja" => :build
|
||||||
<% elsif mode == :node %>
|
<% elsif @mode == :node %>
|
||||||
depends_on "node"
|
depends_on "node"
|
||||||
<% elsif mode == :perl %>
|
<% elsif @mode == :perl %>
|
||||||
uses_from_macos "perl"
|
uses_from_macos "perl"
|
||||||
<% elsif mode == :python %>
|
<% elsif @mode == :python %>
|
||||||
depends_on "python"
|
depends_on "python"
|
||||||
<% elsif mode == :ruby %>
|
<% elsif @mode == :ruby %>
|
||||||
uses_from_macos "ruby"
|
uses_from_macos "ruby"
|
||||||
<% elsif mode == :rust %>
|
<% elsif @mode == :rust %>
|
||||||
depends_on "rust" => :build
|
depends_on "rust" => :build
|
||||||
<% elsif mode.nil? %>
|
<% elsif @mode.nil? %>
|
||||||
# depends_on "cmake" => :build
|
# depends_on "cmake" => :build
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if mode == :perl %>
|
<% if @mode == :perl %>
|
||||||
# Additional dependency
|
# Additional dependency
|
||||||
# resource "" do
|
# resource "" do
|
||||||
# url ""
|
# url ""
|
||||||
@ -148,28 +150,28 @@ module Homebrew
|
|||||||
<% end %>
|
<% end %>
|
||||||
def install
|
def install
|
||||||
# ENV.deparallelize # if your formula fails when building in parallel
|
# ENV.deparallelize # if your formula fails when building in parallel
|
||||||
<% if mode == :cmake %>
|
<% if @mode == :cmake %>
|
||||||
system "cmake", "-S", ".", "-B", "build", *std_cmake_args
|
system "cmake", "-S", ".", "-B", "build", *std_cmake_args
|
||||||
system "cmake", "--build", "build"
|
system "cmake", "--build", "build"
|
||||||
system "cmake", "--install", "build"
|
system "cmake", "--install", "build"
|
||||||
<% elsif mode == :autotools %>
|
<% elsif @mode == :autotools %>
|
||||||
# Remove unrecognized options if warned by configure
|
# Remove unrecognized options if warned by configure
|
||||||
# https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
|
# https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
|
||||||
system "./configure", *std_configure_args, "--disable-silent-rules"
|
system "./configure", *std_configure_args, "--disable-silent-rules"
|
||||||
system "make", "install" # if this fails, try separate make/make install steps
|
system "make", "install" # if this fails, try separate make/make install steps
|
||||||
<% elsif mode == :crystal %>
|
<% elsif @mode == :crystal %>
|
||||||
system "shards", "build", "--release"
|
system "shards", "build", "--release"
|
||||||
bin.install "bin/#{name}"
|
bin.install "bin/#{name}"
|
||||||
<% elsif mode == :go %>
|
<% elsif @mode == :go %>
|
||||||
system "go", "build", *std_go_args(ldflags: "-s -w")
|
system "go", "build", *std_go_args(ldflags: "-s -w")
|
||||||
<% elsif mode == :meson %>
|
<% elsif @mode == :meson %>
|
||||||
system "meson", "setup", "build", *std_meson_args
|
system "meson", "setup", "build", *std_meson_args
|
||||||
system "meson", "compile", "-C", "build", "--verbose"
|
system "meson", "compile", "-C", "build", "--verbose"
|
||||||
system "meson", "install", "-C", "build"
|
system "meson", "install", "-C", "build"
|
||||||
<% elsif mode == :node %>
|
<% elsif @mode == :node %>
|
||||||
system "npm", "install", *Language::Node.std_npm_install_args(libexec)
|
system "npm", "install", *Language::Node.std_npm_install_args(libexec)
|
||||||
bin.install_symlink Dir["\#{libexec}/bin/*"]
|
bin.install_symlink Dir["\#{libexec}/bin/*"]
|
||||||
<% elsif mode == :perl %>
|
<% elsif @mode == :perl %>
|
||||||
ENV.prepend_create_path "PERL5LIB", libexec/"lib/perl5"
|
ENV.prepend_create_path "PERL5LIB", libexec/"lib/perl5"
|
||||||
ENV.prepend_path "PERL5LIB", libexec/"lib"
|
ENV.prepend_path "PERL5LIB", libexec/"lib"
|
||||||
|
|
||||||
@ -189,15 +191,15 @@ module Homebrew
|
|||||||
|
|
||||||
bin.install name
|
bin.install name
|
||||||
bin.env_script_all_files(libexec/"bin", PERL5LIB: ENV["PERL5LIB"])
|
bin.env_script_all_files(libexec/"bin", PERL5LIB: ENV["PERL5LIB"])
|
||||||
<% elsif mode == :python %>
|
<% elsif @mode == :python %>
|
||||||
virtualenv_install_with_resources
|
virtualenv_install_with_resources
|
||||||
<% elsif mode == :ruby %>
|
<% elsif @mode == :ruby %>
|
||||||
ENV["GEM_HOME"] = libexec
|
ENV["GEM_HOME"] = libexec
|
||||||
system "gem", "build", "\#{name}.gemspec"
|
system "gem", "build", "\#{name}.gemspec"
|
||||||
system "gem", "install", "\#{name}-\#{version}.gem"
|
system "gem", "install", "\#{name}-\#{@version}.gem"
|
||||||
bin.install libexec/"bin/\#{name}"
|
bin.install libexec/"bin/\#{name}"
|
||||||
bin.env_script_all_files(libexec/"bin", GEM_HOME: ENV["GEM_HOME"])
|
bin.env_script_all_files(libexec/"bin", GEM_HOME: ENV["GEM_HOME"])
|
||||||
<% elsif mode == :rust %>
|
<% elsif @mode == :rust %>
|
||||||
system "cargo", "install", *std_cargo_args
|
system "cargo", "install", *std_cargo_args
|
||||||
<% else %>
|
<% else %>
|
||||||
# Remove unrecognized options if warned by configure
|
# Remove unrecognized options if warned by configure
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user