77 lines
2.0 KiB
Bash
Executable File
77 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
# Show simple usage help.
|
|
print_usage() {
|
|
echo "Usage: scripts/format-nvgt.sh [path/to/file.nvgt ...]"
|
|
echo "Formats all tracked .nvgt files when no file paths are provided."
|
|
}
|
|
|
|
scriptDir=""
|
|
repoRoot=""
|
|
filePath=""
|
|
formattedCount=0
|
|
targetFiles=()
|
|
|
|
# Help flag is optional and exits early without formatting.
|
|
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
|
print_usage
|
|
exit 0
|
|
fi
|
|
|
|
# Stop immediately if clang-format is not installed.
|
|
if ! command -v clang-format >/dev/null 2>&1; then
|
|
echo "clang-format is required but was not found in PATH." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Resolve script location, then run from repo root so relative paths work.
|
|
scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
repoRoot="$(cd "${scriptDir}/.." && pwd)"
|
|
cd "${repoRoot}"
|
|
|
|
# We require the project style file to keep formatting consistent.
|
|
if [[ ! -f ".clang-format" ]]; then
|
|
echo "Missing .clang-format in repo root: ${repoRoot}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# No args: format every tracked .nvgt file in git.
|
|
if [[ "$#" -eq 0 ]]; then
|
|
mapfile -t targetFiles < <(git ls-files "*.nvgt")
|
|
else
|
|
# Args provided: validate each file and format only those paths.
|
|
for filePath in "$@"; do
|
|
if [[ ! -f "${filePath}" ]]; then
|
|
echo "File not found: ${filePath}" >&2
|
|
exit 1
|
|
fi
|
|
if [[ "${filePath}" != *.nvgt ]]; then
|
|
echo "Only .nvgt files are supported: ${filePath}" >&2
|
|
exit 1
|
|
fi
|
|
targetFiles+=("${filePath}")
|
|
done
|
|
fi
|
|
|
|
if [[ "${#targetFiles[@]}" -eq 0 ]]; then
|
|
echo "No .nvgt files found to format."
|
|
exit 0
|
|
fi
|
|
|
|
# Force C++ parsing rules for NVGT while still using repo .clang-format.
|
|
for filePath in "${targetFiles[@]}"; do
|
|
clang-format -i --style=file --assume-filename=file.cpp "${filePath}"
|
|
formattedCount=$((formattedCount + 1))
|
|
done
|
|
|
|
echo -n "Formatted ${formattedCount} "
|
|
if [[ ${formattedCount} -ne 1 ]]; then
|
|
echo "files."
|
|
else
|
|
echo "file."
|
|
fi
|
|
|
|
exit 0
|