mas/script/lint
Ross Goldberg 466ea67194
Improve scripts: bootstrap, build, format, lint, test & version.
Allow them to be run from any directory.

Call version from lint & test to create Package.swift with version info.

Fail when accessing unset variables.

Improve variable names.

Fix lint issues.

Improve lint & format scripts.

Don't require user input to continue linting.

Much cleaner lint output.

Reorder lint output.

Get swift-format from Brewfile instead of from Package.swift:

 - Speeds up linting.
 - Properly models dependency (not a code dependency).
 - swift-format depends on an old version of swift-argument-parser. Will refactor to use SAP soon.

Include some improvements from 1.8.7 PR.

Other scripts need improvement, too.

Resolve #545

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2024-10-05 20:33:45 -04:00

72 lines
2.3 KiB
Bash
Executable file

#!/bin/bash -u
#
# script/lint
# mas
#
# Linting checks for development and CI.
#
# Reports style violations without making any modifications to the code.
#
# Please keep in sync with script/format.
#
set -o pipefail
mas_dir="$(readlink -fn "$(dirname "${BASH_SOURCE:-"${0}"}")/..")"
if ! cd -- "${mas_dir}"; then
printf $'Error: Could not cd into mas directory: %s\n' "${mas_dir}" >&2
exit 1
fi
printf $'==> 🚨 Linting mas (%s)\n' "$(script/version)"
for linter in git markdownlint periphery shellcheck shfmt swift-format swiftformat swiftlint; do
if [[ ! -x "$(command -v ${linter})" ]]; then
printf $'error: %s is not installed. Run \'script/bootstrap\' or \'brew install %s\'.\n' "${linter}" "${linter}"
exit 1
fi
done
exit_code=0
for source in Package.swift Sources Tests; do
printf -- $'--> 🕊 %s swift-format\n' "${source}"
swift-format lint --strict --recursive "${source}"
((exit_code |= "${?}"))
printf -- $'--> 🕊 %s swiftformat\n' "${source}"
script -q /dev/null swiftformat --lint --strict "${source}" |
(grep -vxE $'Running SwiftFormat\\.\\.\\.\r|\\(lint mode - no files will be changed\\.\\)\r|Reading (?:config|swift-version) file at .*|\033\[32mSwiftFormat completed in \\d+\\.\\d+s\\.\033\\[0m\r|0/\\d+ files require formatting\\.\r' || true)
((exit_code |= "${?}"))
printf -- $'--> 🕊 %s swiftlint\n' "${source}"
swiftlint --strict --quiet "${source}" 2> \
>((grep -vxF $'warning: Configuration option \'allow_multiline_func\' in \'opening_brace\' rule is deprecated. Use the option \'ignore_multiline_function_signatures\' instead.' || true) >&2)
((exit_code |= "${?}"))
done
printf -- $'--> 🐚 Bash shellcheck\n'
shellcheck --shell=bash script/*
((exit_code |= "${?}"))
printf -- $'--> 📜 Bash shfmt\n'
shfmt \
--diff \
--list \
--indent 2 \
--case-indent \
contrib/ script/
((exit_code |= "${?}"))
printf -- $'--> 〽️ Markdown\n'
markdownlint --config .markdownlint.json .github .
((exit_code |= "${?}"))
printf -- $'--> 🌳 Git\n'
PAGER='cat' git diff --check
((exit_code |= "${?}"))
printf -- $'--> 🌀 Periphery\n'
script -q /dev/null periphery scan --strict --quiet --disable-update-check |
(grep -vxF $'\033[0;1;32m* \033[0;0m\033[0;1mNo unused code detected.\033[0;0m\r\n' || true)
((exit_code |= "${?}"))
exit "${exit_code}"