Skip to content

Commit

Permalink
Merge pull request #15249 from dduugg/module-function-cleanup
Browse files Browse the repository at this point in the history
Refactor module_function to reduce rbi need
  • Loading branch information
MikeMcQuaid committed Apr 17, 2023
2 parents e98bfdd + df5c584 commit 8548ea1
Show file tree
Hide file tree
Showing 44 changed files with 510 additions and 694 deletions.
6 changes: 2 additions & 4 deletions Library/Homebrew/cmd/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
module Homebrew
extend T::Sig

module_function

sig { returns(CLI::Parser) }
def install_args
def self.install_args
Homebrew::CLI::Parser.new do
description <<~EOS
Install a <formula> or <cask>. Additional options specific to a <formula> may be
Expand Down Expand Up @@ -146,7 +144,7 @@ def install_args
end
end

def install
def self.install
args = install_args.parse

if args.env.present?
Expand Down
6 changes: 2 additions & 4 deletions Library/Homebrew/cmd/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
module Homebrew
extend T::Sig

module_function

sig { returns(CLI::Parser) }
def reinstall_args
def self.reinstall_args
Homebrew::CLI::Parser.new do
description <<~EOS
Uninstall and then reinstall a <formula> or <cask> using the same options it was
Expand Down Expand Up @@ -92,7 +90,7 @@ def reinstall_args
end
end

def reinstall
def self.reinstall
args = reinstall_args.parse

formulae, casks = args.named.to_formulae_and_casks(method: :resolve)
Expand Down
56 changes: 27 additions & 29 deletions Library/Homebrew/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#
# @api private
module Commands
module_function

HOMEBREW_CMD_PATH = (HOMEBREW_LIBRARY_PATH/"cmd").freeze
HOMEBREW_DEV_CMD_PATH = (HOMEBREW_LIBRARY_PATH/"dev-cmd").freeze
# If you are going to change anything in below hash,
Expand Down Expand Up @@ -38,57 +36,57 @@ module Commands
# middle due to dots in URLs or paths.
DESCRIPTION_SPLITTING_PATTERN = /\.(?>\s|$)/.freeze

def valid_internal_cmd?(cmd)
def self.valid_internal_cmd?(cmd)
require?(HOMEBREW_CMD_PATH/cmd)
end

def valid_internal_dev_cmd?(cmd)
def self.valid_internal_dev_cmd?(cmd)
require?(HOMEBREW_DEV_CMD_PATH/cmd)
end

def method_name(cmd)
def self.method_name(cmd)
cmd.to_s
.tr("-", "_")
.downcase
.to_sym
end

def args_method_name(cmd_path)
def self.args_method_name(cmd_path)
cmd_path_basename = basename_without_extension(cmd_path)
cmd_method_prefix = method_name(cmd_path_basename)
"#{cmd_method_prefix}_args".to_sym
end

def internal_cmd_path(cmd)
def self.internal_cmd_path(cmd)
[
HOMEBREW_CMD_PATH/"#{cmd}.rb",
HOMEBREW_CMD_PATH/"#{cmd}.sh",
].find(&:exist?)
end

def internal_dev_cmd_path(cmd)
def self.internal_dev_cmd_path(cmd)
[
HOMEBREW_DEV_CMD_PATH/"#{cmd}.rb",
HOMEBREW_DEV_CMD_PATH/"#{cmd}.sh",
].find(&:exist?)
end

# Ruby commands which can be `require`d without being run.
def external_ruby_v2_cmd_path(cmd)
def self.external_ruby_v2_cmd_path(cmd)
path = which("#{cmd}.rb", Tap.cmd_directories)
path if require?(path)
end

