diff --git a/ci/scripts/doc_prettier_check.sh b/ci/scripts/doc_prettier_check.sh index d94a0d1c96171..f074ab928355d 100755 --- a/ci/scripts/doc_prettier_check.sh +++ b/ci/scripts/doc_prettier_check.sh @@ -17,41 +17,68 @@ # specific language governing permissions and limitations # under the License. -SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")" - -MODE="--check" -ACTION="Checking" -if [ $# -gt 0 ]; then - if [ "$1" = "--write" ]; then - MODE="--write" - ACTION="Formatting" - else - echo "Usage: $0 [--write]" >&2 - exit 1 - fi +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" +PRETTIER_VERSION="2.7.1" +PRETTIER_TARGETS=( + '{datafusion,datafusion-cli,datafusion-examples,dev,docs}/**/*.md' + '!datafusion/CHANGELOG.md' + README.md + CONTRIBUTING.md +) + +source "${SCRIPT_DIR}/utils/git.sh" + +MODE="check" +ALLOW_DIRTY=0 + +usage() { + cat >&2 </dev/null 2>&1; then echo "npx is required to run the prettier check. Install Node.js (e.g., brew install node) and re-run." >&2 exit 1 fi - -# Ignore subproject CHANGELOG.md because it is machine generated -npx prettier@2.7.1 $MODE \ - '{datafusion,datafusion-cli,datafusion-examples,dev,docs}/**/*.md' \ - '!datafusion/CHANGELOG.md' \ - README.md \ - CONTRIBUTING.md -status=$? - -if [ $status -ne 0 ]; then - if [ "$MODE" = "--check" ]; then - echo "Prettier check failed. Re-run with --write (e.g., ./ci/scripts/doc_prettier_check.sh --write) to format files, commit the changes, and re-run the check." >&2 - else - echo "Prettier format failed. Files may have been modified; commit any changes and re-run." >&2 - fi - exit $status + +PRETTIER_MODE=(--check) +if [[ "$MODE" == "write" ]]; then + PRETTIER_MODE=(--write) fi + +# Ignore subproject CHANGELOG.md because it is machine generated +npx "prettier@${PRETTIER_VERSION}" "${PRETTIER_MODE[@]}" "${PRETTIER_TARGETS[@]}" diff --git a/ci/scripts/license_header.sh b/ci/scripts/license_header.sh index 5345728f9cdf0..7ab8c9637598b 100755 --- a/ci/scripts/license_header.sh +++ b/ci/scripts/license_header.sh @@ -17,6 +17,62 @@ # specific language governing permissions and limitations # under the License. -# Check Apache license header -set -ex -hawkeye check --config licenserc.toml +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" + +source "${SCRIPT_DIR}/utils/git.sh" + +MODE="check" +ALLOW_DIRTY=0 +HAWKEYE_CONFIG="licenserc.toml" + +usage() { + cat >&2 <&2 <&2 <&2 <&2 <&2 + return 1 + fi +} diff --git a/dev/rust_lint.sh b/dev/rust_lint.sh index 21d4611846413..43d29bd88166d 100755 --- a/dev/rust_lint.sh +++ b/dev/rust_lint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -23,30 +23,103 @@ # Note: The installed checking tools (e.g., taplo) are not guaranteed to match # the CI versions for simplicity, there might be some minor differences. Check # `.github/workflows` for the CI versions. +# +# +# +# For each lint scripts: +# +# By default, they run in check mode: +# ./ci/scripts/rust_fmt.sh +# +# With `--write`, scripts perform best-effort auto fixes: +# ./ci/scripts/rust_fmt.sh --write +# +# The `--write` flag assumes a clean git repository (no uncommitted changes); to force +# auto fixes even if there are unstaged changes, use `--allow-dirty`: +# ./ci/scripts/rust_fmt.sh --write --allow-dirty +# +# New scripts can use `rust_fmt.sh` as a reference. + +set -euo pipefail + +usage() { + cat >&2 < /dev/null; then + echo "Installing $cmd using: $install_cmd" + eval "$install_cmd" + fi +} + +MODE="check" +ALLOW_DIRTY=0 + +while [[ $# -gt 0 ]]; do + case "$1" in + --write) + MODE="write" + ;; + --allow-dirty) + ALLOW_DIRTY=1 + ;; + -h|--help) + usage + ;; + *) + usage + ;; + esac + shift +done + +SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" + +ensure_tool "taplo" "cargo install taplo-cli --locked" +ensure_tool "hawkeye" "cargo install hawkeye --locked" +ensure_tool "typos" "cargo install typos-cli --locked" + +run_step() { + local name="$1" + shift + echo "[${SCRIPT_NAME}] Running ${name}" + "$@" +} + +declare -a WRITE_STEPS=( + "ci/scripts/rust_fmt.sh|true" + "ci/scripts/rust_clippy.sh|true" + "ci/scripts/rust_toml_fmt.sh|true" + "ci/scripts/license_header.sh|true" + "ci/scripts/typos_check.sh|true" + "ci/scripts/doc_prettier_check.sh|true" +) + +declare -a READONLY_STEPS=( + "ci/scripts/rust_docs.sh|false" +) -# For `.toml` format checking -set -e -if ! command -v taplo &> /dev/null; then - echo "Installing taplo using cargo" - cargo install taplo-cli -fi - -# For Apache licence header checking -if ! command -v hawkeye &> /dev/null; then - echo "Installing hawkeye using cargo" - cargo install hawkeye --locked -fi - -# For spelling checks -if ! command -v typos &> /dev/null; then - echo "Installing typos using cargo" - cargo install typos-cli --locked -fi - -ci/scripts/rust_fmt.sh -ci/scripts/rust_clippy.sh -ci/scripts/rust_toml_fmt.sh -ci/scripts/rust_docs.sh -ci/scripts/license_header.sh -ci/scripts/typos_check.sh -ci/scripts/doc_prettier_check.sh +for entry in "${WRITE_STEPS[@]}" "${READONLY_STEPS[@]}"; do + IFS='|' read -r script_path supports_write <<<"$entry" + script_name="$(basename "$script_path")" + args=() + if [[ "$supports_write" == "true" && "$MODE" == "write" ]]; then + args+=(--write) + [[ $ALLOW_DIRTY -eq 1 ]] && args+=(--allow-dirty) + fi + if [[ ${#args[@]} -gt 0 ]]; then + run_step "$script_name" "$script_path" "${args[@]}" + else + run_step "$script_name" "$script_path" + fi +done