Files
claude-scripts/hooks/setup_hook.sh

100 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Setup script for Claude Code quality hooks
set -e
echo "🔧 Setting up Claude Code quality hooks..."
# Get the directory of this script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Check if claude-quality is installed
if ! command -v claude-quality &> /dev/null; then
echo "❌ claude-quality is not installed or not in PATH"
echo "Please install it first with: pip install claude-scripts"
exit 1
fi
# Make hook scripts executable
chmod +x "$SCRIPT_DIR/code_quality_guard.py"
chmod +x "$SCRIPT_DIR/code_quality_guard_advanced.py"
# Check Claude Code settings location
CLAUDE_SETTINGS_DIR="$HOME/.config/claude"
CLAUDE_SETTINGS_FILE="$CLAUDE_SETTINGS_DIR/settings.json"
# Create directory if it doesn't exist
mkdir -p "$CLAUDE_SETTINGS_DIR"
# Choose which hook to use
echo "Select hook version:"
echo "1) Basic - Simple deny/allow based on quality issues"
echo "2) Advanced - Configurable thresholds and enforcement modes"
read -p "Choose (1 or 2): " choice
case $choice in
1)
HOOK_SCRIPT="$SCRIPT_DIR/code_quality_guard.py"
echo "Using basic hook"
;;
2)
HOOK_SCRIPT="$SCRIPT_DIR/code_quality_guard_advanced.py"
echo "Using advanced hook with configurable thresholds"
echo ""
echo "You can configure the advanced hook with environment variables:"
echo " QUALITY_DUP_THRESHOLD - Duplicate similarity threshold (default: 0.7)"
echo " QUALITY_COMPLEXITY_THRESHOLD - Max cyclomatic complexity (default: 10)"
echo " QUALITY_ENFORCEMENT - Mode: strict/warn/permissive (default: strict)"
echo ""
;;
*)
echo "Invalid choice. Using basic hook."
HOOK_SCRIPT="$SCRIPT_DIR/code_quality_guard.py"
;;
esac
# Create the settings JSON
cat > "$CLAUDE_SETTINGS_FILE" << EOF
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "python $HOOK_SCRIPT"
}
]
},
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "python $HOOK_SCRIPT"
}
]
},
{
"matcher": "MultiEdit",
"hooks": [
{
"type": "command",
"command": "python $HOOK_SCRIPT"
}
]
}
]
}
}
EOF
echo "✅ Hook installed successfully!"
echo "📁 Settings file: $CLAUDE_SETTINGS_FILE"
echo ""
echo "The hook will now check Python code quality before allowing writes/edits in Claude Code."
echo ""
echo "To disable the hook, remove or rename: $CLAUDE_SETTINGS_FILE"
echo "To test the hook, try writing low-quality Python code in Claude Code."