Port prof to use AbstractCommand

This commit is contained in:
Douglas Eichelberger 2024-03-04 10:29:47 -08:00
parent 5364b6ef03
commit 2dceb65b42
2 changed files with 59 additions and 59 deletions

View File

@ -1,65 +1,67 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "cli/parser" require "cli/parser"
module Homebrew module Homebrew
module_function module DevCmd
class Prof < AbstractCommand
sig { override.returns(CLI::Parser) }
def raw_args
Homebrew::CLI::Parser.new do
description <<~EOS
Run Homebrew with a Ruby profiler. For example, `brew prof readall`.
EOS
switch "--stackprof",
description: "Use `stackprof` instead of `ruby-prof` (the default)."
sig { returns(CLI::Parser) } named_args :command, min: 1
def prof_args end
Homebrew::CLI::Parser.new do end
description <<~EOS
Run Homebrew with a Ruby profiler. For example, `brew prof readall`. sig { override.void }
EOS def run
switch "--stackprof", Homebrew.install_bundler_gems!(groups: ["prof"], setup_path: false)
description: "Use `stackprof` instead of `ruby-prof` (the default)."
brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path
named_args :command, min: 1 FileUtils.mkdir_p "prof"
end cmd = args.named.first
end
case Commands.path(cmd)&.extname
def prof when ".rb"
args = prof_args.parse # expected file extension so we do nothing
when ".sh"
Homebrew.install_bundler_gems!(groups: ["prof"], setup_path: false) raise UsageError, <<~EOS
`#{cmd}` is a Bash command!
brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path Try `hyperfine` for benchmarking instead.
FileUtils.mkdir_p "prof" EOS
cmd = args.named.first else
raise UsageError, "`#{cmd}` is an unknown command!"
case Commands.path(cmd)&.extname end
when ".rb"
# expected file extension so we do nothing Homebrew.setup_gem_environment!
when ".sh"
raise UsageError, <<~EOS if args[:stackprof?]
`#{cmd}` is a Bash command! with_env HOMEBREW_STACKPROF: "1" do
Try `hyperfine` for benchmarking instead. system(*HOMEBREW_RUBY_EXEC_ARGS, brew_rb, *args.named)
EOS end
else output_filename = "prof/d3-flamegraph.html"
raise UsageError, "`#{cmd}` is an unknown command!" safe_system "stackprof --d3-flamegraph prof/stackprof.dump > #{output_filename}"
end else
output_filename = "prof/call_stack.html"
Homebrew.setup_gem_environment! safe_system "ruby-prof", "--printer=call_stack", "--file=#{output_filename}", brew_rb, "--", *args.named
end
if args.stackprof?
with_env HOMEBREW_STACKPROF: "1" do exec_browser output_filename
system(*HOMEBREW_RUBY_EXEC_ARGS, brew_rb, *args.named) rescue OptionParser::InvalidOption => e
ofail e
# The invalid option could have been meant for the subcommand.
# Suggest `brew prof list -r` -> `brew prof -- list -r`
args = ARGV - ["--"]
puts "Try `brew prof -- #{args.join(" ")}` instead."
end end
output_filename = "prof/d3-flamegraph.html"
safe_system "stackprof --d3-flamegraph prof/stackprof.dump > #{output_filename}"
else
output_filename = "prof/call_stack.html"
safe_system "ruby-prof", "--printer=call_stack", "--file=#{output_filename}", brew_rb, "--", *args.named
end end
exec_browser output_filename
rescue OptionParser::InvalidOption => e
ofail e
# The invalid option could have been meant for the subcommand.
# Suggest `brew prof list -r` -> `brew prof -- list -r`
args = ARGV - ["--"]
puts "Try `brew prof -- #{args.join(" ")}` instead."
end end
end end

View File

@ -1,10 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "dev-cmd/prof"
RSpec.describe "brew prof" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::Prof do
describe "integration tests", :integration_test, :needs_network do describe "integration tests", :integration_test, :needs_network do
after do after do
FileUtils.rm_rf HOMEBREW_LIBRARY_PATH/"prof" FileUtils.rm_rf HOMEBREW_LIBRARY_PATH/"prof"