diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..ad19955 --- /dev/null +++ b/.clang-format @@ -0,0 +1,18 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 4 +ContinuationIndentWidth: 4 +TabWidth: 4 +UseTab: Never +ColumnLimit: 120 +BreakBeforeBraces: Attach +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +IndentCaseLabels: true +SortIncludes: Never +ReflowComments: false +PointerAlignment: Left +ReferenceAlignment: Left +SpaceBeforeParens: ControlStatements +... diff --git a/scripts/format-nvgt.sh b/scripts/format-nvgt.sh new file mode 100755 index 0000000..4f1c921 --- /dev/null +++ b/scripts/format-nvgt.sh @@ -0,0 +1,76 @@ +#!/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