<commit_before>#:  * `man` [`--fail-if-changed`]:
#:    Generate Homebrew's manpages.
#:
#:    If `--fail-if-changed` is passed, the command will return a failing
#:    status code if changes are detected in the manpage outputs.
#:    This can be used for CI to be notified when the manpages are out of date.
#:    Additionally, the date used in new manpages will match those in the existing
#:    manpages (to allow comparison without factoring in the date).

require "formula"
require "erb"
require "ostruct"
require "cli_parser"
require "dev-cmd/audit"
require "dev-cmd/bottle"
require "dev-cmd/bump-formula-pr"
require "dev-cmd/create"
require "dev-cmd/edit"
require "dev-cmd/formula"
require "dev-cmd/irb"
require "dev-cmd/linkage"
require "dev-cmd/mirror"
require "dev-cmd/pull"
require "dev-cmd/extract"
require "dev-cmd/release-notes"
require "dev-cmd/tap-new"
require "dev-cmd/tests"
require "dev-cmd/update-test"

module Homebrew
  module_function

  SOURCE_PATH = HOMEBREW_LIBRARY_PATH/"manpages"
  TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"manpages"
  TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs"

  def man_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `man` [<options>]:

        Generate Homebrew's manpages.
      EOS
      switch "--fail-if-changed",
        description: "Return a failing status code if changes are detected in the manpage outputs. This "\
                     "can be used for CI to be notified when the manpages are out of date. Additionally, "\
                     "the date used in new manpages will match those in the existing manpages (to allow "\
                     "comparison without factoring in the date)."
      switch "--link",
        description: "It is now done automatically by `brew update`."
    end
  end

  def man
    man_args.parse

    raise UsageError unless ARGV.named.empty?

    if args.link?
      odie "`brew man --link` is now done automatically by `brew update`."
    end

    regenerate_man_pages

    if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/Manpage.md", "manpages"
      puts "No changes to manpage output detected."
    elsif args.fail_if_changed?
      Homebrew.failed = true
    end
  end

  def regenerate_man_pages
    Homebrew.install_gem_setup_path! "ronn"

    markup = build_man_page
    convert_man_page(markup, TARGET_DOC_PATH/"Manpage.md")
    convert_man_page(markup, TARGET_MAN_PATH/"brew.1")

    cask_markup = (SOURCE_PATH/"brew-cask.1.md").read
    convert_man_page(cask_markup, TARGET_MAN_PATH/"brew-cask.1")
  end

  def path_glob_commands(glob)
    Pathname.glob(glob)
            .map(&:read).map(&:lines)
            .map { |lines| lines.grep(/^#:/).map { |line| line.slice(2..-1) }.join }
            .reject { |s| s.strip.empty? || s.include?("@hide_from_man_page") }
  end

  def build_man_page
    template = (SOURCE_PATH/"brew.1.md.erb").read
    variables = OpenStruct.new
<commit_msg>restore sorting of options after commands<commit_after>#:  * `man` [`--fail-if-changed`]:
#:    Generate Homebrew's manpages.
#:
#:    If `--fail-if-changed` is passed, the command will return a failing
#:    status code if changes are detected in the manpage outputs.
#:    This can be used for CI to be notified when the manpages are out of date.
#:    Additionally, the date used in new manpages will match those in the existing
#:    manpages (to allow comparison without factoring in the date).

require "formula"
require "erb"
require "ostruct"
require "cli_parser"
require "dev-cmd/audit"
require "dev-cmd/bottle"
require "dev-cmd/bump-formula-pr"
require "dev-cmd/create"
require "dev-cmd/edit"
require "dev-cmd/formula"
require "dev-cmd/irb"
require "dev-cmd/linkage"
require "dev-cmd/mirror"
require "dev-cmd/pull"
require "dev-cmd/extract"
require "dev-cmd/release-notes"
require "dev-cmd/tap-new"
require "dev-cmd/tests"
require "dev-cmd/update-test"

module Homebrew
  module_function

  SOURCE_PATH = HOMEBREW_LIBRARY_PATH/"manpages"
  TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"manpages"
  TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs"

  def man_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `man` [<options>]:

        Generate Homebrew's manpages.
      EOS
      switch "--fail-if-changed",
        description: "Return a failing status code if changes are detected in the manpage outputs. This "\
                     "can be used for CI to be notified when the manpages are out of date. Additionally, "\
                     "the date used in new manpages will match those in the existing manpages (to allow "\
                     "comparison without factoring in the date)."
      switch "--link",
        description: "It is now done automatically by `brew update`."
    end
  end

  def man
    man_args.parse

    raise UsageError unless ARGV.named.empty?

    if args.link?
      odie "`brew man --link` is now done automatically by `brew update`."
    end

    regenerate_man_pages

    if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/Manpage.md", "manpages"
      puts "No changes to manpage output detected."
    elsif args.fail_if_changed?
      Homebrew.failed = true
    end
  end

  def regenerate_man_pages
    Homebrew.install_gem_setup_path! "ronn"

    markup = build_man_page
    convert_man_page(markup, TARGET_DOC_PATH/"Manpage.md")
    convert_man_page(markup, TARGET_MAN_PATH/"brew.1")

    cask_markup = (SOURCE_PATH/"brew-cask.1.md").read
    convert_man_page(cask_markup, TARGET_MAN_PATH/"brew-cask.1")
  end

  def path_glob_commands(glob)
    Pathname.glob(glob)
            .sort_by { |source_file| sort_key_for_path(source_file) }
            .map(&:read).map(&:lines)
            .map { |lines| lines.grep(/^#:/).map { |line| line.slice(2..-1) }.join }
            .reject { |s| s.strip.empty? || s.include?("@hide_from_man_page") }
  end

  def build_man_page
    template = (SOURCE_PATH/"brew.1.md.erb").read
    variables = OpenStruct.new

{'fraction_non_alphanumeric': 0.11336235766912257, 'fraction_numerical': 0.0020093770931011385, 'mean_word_length': 3.0799180327868854, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '947408', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1922, 'n_words': 548}