Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formula_creator: move initial CLI values into constructor #16260

Merged
merged 19 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 19 additions & 16 deletions Library/Homebrew/dev-cmd/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,7 @@
end

def create_formula(args:)
fc = FormulaCreator.new(args)
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?
mode = if args.autotools?
:autotools
elsif args.cmake?
:cmake
Expand All @@ -177,6 +162,24 @@
:rust
end

fc = FormulaCreator.new(
args.set_name,
args.set_version,
tap: args.tap,
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

Check warning on line 177 in Library/Homebrew/dev-cmd/create.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/create.rb#L176-L177

Added lines #L176 - L177 were not covered by tests
end
raise TapUnavailableError, fc.tap.name unless fc.tap.installed?

fc.url = args.named.first

# Check for disallowed formula, or names that shadow aliases,
# unless --force is specified.
unless args.force?
Expand Down
100 changes: 49 additions & 51 deletions Library/Homebrew/formula_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ module Homebrew
#
# @api private
class FormulaCreator
attr_reader :args, :url, :sha256, :desc, :homepage
attr_accessor :name, :version, :tap, :mode, :license

def initialize(args)
@args = args
attr_reader :url, :tap, :sha256, :desc, :homepage
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
attr_accessor :name

sig {
params(name: T.nilable(String), version: T.nilable(String), tap: T.nilable(String), mode: T.nilable(Symbol),
license: T.nilable(String), fetch: T::Boolean, head: T::Boolean).void
}
def initialize(name, version, tap:, mode:, license:, fetch: true, head: false)
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
@name = name
@version = Version.new(version) if version
@tap = Tap.fetch(tap || "homebrew/core")
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
@mode = mode
@license = license
@fetch = fetch
@head = head
end

def url=(url)
Expand All @@ -34,19 +44,7 @@ def url=(url)
@name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1]
end
end
@version = if @version
Version.new(@version)
else
Version.detect(url)
end
end

def fetch?
!args.no_fetch?
end

def head?
@head || args.HEAD?
@version = Version.detect(url) if @version.nil?
end

def write_formula!
Expand All @@ -56,13 +54,13 @@ def write_formula!
path = @tap.new_formula_path(@name)
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."
elsif fetch?
unless head?
elsif @fetch
unless @head
r = Resource.new
r.url(url)
r.version(version)
r.version(@version)
r.owner = self
@sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy
end
Expand Down Expand Up @@ -92,53 +90,53 @@ def template
# Documentation: https://docs.brew.sh/Formula-Cookbook
# https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
<% if mode == :node %>
<% if @mode == :node %>
require "language/node"

<% end %>
class #{Formulary.class_s(name)} < Formula
<% if mode == :python %>
<% if @mode == :python %>
include Language::Python::Virtualenv

<% end %>
desc "#{desc}"
homepage "#{homepage}"
<% unless head? %>
<% unless @head %>
url "#{url}"
<% unless version.detected_from_url? %>
version "#{version}"
<% unless @version.detected_from_url? %>
version "#{@version}"
<% end %>
sha256 "#{sha256}"
<% end %>
license "#{license}"
<% if head? %>
license "#{@license}"
<% if @head %>
head "#{url}"
<% end %>

<% if mode == :cmake %>
<% if @mode == :cmake %>
depends_on "cmake" => :build
<% elsif mode == :crystal %>
<% elsif @mode == :crystal %>
depends_on "crystal" => :build
<% elsif mode == :go %>
<% elsif @mode == :go %>
depends_on "go" => :build
<% elsif mode == :meson %>
<% elsif @mode == :meson %>
depends_on "meson" => :build
depends_on "ninja" => :build
<% elsif mode == :node %>
<% elsif @mode == :node %>
depends_on "node"
<% elsif mode == :perl %>
<% elsif @mode == :perl %>
uses_from_macos "perl"
<% elsif mode == :python %>
<% elsif @mode == :python %>
depends_on "python"
<% elsif mode == :ruby %>
<% elsif @mode == :ruby %>
uses_from_macos "ruby"
<% elsif mode == :rust %>
<% elsif @mode == :rust %>
depends_on "rust" => :build
<% elsif mode.nil? %>
<% elsif @mode.nil? %>
# depends_on "cmake" => :build
<% end %>

<% if mode == :perl %>
<% if @mode == :perl %>
# Additional dependency
# resource "" do
# url ""
Expand All @@ -148,28 +146,28 @@ class #{Formulary.class_s(name)} < Formula
<% end %>
def install
# 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", "--build", "build"
system "cmake", "--install", "build"
<% elsif mode == :autotools %>
<% elsif @mode == :autotools %>
# Remove unrecognized options if warned by configure
# https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
system "./configure", *std_configure_args, "--disable-silent-rules"
system "make", "install" # if this fails, try separate make/make install steps
<% elsif mode == :crystal %>
<% elsif @mode == :crystal %>
system "shards", "build", "--release"
bin.install "bin/#{name}"
<% elsif mode == :go %>
<% elsif @mode == :go %>
system "go", "build", *std_go_args(ldflags: "-s -w")
<% elsif mode == :meson %>
<% elsif @mode == :meson %>
system "meson", "setup", "build", *std_meson_args
system "meson", "compile", "-C", "build", "--verbose"
system "meson", "install", "-C", "build"
<% elsif mode == :node %>
<% elsif @mode == :node %>
system "npm", "install", *Language::Node.std_npm_install_args(libexec)
bin.install_symlink Dir["\#{libexec}/bin/*"]
<% elsif mode == :perl %>
<% elsif @mode == :perl %>
ENV.prepend_create_path "PERL5LIB", libexec/"lib/perl5"
ENV.prepend_path "PERL5LIB", libexec/"lib"

Expand All @@ -189,15 +187,15 @@ def install

bin.install name
bin.env_script_all_files(libexec/"bin", PERL5LIB: ENV["PERL5LIB"])
<% elsif mode == :python %>
<% elsif @mode == :python %>
virtualenv_install_with_resources
<% elsif mode == :ruby %>
<% elsif @mode == :ruby %>
ENV["GEM_HOME"] = libexec
system "gem", "build", "\#{name}.gemspec"
system "gem", "install", "\#{name}-\#{version}.gem"
system "gem", "install", "\#{name}-\#{@version}.gem"
bin.install libexec/"bin/\#{name}"
bin.env_script_all_files(libexec/"bin", GEM_HOME: ENV["GEM_HOME"])
<% elsif mode == :rust %>
<% elsif @mode == :rust %>
system "cargo", "install", *std_cargo_args
<% else %>
# Remove unrecognized options if warned by configure
Expand Down