From 2402df67ca8611a2d42e99e08662b3db81e42fca Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 5 Apr 2023 22:47:02 +0900 Subject: [PATCH] Update tools/tidy.sh --- .github/.cspell/organization-dictionary.txt | 4 ++ .github/.cspell/project-dictionary.txt | 7 ---- tools/tidy.sh | 46 ++++++++++++++++----- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.github/.cspell/organization-dictionary.txt b/.github/.cspell/organization-dictionary.txt index f7b26ea5..6d51b6fd 100644 --- a/.github/.cspell/organization-dictionary.txt +++ b/.github/.cspell/organization-dictionary.txt @@ -67,6 +67,7 @@ openwrt riscv softfloat sparcv +spirv thumbeb thumbebv thumbv @@ -89,12 +90,14 @@ bools builtins bytecount canonicalize +concat consts ctypes dealloc deque docsrs doctest +doctests hasher impls inlateout @@ -148,6 +151,7 @@ powerset proto ranlib readelf +shellcheckrc SIGABRT SIGILL SIGINT diff --git a/.github/.cspell/project-dictionary.txt b/.github/.cspell/project-dictionary.txt index ca7e64b9..7d572bc4 100644 --- a/.github/.cspell/project-dictionary.txt +++ b/.github/.cspell/project-dictionary.txt @@ -1,5 +1,4 @@ binstall -bytecodealliance coreutils distro doas @@ -7,21 +6,15 @@ dprint enablerepo epel grcov -jfrimmel -koalaman libc linkcheck mdbook microdnf -mvdan nextest -protobuf protoc -protocolbuffers pwsh quickinstall rockylinux -rustwasm shellcheck shfmt udeps diff --git a/tools/tidy.sh b/tools/tidy.sh index 56e2a903..7f1ad86a 100755 --- a/tools/tidy.sh +++ b/tools/tidy.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 OR MIT # shellcheck disable=SC2046 set -euo pipefail IFS=$'\n\t' @@ -33,6 +34,9 @@ check_diff() { fi fi } +info() { + echo >&2 "info: $*" +} warn() { if [[ -n "${GITHUB_ACTIONS:-}" ]]; then echo "::warning::$*" @@ -171,7 +175,9 @@ fi # Spell check (if config exists) if [[ -f .cspell.json ]]; then if type -P npm &>/dev/null; then + has_rust='' if [[ -n "$(git ls-files '*Cargo.toml')" ]]; then + has_rust='1' dependencies='' for manifest_path in $(git ls-files '*Cargo.toml'); do if [[ "${manifest_path}" != "Cargo.toml" ]] && ! grep -Eq '\[workspace\]' "${manifest_path}"; then @@ -179,24 +185,27 @@ if [[ -f .cspell.json ]]; then 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') + dependencies+="$(jq <<<"${metadata}" ".packages[] | select(.id == ${id})" | jq -r '.dependencies[].name')"$'\n' done done # shellcheck disable=SC2001 dependencies=$(sed <<<"${dependencies}" 's/[0-9_-]/\n/g' | LC_ALL=C sort -f -u) - config_old=$(<.cspell.json) - config_new=$(grep <<<"${config_old}" -v ' *//' | jq 'del(.dictionaries[] | select(index("organization-dictionary") | not))' | jq 'del(.dictionaryDefinitions[] | select(.name == "organization-dictionary" | not))') - echo "${config_new}" >.cspell.json - words=$(npx <<<"${dependencies}" cspell stdin --no-progress --no-summary --words-only --unique || true) - echo "${config_old}" >.cspell.json fi + config_old=$(<.cspell.json) + config_new=$(grep <<<"${config_old}" -v ' *//' | jq 'del(.dictionaries[] | select(index("organization-dictionary") | not))' | jq 'del(.dictionaryDefinitions[] | select(.name == "organization-dictionary" | not))') + echo "${config_new}" >.cspell.json + if [[ -n "${has_rust}" ]]; then + dependencies_words=$(npx <<<"${dependencies}" cspell stdin --no-progress --no-summary --words-only --unique || true) + fi + all_words=$(npx cspell --no-progress --no-summary --words-only --unique $(git ls-files | (grep -v '\.github/\.cspell/project-dictionary\.txt' || true)) || true) + # TODO: handle SIGINT + echo "${config_old}" >.cspell.json cat >.github/.cspell/rust-dependencies.txt <>.github/.cspell/rust-dependencies.txt + if [[ -n "${dependencies_words:-}" ]]; then + echo $'\n'"${dependencies_words}" >>.github/.cspell/rust-dependencies.txt fi check_diff .github/.cspell/rust-dependencies.txt if ! grep -Eq "^\.github/\.cspell/rust-dependencies.txt linguist-generated" .gitattributes; then @@ -204,8 +213,11 @@ EOF fi echo "+ npx cspell --no-progress --no-summary \$(git ls-files)" - npx cspell --no-progress --no-summary $(git ls-files) + if ! npx cspell --no-progress --no-summary $(git ls-files); then + error "spellcheck failed: please fix uses of above words or add to .github/.cspell/project-dictionary.txt if correct" + fi + # Make sure the project-specific dictionary does not contain duplicated words. for dictionary in .github/.cspell/*.txt; do if [[ "${dictionary}" == .github/.cspell/project-dictionary.txt ]]; then continue @@ -218,6 +230,20 @@ EOF echo "=======================================" fi done + + # Make sure the project-specific dictionary does not contain unused words. + unused='' + for word in $(grep -v '//.*' .github/.cspell/project-dictionary.txt || true); do + if ! grep <<<"${all_words}" -Eq -i "^${word}$"; then + unused+="${word}"$'\n' + fi + done + if [[ -n "${unused}" ]]; then + error "unused words in dictionaries; please remove the following words from .github/.cspell/project-dictionary.txt" + echo "=======================================" + echo -n "${unused}" + echo "=======================================" + fi else warn "'npm' is not installed" fi