1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 14:26:46 +00:00

Code support format detection

This commit is contained in:
Tw93
2025-10-12 20:49:10 +08:00
parent d111f18cea
commit cf821cdc4b
25 changed files with 1482 additions and 1152 deletions

60
scripts/format.sh Executable file
View File

@@ -0,0 +1,60 @@
#!/bin/bash
# Format all shell scripts in the Mole project
#
# Usage:
# ./scripts/format.sh # Format all scripts
# ./scripts/format.sh --check # Check only, don't modify
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
CHECK_ONLY=false
# Parse arguments
if [[ "${1:-}" == "--check" ]]; then
CHECK_ONLY=true
elif [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
cat << 'EOF'
Usage: ./scripts/format.sh [--check]
Format shell scripts using shfmt.
Options:
--check Check formatting without modifying files
--help Show this help
Install: brew install shfmt
EOF
exit 0
fi
# Check if shfmt is installed
if ! command -v shfmt > /dev/null 2>&1; then
echo "Error: shfmt not installed"
echo "Install: brew install shfmt"
exit 1
fi
# Find all shell scripts
cd "$PROJECT_ROOT"
# shfmt options: -i 4 (4 spaces), -ci (indent switch cases), -sr (space after redirect)
if [[ "$CHECK_ONLY" == "true" ]]; then
echo "Checking formatting..."
if shfmt -i 4 -ci -sr -d . > /dev/null 2>&1; then
echo "✓ All scripts properly formatted"
exit 0
else
echo "✗ Some scripts need formatting:"
shfmt -i 4 -ci -sr -d .
echo ""
echo "Run './scripts/format.sh' to fix"
exit 1
fi
else
echo "Formatting scripts..."
shfmt -i 4 -ci -sr -w .
echo "✓ Done"
fi

44
scripts/install-hooks.sh Executable file
View File

@@ -0,0 +1,44 @@
#!/bin/bash
# Install git hooks for Mole project
#
# Usage:
# ./scripts/install-hooks.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
cd "$PROJECT_ROOT"
# Check if this is a git repository
if [ ! -d ".git" ]; then
echo "Error: Not a git repository"
exit 1
fi
echo -e "${BLUE}Installing git hooks...${NC}"
# Install pre-commit hook
if [ -f ".git/hooks/pre-commit" ]; then
echo "Pre-commit hook already exists, creating backup..."
mv .git/hooks/pre-commit .git/hooks/pre-commit.backup
fi
ln -s ../../scripts/pre-commit.sh .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo -e "${GREEN}✓ Pre-commit hook installed${NC}"
echo ""
echo "The hook will:"
echo " • Auto-format shell scripts before commit"
echo " • Run shellcheck on changed files"
echo " • Show warnings but won't block commits"
echo ""
echo "To uninstall:"
echo " rm .git/hooks/pre-commit"
echo ""

67
scripts/pre-commit.sh Executable file
View File

@@ -0,0 +1,67 @@
#!/bin/bash
# Git pre-commit hook for Mole
# Automatically formats shell scripts before commit
#
# Installation:
# ln -s ../../scripts/pre-commit.sh .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#
# Or use the install script:
# ./scripts/install-hooks.sh
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Only check shell files that are staged
STAGED_SH_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.sh$|^mole$' || true)
if [ -z "$STAGED_SH_FILES" ]; then
exit 0
fi
echo -e "${YELLOW}Running pre-commit checks on shell files...${NC}"
# Check if shfmt is installed
if ! command -v shfmt &> /dev/null; then
echo -e "${RED}shfmt is not installed. Install with: brew install shfmt${NC}"
exit 1
fi
# Check if shellcheck is installed
if ! command -v shellcheck &> /dev/null; then
echo -e "${RED}shellcheck is not installed. Install with: brew install shellcheck${NC}"
exit 1
fi
NEEDS_FORMAT=0
# Check formatting
for file in $STAGED_SH_FILES; do
if ! shfmt -i 4 -ci -sr -d "$file" > /dev/null 2>&1; then
echo -e "${YELLOW}Formatting $file...${NC}"
shfmt -i 4 -ci -sr -w "$file"
git add "$file"
NEEDS_FORMAT=1
fi
done
# Run shellcheck
for file in $STAGED_SH_FILES; do
if ! shellcheck -S warning "$file" > /dev/null 2>&1; then
echo -e "${YELLOW}ShellCheck warnings in $file:${NC}"
shellcheck -S warning "$file"
echo -e "${YELLOW}Continuing with commit (warnings are non-critical)...${NC}"
fi
done
if [ $NEEDS_FORMAT -eq 1 ]; then
echo -e "${GREEN}✓ Files formatted and re-staged${NC}"
fi
echo -e "${GREEN}✓ Pre-commit checks passed${NC}"
exit 0