Skip to content

Commit

Permalink
Avoid re-defining #formula?
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Dec 6, 2024
1 parent 6352439 commit bbb9bd3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
12 changes: 9 additions & 3 deletions Library/Homebrew/cli/args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ def build_bottle? = false
sig { returns(T::Boolean) }
def build_from_source? = false

sig { returns(T::Boolean) }
def cask? = false

sig { returns(T::Boolean) }
def force_bottle? = false

# Defined in extend/os:
# def formula; end

sig { returns(T::Boolean) }
def HEAD? = false

Expand Down Expand Up @@ -122,9 +128,9 @@ def context

sig { returns(T.nilable(Symbol)) }
def only_formula_or_cask
if invoke_if_respond_to(:formula?) && !invoke_if_respond_to(:cask?)
if formula? && !cask?
:formula
elsif invoke_if_respond_to(:cask?) && !invoke_if_respond_to(:formula?)
elsif cask? && !formula?
:cask
end
end
Expand Down Expand Up @@ -194,7 +200,7 @@ def cli_args

# FIXME: This is here for compatibility with the previous implementation that used OpenStruct.
# This is for args that need to determine whether they have been set or not, and thus may not support a default
# implementation. (The Linux override of `set_default_options` might be one example.)
# implementation.
sig { params(method: Symbol).returns(T.untyped) }
def invoke_if_respond_to(method)
public_send(method) if respond_to?(method)
Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,7 @@ def parse(argv = ARGV.freeze, ignore_invalid_options: false)
end

unless ignore_invalid_options
unless @is_dev_cmd
set_default_options
validate_options
end
validate_options unless @is_dev_cmd
check_constraint_violations
check_named_args(named_args)
end
Expand All @@ -416,9 +413,6 @@ def parse(argv = ARGV.freeze, ignore_invalid_options: false)
@args
end

sig { void }
def set_default_options; end

sig { void }
def validate_options; end

Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/extend/os/linux/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ module Parser

requires_ancestor { Homebrew::CLI::Parser }

sig { void }
def set_default_options
args.define_singleton_method(:formula?) { true } if args.respond_to?(:formula?)
end

sig { void }
def validate_options
return unless args.respond_to?(:cask?)
return unless T.unsafe(args).cask?
return unless args.cask?

# NOTE: We don't raise an error here because we don't want
# to print the help page or a stack trace.
Expand Down
9 changes: 9 additions & 0 deletions Library/Homebrew/extend/os/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
# frozen_string_literal: true

require "extend/os/linux/cli/parser" if OS.linux?

module Homebrew
module CLI
class Args
sig { returns(T::Boolean) }
def formula? = OS.linux?
end
end
end

0 comments on commit bbb9bd3

Please sign in to comment.