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

Refactor commands to remove 'require "cmd/*"' #4286

Merged
merged 1 commit into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Library/Homebrew/brew.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
# - no arguments are passed
# - if cmd is Cask, let Cask handle the help command instead
if (empty_argv || help_flag) && cmd != "cask"
require "cmd/help"
Homebrew.help cmd, empty_argv: empty_argv
require "help"
Homebrew::Help.help cmd, empty_argv: empty_argv
# `Homebrew.help` never returns, except for external/unknown commands.
end

Expand Down Expand Up @@ -119,8 +119,8 @@
exec HOMEBREW_BREW_FILE, cmd, *ARGV
end
rescue UsageError => e
require "cmd/help"
Homebrew.help cmd, usage_error: e.message
require "help"
Homebrew::Help.help cmd, usage_error: e.message
rescue SystemExit => e
onoe "Kernel.exit" if ARGV.verbose? && !e.success?
$stderr.puts e.backtrace if ARGV.debug?
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cmd/--cache.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#: * `--cache`:
#: Display Homebrew's download cache. See also `HOMEBREW_CACHE`.
#:
#: * `--cache` <formula>:
#: * `--cache` [`--build-from-source`|`-s`] [`--force-bottle`] <formula>:
#: Display the file or directory used to cache <formula>.

require "cmd/fetch"
require "fetch"

module Homebrew
module_function
Expand All @@ -14,7 +14,7 @@ def __cache
puts HOMEBREW_CACHE
else
ARGV.formulae.each do |f|
if fetch_bottle?(f)
if Fetch.fetch_bottle?(f)
puts f.bottle.cached_download
else
puts f.cached_download
Expand Down
11 changes: 2 additions & 9 deletions Library/Homebrew/cmd/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#: installation.

require "formula"
require "fetch"

module Homebrew
module_function
Expand All @@ -46,7 +47,7 @@ def fetch
f.print_tap_action verb: "Fetching"

fetched_bottle = false
if fetch_bottle?(f)
if Fetch.fetch_bottle?(f)
begin
fetch_formula(f.bottle)
rescue Interrupt
Expand All @@ -73,14 +74,6 @@ def fetch
end
end

def fetch_bottle?(f)
return true if ARGV.force_bottle? && f.bottle
return false unless f.bottle && f.pour_bottle?
return false if ARGV.build_formula_from_source?(f)
return false unless f.bottle.compatible_cellar?
true
end

def fetch_resource(r)
puts "Resource: #{r.name}"
fetch_fetchable r
Expand Down
88 changes: 2 additions & 86 deletions Library/Homebrew/cmd/help.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,7 @@
HOMEBREW_HELP = <<~EOS.freeze
Example usage:
brew search [TEXT|/REGEX/]
brew info [FORMULA...]
brew install FORMULA...
brew update
brew upgrade [FORMULA...]
brew uninstall FORMULA...
brew list [FORMULA...]

Troubleshooting:
brew config
brew doctor
brew install --verbose --debug FORMULA

Contributing:
brew create [URL [--no-fetch]]
brew edit [FORMULA...]

Further help:
brew commands
brew help [COMMAND]
man brew
https://docs.brew.sh
EOS

# NOTE Keep the lenth of vanilla --help less than 25 lines!
# This is because the default Terminal height is 25 lines. Scrolling sucks
# and concision is important. If more help is needed we should start
# specialising help like the gem command does.
# NOTE Keep lines less than 80 characters! Wrapping is just not cricket.
# NOTE The reason the string is at the top is so 25 lines is easy to measure!

require "commands"
require "help"

module Homebrew
module_function

def help(cmd = nil, flags = {})
# Resolve command aliases and find file containing the implementation.
if cmd
cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
path = Commands.path(cmd)
path ||= which("brew-#{cmd}")
path ||= which("brew-#{cmd}.rb")
end

# Display command-specific (or generic) help in response to `UsageError`.
if (error_message = flags[:usage_error])
$stderr.puts path ? command_help(path) : HOMEBREW_HELP
$stderr.puts
onoe error_message
exit 1
end

