# typed: strict
# frozen_string_literal: true

require "abstract_command"
require "fileutils"

module Homebrew
  module DevCmd
    class Prof < AbstractCommand
      cmd_args 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)."
        switch "--vernier",
               description: "Use `vernier` instead of `ruby-prof` (the default)."
        switch "--timings",
               description: "Record machine-readable timings for Homebrew command phases."
        conflicts "--timings", "--stackprof"
        conflicts "--timings", "--vernier"

        named_args :command, min: 1
      end

      sig { override.void }
      def run
        Homebrew.install_bundler_gems!(groups: ["prof"], setup_path: false) unless args.timings?

        brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path
        FileUtils.mkdir_p "prof"
        cmd = T.must(args.named.first)

        case Commands.path(cmd)&.extname
        when ".rb"
          # expected file extension so we do nothing
        when ".sh"
          raise UsageError, <<~EOS
            `#{cmd}` is a Bash command!
            Try `hyperfine` for benchmarking instead.
          EOS
        else
          raise UsageError, "`#{cmd}` is an unknown command!"
        end

        if args.timings?
          output_filename = "prof/timings.json"
          safe_system({ "HOMEBREW_PHASE_TIMINGS" => output_filename },
                      *HOMEBREW_RUBY_EXEC_ARGS, brew_rb, *args.named)
          ohai "Phase timings written to #{output_filename}"
          return
        end

        Homebrew.setup_gem_environment!

        if args.stackprof?
          with_env HOMEBREW_STACKPROF: "1" do
            system(*HOMEBREW_RUBY_EXEC_ARGS, brew_rb, *args.named)
          end
          output_filename = "prof/d3-flamegraph.html"
          safe_system "stackprof --d3-flamegraph prof/stackprof.dump > #{output_filename}"
          # `brew prof` is often run from tests or scripts. Only open the HTML
          # report automatically when the user is attached to a terminal.
          exec_browser output_filename if $stdout.tty?
        elsif args.vernier?
          output_filename = "prof/vernier.json"
          Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid
          # Avoid `vernier run`: it injects `vernier/autorun` through `RUBYOPT`,
          # which child Ruby processes inherit. Profiling only this Ruby process
          # keeps nested `brew` commands from trying to write the same profile.
          #
          # `HOMEBREW_SPAWN_SYSTEM` is intentionally scoped to this profiled
          # process. It lets selected process helpers avoid manual fork paths
          # that can inherit Vernier's active native collector state.
          safe_system({ "HOMEBREW_SPAWN_SYSTEM" => "1",
                        "VERNIER_ALLOCATION_INTERVAL" => "500", "VERNIER_OUTPUT" => output_filename },
                      RUBY_PATH, "-I", (Pathname(Gem::Specification.find_by_name("vernier").full_gem_path)/"lib").to_s,
                      "-r", "vernier/autorun",
                      "-r", (HOMEBREW_LIBRARY_PATH/"prof/vernier_fork_guard").to_s, brew_rb, *args.named)
          ohai "Profiling complete!"
          puts "Upload the results from #{output_filename} to:"
          puts "  #{Formatter.url("https://vernier.prof")}"
        else
          output_filename = "prof/call_stack.html"
          safe_system "ruby-prof", "--printer=call_stack", "--file=#{output_filename}", brew_rb, "--", *args.named
          # Match the stackprof behaviour above: generating the file is useful
          # in non-interactive runs but launching a browser is not.
          exec_browser output_filename if $stdout.tty?
        end
      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
