Add spell-check to CI

This commit is contained in:
Taiki Endo
2022-12-09 22:03:48 +09:00
parent 8666426bd7
commit ab2d9d2b4f
7 changed files with 230 additions and 23 deletions

View File

@@ -4,13 +4,16 @@ set -euo pipefail
IFS=$'\n\t'
cd "$(dirname "$0")"/..
# shellcheck disable=SC2154
trap 's=$?; echo >&2 "$0: Error on line "${LINENO}": ${BASH_COMMAND}"; exit ${s}' ERR
# USAGE:
# ./tools/tidy.sh
#
# Note: This script requires the following tools:
# - shfmt
# - shellcheck
# - npm (if any of YAML/JavaScript/JSON exists)
# - npm
# - jq and yq (if this repository uses bors)
# - clang-format (if any of C/C++ exists)
#
@@ -21,14 +24,10 @@ cd "$(dirname "$0")"/..
x() {
local cmd="$1"
shift
if [[ -n "${verbose:-}" ]]; then
(
set -x
"${cmd}" "$@"
)
else
(
set -x
"${cmd}" "$@"
fi
)
}
check_diff() {
if [[ -n "${CI:-}" ]]; then
@@ -50,17 +49,10 @@ warn() {
should_fail=1
}
if [[ "${1:-}" == "-v" ]]; then
shift
verbose=1
fi
if [[ -n "${CI:-}" ]]; then
verbose=1
fi
if [[ $# -gt 0 ]]; then
cat <<EOF
USAGE:
$0 [-v]
$0
EOF
exit 1
fi
@@ -81,15 +73,12 @@ fi
# YAML/JavaScript/JSON (if exists)
if [[ -n "$(git ls-files '*.yml')$(git ls-files '*.js')$(git ls-files '*.json')" ]]; then
if type -P npm &>/dev/null; then
if [[ ! -e node_modules/.bin/prettier ]]; then
x npm install prettier &>/dev/null
fi
x npx prettier -l -w $(git ls-files '*.yml') $(git ls-files '*.js') $(git ls-files '*.json')
check_diff $(git ls-files '*.yml') $(git ls-files '*.js') $(git ls-files '*.json')
else
warn "'npm' is not installed"
fi
if [[ -e .github/workflows/ci.yml ]] && grep -q '# tidy:needs' .github/workflows/ci.yml; then
if [[ -e .github/workflows/ci.yml ]] && grep -q '# tidy:needs' .github/workflows/ci.yml && ! grep -Eq '# *needs: \[' .github/workflows/ci.yml; then
if type -P jq &>/dev/null && type -P yq &>/dev/null; then
# shellcheck disable=SC2207
jobs_actual=($(yq '.jobs' .github/workflows/ci.yml | jq -r 'keys_unsorted[]'))
@@ -133,6 +122,47 @@ else
warn "'shellcheck' is not installed"
fi
# Spell check (if config exists)
if [[ -f .cspell.json ]]; then
if type -P npm &>/dev/null; then
if [[ -f Cargo.toml ]]; then
metadata=$(cargo metadata --format-version=1 --all-features --no-deps)
dependencies=''
for id in $(jq <<<"${metadata}" '.workspace_members[]'); do
dependencies+=$'\n'
dependencies+=$(jq <<<"${metadata}" ".packages[] | select(.id == ${id})" | jq -r '.dependencies[].name')
done
cat >.github/.cspell/rust-dependencies.txt <<EOF
// This file is @generated by $(basename "$0").
// It is not intended for manual editing.
EOF
# shellcheck disable=SC2001
sed <<<"${dependencies}" 's/[0-9_-]/\n/g' | LC_ALL=C sort -f -u | (grep -E '.{4,}' || true) >>.github/.cspell/rust-dependencies.txt
check_diff .github/.cspell/rust-dependencies.txt
else
touch .github/.cspell/rust-dependencies.txt
fi
x npx cspell --no-progress $(git ls-files)
for dictionary in .github/.cspell/*.txt; do
if [[ "${dictionary}" == .github/.cspell/project-dictionary.txt ]]; then
continue
fi
dup=$(sed '/^$/d' .github/.cspell/project-dictionary.txt "${dictionary}" | LC_ALL=C sort -f | uniq -d -i | (grep -v '//.*' || true))
if [[ -n "${dup}" ]]; then
warn "duplicated words in dictionaries; please remove the following words from .github/.cspell/project-dictionary.txt"
echo "======================================="
echo "${dup}"
echo "======================================="
fi
done
else
warn "'npm' is not installed"
fi
fi
if [[ -n "${should_fail:-}" ]]; then
exit 1
fi