# Ruby commands which are run by being `require`d.
def external_ruby_cmd_path(cmd)
def self.external_ruby_cmd_path(cmd)
which("brew-#{cmd}.rb", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
end

def external_cmd_path(cmd)
def self.external_cmd_path(cmd)
which("brew-#{cmd}", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
end

def path(cmd)
def self.path(cmd)
internal_cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
path ||= internal_cmd_path(internal_cmd)
path ||= internal_dev_cmd_path(internal_cmd)
Expand All @@ -98,48 +96,48 @@ def path(cmd)
path
end

def commands(external: true, aliases: false)
def self.commands(external: true, aliases: false)
cmds = internal_commands
cmds += internal_developer_commands
cmds += external_commands if external
cmds += internal_commands_aliases if aliases
cmds.sort
end

def internal_commands_paths
def self.internal_commands_paths
find_commands HOMEBREW_CMD_PATH
end

def internal_developer_commands_paths
def self.internal_developer_commands_paths
find_commands HOMEBREW_DEV_CMD_PATH
end

def official_external_commands_paths(quiet:)
def self.official_external_commands_paths(quiet:)
OFFICIAL_CMD_TAPS.flat_map do |tap_name, cmds|
tap = Tap.fetch(tap_name)
tap.install(quiet: quiet) unless tap.installed?
cmds.map(&method(:external_ruby_v2_cmd_path)).compact
end
end

def internal_commands
def self.internal_commands
find_internal_commands(HOMEBREW_CMD_PATH).map(&:to_s)
end

def internal_developer_commands
def self.internal_developer_commands
find_internal_commands(HOMEBREW_DEV_CMD_PATH).map(&:to_s)
end

def internal_commands_aliases
def self.internal_commands_aliases
HOMEBREW_INTERNAL_COMMAND_ALIASES.keys
end

def find_internal_commands(path)
def self.find_internal_commands(path)
find_commands(path).map(&:basename)
.map(&method(:basename_without_extension))
end

def external_commands
def self.external_commands
Tap.cmd_directories.flat_map do |path|
find_commands(path).select(&:executable?)
.map(&method(:basename_without_extension))
Expand All @@ -148,25 +146,25 @@ def external_commands
.sort
end

def basename_without_extension(path)
def self.basename_without_extension(path)
path.basename(path.extname)
end

def find_commands(path)
def self.find_commands(path)
Pathname.glob("#{path}/*")
.select(&:file?)
.sort
end

def rebuild_internal_commands_completion_list
def self.rebuild_internal_commands_completion_list
cmds = internal_commands + internal_developer_commands + internal_commands_aliases
cmds.reject! { |cmd| Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST.include? cmd }

file = HOMEBREW_REPOSITORY/"completions/internal_commands_list.txt"
file.atomic_write("#{cmds.sort.join("\n")}\n")
end

def rebuild_commands_completion_list
def self.rebuild_commands_completion_list
# Ensure that the cache exists so we can build the commands list
HOMEBREW_CACHE.mkpath

Expand All @@ -178,7 +176,7 @@ def rebuild_commands_completion_list
external_commands_file.atomic_write("#{external_commands.sort.join("\n")}\n")
end

def command_options(command)
def self.command_options(command)
path = self.path(command)
return if path.blank?

Expand All @@ -202,7 +200,7 @@ def command_options(command)
end
end

def command_description(command, short: false)
def self.command_description(command, short: false)
path = self.path(command)
return if path.blank?

Expand All @@ -228,7 +226,7 @@ def command_description(command, short: false)
end
end

def named_args_type(command)
def self.named_args_type(command)
path = self.path(command)
return if path.blank?

Expand All @@ -239,7 +237,7 @@ def named_args_type(command)
end

# Returns the conflicts of a given `option` for `command`.
def option_conflicts(command, option)
def self.option_conflicts(command, option)
path = self.path(command)
return if path.blank?

Expand Down
5 changes: 0 additions & 5 deletions Library/Homebrew/commands.rbi

This file was deleted.

34 changes: 16 additions & 18 deletions Library/Homebrew/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ module Completions
keyword_init: true,
)

module_function

COMPLETIONS_DIR = (HOMEBREW_REPOSITORY/"completions").freeze
TEMPLATE_DIR = (HOMEBREW_LIBRARY_PATH/"completions").freeze

Expand Down Expand Up @@ -74,15 +72,15 @@ module Completions
}.freeze

sig { void }
def link!
def self.link!
Settings.write :linkcompletions, true
Tap.each do |tap|
Utils::Link.link_completions tap.path, "brew completions link"
end
end

