|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +# This script runs all the Rust lints locally the same way the |
| 21 | +# DataFusion CI does |
| 22 | +# |
| 23 | +# Note: The installed checking tools (e.g., taplo) are not guaranteed to match |
| 24 | +# the CI versions for simplicity, there might be some minor differences. Check |
| 25 | +# `.github/workflows` for the CI versions. |
| 26 | +# |
| 27 | +# |
| 28 | +# |
| 29 | +# For each lint scripts: |
| 30 | +# |
| 31 | +# By default, they run in check mode: |
| 32 | +# ./ci/scripts/rust_fmt.sh |
| 33 | +# |
| 34 | +# With `--write`, scripts perform best-effort auto fixes: |
| 35 | +# ./ci/scripts/rust_fmt.sh --write |
| 36 | +# |
| 37 | +# The `--write` flag assumes a clean git repository (no uncommitted changes); to force |
| 38 | +# auto fixes even if there are unstaged changes, use `--allow-dirty`: |
| 39 | +# ./ci/scripts/rust_fmt.sh --write --allow-dirty |
| 40 | +# |
| 41 | +# New scripts can use `rust_fmt.sh` as a reference. |
| 42 | + |
| 43 | +set -euo pipefail |
| 44 | + |
| 45 | +usage() { |
| 46 | + cat >&2 <<EOF |
| 47 | +Usage: $0 [--write] [--allow-dirty] |
| 48 | +
|
| 49 | +Runs the local Rust lint suite similar to CI. |
| 50 | +--write Run formatters, clippy and other non-functional checks in best-effort write/fix mode (requires a clean git worktree, no uncommitted changes; some checks are test-only and ignore this flag). |
| 51 | +--allow-dirty Allow \`--write\` to run even when the git worktree has uncommitted changes. |
| 52 | +EOF |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +ensure_tool() { |
| 57 | + local cmd="$1" |
| 58 | + local install_cmd="$2" |
| 59 | + if ! command -v "$cmd" &> /dev/null; then |
| 60 | + echo "Installing $cmd using: $install_cmd" |
| 61 | + eval "$install_cmd" |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +MODE="check" |
| 66 | +ALLOW_DIRTY=0 |
| 67 | + |
| 68 | +while [[ $# -gt 0 ]]; do |
| 69 | + case "$1" in |
| 70 | + --write) |
| 71 | + MODE="write" |
| 72 | + ;; |
| 73 | + --allow-dirty) |
| 74 | + ALLOW_DIRTY=1 |
| 75 | + ;; |
| 76 | + -h|--help) |
| 77 | + usage |
| 78 | + ;; |
| 79 | + *) |
| 80 | + usage |
| 81 | + ;; |
| 82 | + esac |
| 83 | + shift |
| 84 | +done |
| 85 | + |
| 86 | +SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" |
| 87 | + |
| 88 | +ensure_tool "taplo" "cargo install taplo-cli --locked" |
| 89 | +ensure_tool "hawkeye" "cargo install hawkeye --locked" |
| 90 | +ensure_tool "typos" "cargo install typos-cli --locked" |
| 91 | + |
| 92 | +run_step() { |
| 93 | + local name="$1" |
| 94 | + shift |
| 95 | + echo "[${SCRIPT_NAME}] Running ${name}" |
| 96 | + "$@" |
| 97 | +} |
| 98 | + |
| 99 | +declare -a WRITE_STEPS=( |
| 100 | + "ci/scripts/rust_fmt.sh|true" |
| 101 | + "ci/scripts/rust_clippy.sh|true" |
| 102 | + "ci/scripts/rust_toml_fmt.sh|true" |
| 103 | + "ci/scripts/license_header.sh|true" |
| 104 | + "ci/scripts/typos_check.sh|true" |
| 105 | + "ci/scripts/doc_prettier_check.sh|true" |
| 106 | +) |
| 107 | + |
| 108 | +declare -a READONLY_STEPS=( |
| 109 | + "ci/scripts/check_no_cargo_install_in_workflows.sh|false" |
| 110 | + "ci/scripts/rust_docs.sh|false" |
| 111 | +) |
| 112 | + |
| 113 | +for entry in "${WRITE_STEPS[@]}" "${READONLY_STEPS[@]}"; do |
| 114 | + IFS='|' read -r script_path supports_write <<<"$entry" |
| 115 | + script_name="$(basename "$script_path")" |
| 116 | + args=() |
| 117 | + if [[ "$supports_write" == "true" && "$MODE" == "write" ]]; then |
| 118 | + args+=(--write) |
| 119 | + [[ $ALLOW_DIRTY -eq 1 ]] && args+=(--allow-dirty) |
| 120 | + fi |
| 121 | + if [[ ${#args[@]} -gt 0 ]]; then |
| 122 | + run_step "$script_name" "$script_path" "${args[@]}" |
| 123 | + else |
| 124 | + run_step "$script_name" "$script_path" |
| 125 | + fi |
| 126 | +done |
0 commit comments