# Handle `brew` (no arguments).
if flags[:empty_argv]
$stderr.puts HOMEBREW_HELP
exit 1
end

# Handle `brew (-h|--help|--usage|-?|help)` (no other arguments).
if cmd.nil?
puts HOMEBREW_HELP
exit 0
end

# Resume execution in `brew.rb` for unknown commands.
return if path.nil?

# Display help for internal command (or generic help if undocumented).
puts command_help(path)
exit 0
end

def command_help(path)
help_lines = command_help_lines(path)
if help_lines.empty?
opoo "No help text in: #{path}" if ARGV.homebrew_developer?
HOMEBREW_HELP
else
help_lines.map do |line|
line.sub(/^ \* /, "#{Tty.bold}brew#{Tty.reset} ")
.gsub(/`(.*?)`/, "#{Tty.bold}\\1#{Tty.reset}")
.gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
.gsub(/<(.*?)>/, "#{Tty.underline}\\1#{Tty.reset}")
.gsub("@hide_from_man_page", "")
end.join.strip
end
Help.help(cmd, flags)
end
end
46 changes: 2 additions & 44 deletions Library/Homebrew/cmd/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@
#: creating patches to the software.

require "missing_formula"
require "diagnostic"
require "cmd/search"
require "formula_installer"
require "hardware"
require "development_tools"
require "install"

module Homebrew
module_function
Expand Down Expand Up @@ -238,7 +237,7 @@ def install
end

return if formulae.empty?
perform_preinstall_checks
Install.perform_preinstall_checks

formulae.each do |f|
Migrator.migrate_if_needed(f)
Expand Down Expand Up @@ -298,47 +297,6 @@ def install
end
end

def check_ppc
case Hardware::CPU.type
when :ppc
abort <<~EOS
Sorry, Homebrew does not support your computer's CPU architecture.
For PPC support, see: https://github.com/mistydemeo/tigerbrew
EOS
end
end

def check_writable_install_location
raise "Cannot write to #{HOMEBREW_CELLAR}" if HOMEBREW_CELLAR.exist? && !HOMEBREW_CELLAR.writable_real?
raise "Cannot write to #{HOMEBREW_PREFIX}" unless HOMEBREW_PREFIX.writable_real? || HOMEBREW_PREFIX.to_s == "/usr/local"
end

def check_development_tools
checks = Diagnostic::Checks.new
checks.fatal_development_tools_checks.each do |check|
out = checks.send(check)
next if out.nil?
ofail out
end
exit 1 if Homebrew.failed?
end

def check_cellar
FileUtils.mkdir_p HOMEBREW_CELLAR unless File.exist? HOMEBREW_CELLAR
rescue
raise <<~EOS
Could not create #{HOMEBREW_CELLAR}
Check you have permission to write to #{HOMEBREW_CELLAR.parent}
EOS
end

def perform_preinstall_checks
check_ppc
check_writable_install_location
check_development_tools if DevelopmentTools.installed?
check_cellar
end

def install_formula(f)
f.print_tap_action
build_options = f.build
Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/cmd/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def link
mode.dry_run = true if ARGV.dry_run?

ARGV.kegs.each do |keg|
keg_only = keg_only?(keg.rack)
keg_only = Formulary.keg_only?(keg.rack)
if HOMEBREW_PREFIX.to_s == "/usr/local" && keg_only &&
keg.name.start_with?("openssl", "libressl")
opoo <<~EOS
Expand Down Expand Up @@ -87,10 +87,4 @@ def puts_keg_only_path_message(keg)
puts " #{Utils::Shell.prepend_path_in_profile(opt/"bin")}" if bin.directory?
puts " #{Utils::Shell.prepend_path_in_profile(opt/"sbin")}" if sbin.directory?
end

def keg_only?(rack)
Formulary.from_rack(rack).keg_only?
rescue FormulaUnavailableError, TapFormulaAmbiguityError, TapFormulaWithOldnameAmbiguityError
false
end
end
Loading