#!/bin/zsh -Ndefgku
#
# script/format
# mas
#
# Automatically formats and fixes style violations using various tools.
#
# Please keep in sync with script/lint.
#

. "${0:a:h}/_setup_script"

printf $'==> 🚨 Formatting mas %s\n' "$(script/version)"

for formatter in markdownlint prettier swift-format swiftformat swiftlint yamllint; do
  if ! command -v "${formatter}" >/dev/null; then
    printf $'error: %s is not installed. Run \'script/bootstrap\' or \'brew install %s\'.\n' "${formatter}" "${formatter}" >&2
    exit 1
  fi
done

for source in Package.swift Sources Tests; do
  printf -- $'--> 🦅 %s swift-format\n' "${source}"
  swift-format format --in-place --recursive "${source}"
  printf -- $'--> 🦅 %s swiftformat\n' "${source}"
  script -q /dev/null swiftformat --strict "${source}" |
    (grep -vxE '(?:\^D\x08{2})?Running SwiftFormat\.{3}\r|Reading (?:config|swift-version) file at .*|\x1b\[32mSwiftFormat completed in \d+\.\d+s\.\x1b\[0m\r|0/\d+ files formatted\.\r' || true)
  printf -- $'--> 🦅 %s swiftlint\n' "${source}"
  swiftlint --fix --quiet "${source}"
done

printf -- $'--> 〽️ Markdown\n'
markdownlint --config .markdownlint.json --fix . docs

printf -- $'--> 📝 YAML\n'
script -q /dev/null prettier --write "${(f)"$(yamllint --list-files .)"}" |
  (grep -vxE '.+ \d+ms \(unchanged\)\r' || true)
