|
| 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 this 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 | +set -euo pipefail |
| 21 | + |
| 22 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 23 | +SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" |
| 24 | + |
| 25 | +source "${SCRIPT_DIR}/utils/git.sh" |
| 26 | + |
| 27 | +MODE="check" |
| 28 | +ALLOW_DIRTY=0 |
| 29 | + |
| 30 | +usage() { |
| 31 | + cat >&2 <<EOF |
| 32 | +Usage: $SCRIPT_NAME [--write] [--allow-dirty] |
| 33 | +
|
| 34 | +Checks if metrics.md is up-to-date by running update_metric_docs.sh. |
| 35 | +--write Run \`./dev/update_metric_docs.sh\` to update metrics.md (requires a clean git worktree, no uncommitted changes). |
| 36 | +--allow-dirty Allow \`--write\` to run even when the git worktree has uncommitted changes. |
| 37 | +EOF |
| 38 | + exit 1 |
| 39 | +} |
| 40 | + |
| 41 | +while [[ $# -gt 0 ]]; do |
| 42 | + case "$1" in |
| 43 | + --write) |
| 44 | + MODE="write" |
| 45 | + ;; |
| 46 | + --allow-dirty) |
| 47 | + ALLOW_DIRTY=1 |
| 48 | + ;; |
| 49 | + -h|--help) |
| 50 | + usage |
| 51 | + ;; |
| 52 | + *) |
| 53 | + usage |
| 54 | + ;; |
| 55 | + esac |
| 56 | + shift |
| 57 | +done |
| 58 | + |
| 59 | +if [[ "$MODE" == "write" && $ALLOW_DIRTY -eq 0 ]]; then |
| 60 | + require_clean_work_tree "$SCRIPT_NAME" || exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +# Ensure we're in the project root |
| 64 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 65 | +cd "$PROJECT_ROOT" |
| 66 | + |
| 67 | +METRICS_FILE="docs/source/user-guide/metrics.md" |
| 68 | + |
| 69 | +if [ ! -f "$METRICS_FILE" ]; then |
| 70 | + echo "Warning: $METRICS_FILE not found, skipping check." >&2 |
| 71 | + exit 0 |
| 72 | +fi |
| 73 | + |
| 74 | +if [[ "$MODE" == "write" ]]; then |
| 75 | + echo "[${SCRIPT_NAME}] Running \`./dev/update_metric_docs.sh\` to update metrics.md" |
| 76 | + ./dev/update_metric_docs.sh |
| 77 | +else |
| 78 | + echo "[${SCRIPT_NAME}] Checking if metrics.md is up-to-date" |
| 79 | + METRICS_BACKUP=$(mktemp) |
| 80 | + |
| 81 | + # Backup the current file |
| 82 | + cp "$METRICS_FILE" "$METRICS_BACKUP" |
| 83 | + |
| 84 | + # Run the update script (this will modify the file) |
| 85 | + # Suppress output but capture exit code |
| 86 | + if ! ./dev/update_metric_docs.sh > /dev/null 2>&1; then |
| 87 | + echo "Error: Failed to run update_metric_docs.sh. Check permissions and try again." >&2 |
| 88 | + # Restore the original file |
| 89 | + mv "$METRICS_BACKUP" "$METRICS_FILE" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + |
| 93 | + # Compare the updated file with the backup |
| 94 | + if ! diff -q "$METRICS_BACKUP" "$METRICS_FILE" > /dev/null; then |
| 95 | + echo "Error: metrics.md is not up-to-date. Run './dev/update_metric_docs.sh' and commit the changes." >&2 |
| 96 | + # Restore the original file |
| 97 | + mv "$METRICS_BACKUP" "$METRICS_FILE" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + # Clean up the backup file |
| 102 | + rm -f "$METRICS_BACKUP" |
| 103 | +fi |
| 104 | + |
0 commit comments