sig { void }
def unlink!
def self.unlink!
Settings.write :linkcompletions, false
Tap.each do |tap|
next if tap.official?
Expand All @@ -92,12 +90,12 @@ def unlink!
end

sig { returns(T::Boolean) }
def link_completions?
def self.link_completions?
Settings.read(:linkcompletions) == "true"
end

sig { returns(T::Boolean) }
def completions_to_link?
def self.completions_to_link?
Tap.each do |tap|
next if tap.official?

Expand All @@ -110,7 +108,7 @@ def completions_to_link?
end

sig { void }
def show_completions_message_if_needed
def self.show_completions_message_if_needed
return if Settings.read(:completionsmessageshown) == "true"
return unless completions_to_link?

Expand All @@ -125,7 +123,7 @@ def show_completions_message_if_needed
end

sig { void }
def update_shell_completions!
def self.update_shell_completions!
commands = Commands.commands(external: false, aliases: true).sort

puts "Writing completions to #{COMPLETIONS_DIR}"
Expand All @@ -136,12 +134,12 @@ def update_shell_completions!
end

sig { params(command: String).returns(T::Boolean) }
def command_gets_completions?(command)
def self.command_gets_completions?(command)
command_options(command).any?
end

sig { params(description: String, fish: T::Boolean).returns(String) }
def format_description(description, fish: false)
def self.format_description(description, fish: false)
description = if fish
description.gsub("'", "\\\\'")
else
Expand All @@ -151,7 +149,7 @@ def format_description(description, fish: false)
end

sig { params(command: String).returns(T::Hash[String, String]) }
def command_options(command)
def self.command_options(command)
options = {}
Commands.command_options(command)&.each do |option|
next if option.blank?
Expand All @@ -169,7 +167,7 @@ def command_options(command)
end

sig { params(command: String).returns(T.nilable(String)) }
def generate_bash_subcommand_completion(command)
def self.generate_bash_subcommand_completion(command)
return unless command_gets_completions? command

named_completion_string = ""
Expand Down Expand Up @@ -202,7 +200,7 @@ def generate_bash_subcommand_completion(command)
end

sig { params(commands: T::Array[String]).returns(String) }
def generate_bash_completion_file(commands)
def self.generate_bash_completion_file(commands)
variables = Variables.new(
completion_functions: commands.map do |command|
generate_bash_subcommand_completion command
Expand All @@ -218,7 +216,7 @@ def generate_bash_completion_file(commands)
end

sig { params(command: String).returns(T.nilable(String)) }
def generate_zsh_subcommand_completion(command)
def self.generate_zsh_subcommand_completion(command)
return unless command_gets_completions? command

options = command_options(command)
Expand Down Expand Up @@ -270,15 +268,15 @@ def generate_zsh_subcommand_completion(command)
COMPLETION
end

def generate_zsh_option_exclusions(command, option)
def self.generate_zsh_option_exclusions(command, option)
conflicts = Commands.option_conflicts(command, option.gsub(/^--/, ""))
return "" unless conflicts.presence

"(#{conflicts.map { |conflict| "--#{conflict}" }.join(" ")})"
end

sig { params(commands: T::Array[String]).returns(String) }
def generate_zsh_completion_file(commands)
def self.generate_zsh_completion_file(commands)
variables = Variables.new(
aliases: Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_command, command|
alias_command = "'#{alias_command}'" if alias_command.start_with? "-"
Expand All @@ -305,7 +303,7 @@ def generate_zsh_completion_file(commands)
end

sig { params(command: String).returns(T.nilable(String)) }
def generate_fish_subcommand_completion(command)
def self.generate_fish_subcommand_completion(command)
return unless command_gets_completions? command

command_description = format_description Commands.command_description(command, short: true), fish: true
Expand Down Expand Up @@ -352,7 +350,7 @@ def generate_fish_subcommand_completion(command)
end

sig { params(commands: T::Array[String]).returns(String) }
def generate_fish_completion_file(commands)
def self.generate_fish_completion_file(commands)
variables = Variables.new(
completion_functions: commands.map do |command|
generate_fish_subcommand_completion command
Expand Down

0 comments on commit 8548ea1

Please sign in to comment.