# frozen_string_literal: true

require "rake"

RUBYDOC_DIR = "#{__dir__}/rubydoc".freeze

task default: :build

desc "Generate documentation with YARD"
task :yard do
  cd "#{__dir__}/../Library/Homebrew" do
    sh "bundle exec yard doc --output #{RUBYDOC_DIR}"
  end
end

desc "Build the site."
task :build do
  sh "bundle exec jekyll build"
end

desc "Run Markdownlint to validate the Markdown style."
task :lint do
  sh "mdl $(git ls-files '*.md' | grep -v 'Manpage.md' | grep -v '^docs/index.md$' | grep -v '^index.md$')"
  cd(__dir__) { sh "mdl --style index.mdl_style.rb index.md" } if File.exist?("#{__dir__}/index.md")
  sh "grep -L '^last_review_date:' $(git ls-files '*.md' | grep -v 'Manpage.md') | " \
     "xargs -I {} echo 'File {} is missing last_review_date frontmatter.'"
end

desc "Run HTMLProofer to validate the HTML output."
task test: :build do
  require "html-proofer"

  HOMEBREW_USER_AGENT = ENV.fetch("HOMEBREW_USER_AGENT", nil).freeze
  HTMLProofer.check_directory(
    "./_site",
    parallel:            { in_threads: 4 },
    typhoeus:            {
      headers: {
        "User-Agent" => "#{"#{HOMEBREW_USER_AGENT} " if HOMEBREW_USER_AGENT}HTMLProofer/#{HTMLProofer::VERSION}",
      },
    },
    favicon:             true,
    ignore_status_codes: [0, 403],
    check_favicon:       true,
    check_opengraph:     true,
    check_html:          true,
    check_img_http:      true,
    enforce_https:       true,
    ignore_files:        [
      /Kickstarter-Supporters/,
    ],
    ignore_urls:         [
      "/",
      %r{https://docs\.brew\.sh/},
      %r{/rubydoc/},
      %r{https://github.com/},
      %r{https://homebrew.1password.com/},
      %r{https://gist.github.com/},
      "https://docs.fedoraproject.org/en-US/packaging-guidelines/#bundling",
      "https://legacy.python.org/dev/peps/pep-0453/#recommendations-for-downstream-distributors",
      "https://metacpan.org/pod/local::lib",
      "https://www.gnu.org/software/bash/",
      %r{https://www\.virustotal\.com/?},
      %r{https://pypi.org/?},
      "https://itunes.apple.com/us/app/xcode/id497799835",
    ],
    cache:               {
      timeframe: {
        external: "1d",
        internal: "1h",
      },
    },
  ).run
end
