Exclude known words from "rust-dependencies" cspell dictionary

This commit is contained in:
Taiki Endo
2023-02-07 21:53:59 +09:00
parent 5737265abf
commit c5ba83b5c8
2 changed files with 24 additions and 16 deletions

View File

@@ -145,24 +145,36 @@ 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)
if [[ -n "$(git ls-files '*Cargo.toml')" ]]; then
dependencies=''
for id in $(jq <<<"${metadata}" '.workspace_members[]'); do
dependencies+=$'\n'
dependencies+=$(jq <<<"${metadata}" ".packages[] | select(.id == ${id})" | jq -r '.dependencies[].name')
for manifest_path in $(git ls-files '*Cargo.toml'); do
if [[ "${manifest_path}" != "Cargo.toml" ]] && ! grep -Eq '\[workspace\]' "${manifest_path}"; then
continue
fi
metadata=$(cargo metadata --format-version=1 --all-features --no-deps --manifest-path "${manifest_path}")
for id in $(jq <<<"${metadata}" '.workspace_members[]'); do
dependencies+=$'\n'
dependencies+=$(jq <<<"${metadata}" ".packages[] | select(.id == ${id})" | jq -r '.dependencies[].name')
done
done
cat >.github/.cspell/rust-dependencies.txt <<EOF
words=''
# shellcheck disable=SC2001
for word in $(sed <<<"${dependencies}" 's/[0-9_-]/\n/g' | LC_ALL=C sort -f -u | (grep -E '.{4,}' || true)); do
# Skip if the word is contained in other dictionaries.
if ! npx cspell trace "${word}" 2>/dev/null | (grep -v -E '/(project-dictionary|rust-dependencies)\.txt' || true) | grep -Eq "^${word} \* [0-9A-Za-z_-]+\* "; then
words+=$'\n'
words+="${word}"
fi
done
fi
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
if [[ -n "${words:-}" ]]; then
echo "${words}" >>.github/.cspell/rust-dependencies.txt
fi
check_diff .github/.cspell/rust-dependencies.txt
echo "+ npx cspell --no-progress \$(git ls-files)"
npx cspell --no-progress $(git ls-files)