agent/claude/code/scripts/detect-module.sh
Snider be32fec23d
feat(plugin): Implement workspace awareness to detect the current module (#75)
Introduces a new script to detect the current module (PHP or Go) based on the presence of composer.json or go.mod files, git remote URL, or the current directory name.

This context is loaded once per session and used to dynamically adjust commands, starting with the QA command.

Refactors the QA command and verification scripts to use the new module context, removing redundant project-type detection.
2026-02-02 07:17:36 +00:00

51 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
#
# Detects the current module and sets environment variables for other tools.
# Intended to be run once per session via a hook.
# --- Detection Logic ---
MODULE_NAME=""
MODULE_TYPE="unknown"
# 1. Check for composer.json (PHP)
if [ -f "composer.json" ]; then
MODULE_TYPE="php"
# Use jq, but check if it is installed first
if command -v jq >/dev/null 2>&1; then
MODULE_NAME=$(jq -r ".name // empty" composer.json)
fi
fi
# 2. Check for go.mod (Go)
if [ -f "go.mod" ]; then
MODULE_TYPE="go"
MODULE_NAME=$(grep "^module" go.mod | awk '{print $2}')
fi
# 3. If name is still empty, try git remote
if [ -z "$MODULE_NAME" ] || [ "$MODULE_NAME" = "unknown" ]; then
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
GIT_REMOTE=$(git remote get-url origin 2>/dev/null)
if [ -n "$GIT_REMOTE" ]; then
MODULE_NAME=$(basename "$GIT_REMOTE" .git)
fi
fi
fi
# 4. As a last resort, use the current directory name
if [ -z "$MODULE_NAME" ] || [ "$MODULE_NAME" = "unknown" ]; then
MODULE_NAME=$(basename "$PWD")
fi
# --- Store Context ---
# Create a file with the context variables to be sourced by other scripts.
mkdir -p .claude-plugin/.tmp
CONTEXT_FILE=".claude-plugin/.tmp/module_context.sh"
echo "export CLAUDE_CURRENT_MODULE=\"$MODULE_NAME\"" > "$CONTEXT_FILE"
echo "export CLAUDE_MODULE_TYPE=\"$MODULE_TYPE\"" >> "$CONTEXT_FILE"
# --- User-facing Message ---
# Print a confirmation message to stderr.
echo "Workspace context loaded: Module='$MODULE_NAME', Type='$MODULE_TYPE'" >&2