tools: Update tidy.sh

This commit is contained in:
Taiki Endo
2025-03-21 03:23:42 +09:00
parent 351cce3d3a
commit bd8846282c
2 changed files with 15 additions and 29 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,5 @@
target target
Cargo.lock Cargo.lock
.venv
tmp tmp
# For platform and editor specific settings, it is recommended to add to # For platform and editor specific settings, it is recommended to add to

View File

@@ -14,7 +14,7 @@ cd -- "$(dirname -- "$0")"/..
# - git 1.8+ # - git 1.8+
# - jq 1.6+ # - jq 1.6+
# - npm (node 18+) # - npm (node 18+)
# - python 3.5.3+ # - python 3.6+ and pipx
# - shfmt # - shfmt
# - shellcheck # - shellcheck
# - cargo, rustfmt (if Rust code exists) # - cargo, rustfmt (if Rust code exists)
@@ -69,7 +69,11 @@ check_diff() {
should_fail=1 should_fail=1
fi fi
else else
if ! git --no-pager diff --exit-code "$@" &>/dev/null; then local res
res=$(git --no-pager diff --exit-code --name-only "$@" || true)
if [[ -n "${res}" ]]; then
warn "please commit changes made by formatter/generator if exists on the following files"
print_fenced "${res}"$'\n'
should_fail=1 should_fail=1
fi fi
fi fi
@@ -119,15 +123,6 @@ check_hidden() {
sed_rhs_escape() { sed_rhs_escape() {
sed 's/\\/\\\\/g; s/\&/\\\&/g; s/\//\\\//g' <<<"$1" sed 's/\\/\\\\/g; s/\&/\\\&/g; s/\//\\\//g' <<<"$1"
} }
venv_install_yq() {
if [[ ! -e "${venv_bin}/yq${exe}" ]]; then
if [[ ! -d .venv ]]; then
"python${py_suffix}" -m venv .venv >&2
fi
info "installing yq to .venv using pip${py_suffix}"
"${venv_bin}/pip${py_suffix}${exe}" install yq >&2
fi
}
if [[ $# -gt 0 ]]; then if [[ $# -gt 0 ]]; then
cat <<EOF cat <<EOF
@@ -137,19 +132,15 @@ EOF
exit 1 exit 1
fi fi
exe=''
py_suffix='' py_suffix=''
if type -P python3 >/dev/null; then if type -P python3 >/dev/null; then
py_suffix=3 py_suffix=3
fi fi
venv_bin=.venv/bin
yq() { yq() {
venv_install_yq pipx run yq "$@"
"${venv_bin}/yq${exe}" "$@"
} }
tomlq() { tomlq() {
venv_install_yq pipx run --spec yq tomlq "$@"
"${venv_bin}/tomlq${exe}" "$@"
} }
case "$(uname -s)" in case "$(uname -s)" in
Linux) Linux)
@@ -187,8 +178,6 @@ case "$(uname -s)" in
;; ;;
MINGW* | MSYS* | CYGWIN* | Windows_NT) MINGW* | MSYS* | CYGWIN* | Windows_NT)
ostype=windows ostype=windows
exe=.exe
venv_bin=.venv/Scripts
if type -P jq >/dev/null; then if type -P jq >/dev/null; then
# https://github.com/jqlang/jq/issues/1854 # https://github.com/jqlang/jq/issues/1854
_tmp=$(jq -r .a <<<'{}') _tmp=$(jq -r .a <<<'{}')
@@ -200,12 +189,10 @@ case "$(uname -s)" in
jq() { command jq "$@" | tr -d '\r'; } jq() { command jq "$@" | tr -d '\r'; }
fi fi
yq() { yq() {
venv_install_yq pipx run yq "$@" | tr -d '\r'
"${venv_bin}/yq${exe}" "$@" | tr -d '\r'
} }
tomlq() { tomlq() {
venv_install_yq pipx run --spec yq tomlq "$@" | tr -d '\r'
"${venv_bin}/tomlq${exe}" "$@" | tr -d '\r'
} }
fi fi
fi fi
@@ -219,7 +206,7 @@ exclude_from_ls_files=()
# - `git submodule status` lists submodules. Use sed to remove the first character indicates status ( |+|-). # - `git submodule status` lists submodules. Use sed to remove the first character indicates status ( |+|-).
# - `git ls-files --deleted` lists removed files. # - `git ls-files --deleted` lists removed files.
while IFS=$'\n' read -r line; do exclude_from_ls_files+=("${line}"); done < <({ while IFS=$'\n' read -r line; do exclude_from_ls_files+=("${line}"); done < <({
find . \! \( -name .git -prune \) \! \( -name target -prune \) \! \( -name .venv -prune \) \! \( -name tmp -prune \) -type l | cut -c3- find . \! \( -name .git -prune \) \! \( -name target -prune \) \! \( -name tmp -prune \) -type l | cut -c3-
git submodule status | sed 's/^.//' | cut -d' ' -f2 git submodule status | sed 's/^.//' | cut -d' ' -f2
git ls-files --deleted git ls-files --deleted
} | LC_ALL=C sort -u) } | LC_ALL=C sort -u)
@@ -242,7 +229,7 @@ if [[ -n "$(ls_files '*.rs')" ]]; then
info "checking Rust code style" info "checking Rust code style"
check_config .rustfmt.toml "; consider adding with reference to https://github.com/taiki-e/cargo-hack/blob/HEAD/.rustfmt.toml" check_config .rustfmt.toml "; consider adding with reference to https://github.com/taiki-e/cargo-hack/blob/HEAD/.rustfmt.toml"
check_config .clippy.toml "; consider adding with reference to https://github.com/taiki-e/cargo-hack/blob/HEAD/.clippy.toml" check_config .clippy.toml "; consider adding with reference to https://github.com/taiki-e/cargo-hack/blob/HEAD/.clippy.toml"
if check_install cargo jq python3; then if check_install cargo jq python3 pipx; then
# `cargo fmt` cannot recognize files not included in the current workspace and modules # `cargo fmt` cannot recognize files not included in the current workspace and modules
# defined inside macros, so run rustfmt directly. # defined inside macros, so run rustfmt directly.
# We need to use nightly rustfmt because we use the unstable formatting options of rustfmt. # We need to use nightly rustfmt because we use the unstable formatting options of rustfmt.
@@ -597,7 +584,7 @@ if [[ -n "${res}" ]]; then
print_fenced "${res}"$'\n' print_fenced "${res}"$'\n'
fi fi
# TODO: chmod|chown # TODO: chmod|chown
res=$({ grep -En '(^|[^0-9A-Za-z\."'\''-])(basename|cat|cd|cp|dirname|ln|ls|mkdir|mv|pushd|rm|rmdir|tee|touch)( +-[0-9A-Za-z]+)* +[^<>\|-]' "${bash_files[@]}" || true; } | { grep -Ev '^[^ ]+: *(#|//)' || true; } | LC_ALL=C sort) res=$({ grep -En '(^|[^0-9A-Za-z\."'\''-])(basename|cat|cd|cp|dirname|ln|ls|mkdir|mv|pushd|rm|rmdir|tee|touch|kill|trap)( +-[0-9A-Za-z]+)* +[^<>\|-]' "${bash_files[@]}" || true; } | { grep -Ev '^[^ ]+: *(#|//)' || true; } | LC_ALL=C sort)
if [[ -n "${res}" ]]; then if [[ -n "${res}" ]]; then
error "use \`--\` before path(s): see https://github.com/koalaman/shellcheck/issues/2707 / https://github.com/koalaman/shellcheck/issues/2612 / https://github.com/koalaman/shellcheck/issues/2305 / https://github.com/koalaman/shellcheck/issues/2157 / https://github.com/koalaman/shellcheck/issues/2121 / https://github.com/koalaman/shellcheck/issues/314 for more" error "use \`--\` before path(s): see https://github.com/koalaman/shellcheck/issues/2707 / https://github.com/koalaman/shellcheck/issues/2612 / https://github.com/koalaman/shellcheck/issues/2305 / https://github.com/koalaman/shellcheck/issues/2157 / https://github.com/koalaman/shellcheck/issues/2121 / https://github.com/koalaman/shellcheck/issues/314 for more"
print_fenced "${res}"$'\n' print_fenced "${res}"$'\n'
@@ -800,7 +787,7 @@ elif check_install shellcheck; then
# Exclude SC2096 due to the way the temporary script is created. # Exclude SC2096 due to the way the temporary script is created.
shellcheck_exclude=SC2086,SC2096,SC2129 shellcheck_exclude=SC2086,SC2096,SC2129
info "running \`shellcheck --exclude ${shellcheck_exclude}\` for scripts in .github/workflows/*.yml and **/action.yml" info "running \`shellcheck --exclude ${shellcheck_exclude}\` for scripts in .github/workflows/*.yml and **/action.yml"
if check_install jq python3; then if check_install jq python3 pipx; then
shellcheck_for_gha() { shellcheck_for_gha() {
local text=$1 local text=$1
local shell=$2 local shell=$2
@@ -968,7 +955,7 @@ fi
if [[ -f .cspell.json ]]; then if [[ -f .cspell.json ]]; then
info "spell checking" info "spell checking"
project_dictionary=.github/.cspell/project-dictionary.txt project_dictionary=.github/.cspell/project-dictionary.txt
if check_install npm jq python3; then if check_install npm jq python3 pipx; then
has_rust='' has_rust=''
if [[ -n "$(ls_files '*Cargo.toml')" ]]; then if [[ -n "$(ls_files '*Cargo.toml')" ]]; then
has_rust=1 has_rust=1