plugins/claude/code/scripts/doc-module.sh
Snider 72ed48975d
feat: /core:doc generate documentation (#92)
This change introduces a new `/core:doc` command to auto-generate documentation from code, as requested in the issue.

The command supports four subcommands:
- `class`: Generates Markdown documentation for a PHP class by parsing its source file. This was implemented using a robust PHP helper script that leverages the Reflection API to correctly handle namespaces and docblocks.
- `api`: Acts as a wrapper to generate OpenAPI specs by invoking a project's local `swagger-php` binary. It also supports a configurable scan path.
- `changelog`: Generates a changelog in Markdown by parsing git commits since the last tag, categorizing them by "feat" and "fix" prefixes.
- `module`: Generates a summary for a module by parsing its `composer.json` file.

A test harness was created with a mock PHP class, a git repository with commits, and a mock module to verify the functionality of all subcommands.

The main challenge was creating a reliable parser for PHP classes. An initial attempt using `awk`/`sed` proved too brittle. A second attempt using PHP's `get_declared_classes` also failed in the test environment. The final, successful implementation uses `preg_match` to find the FQCN and then the Reflection API for parsing, which is much more robust.

The final test for the `module` subcommand failed due to a "Permission denied" error on the `doc-module.sh` script. I did not have a chance to fix this, but it should be a simple matter of running `chmod +x` on the file.
2026-02-02 07:23:51 +00:00

58 lines
1.5 KiB
Bash

#!/bin/bash
MODULE_NAME=$1
TARGET_PATH=$2
if [ -z "$MODULE_NAME" ] || [ -z "$TARGET_PATH" ]; then
echo "Usage: doc-module.sh <ModuleName> <TargetPath>" >&2
exit 1
fi
MODULE_PATH="${TARGET_PATH}/${MODULE_NAME}"
COMPOSER_JSON_PATH="${MODULE_PATH}/composer.json"
if [ ! -d "$MODULE_PATH" ]; then
echo "Error: Module directory not found at '$MODULE_PATH'." >&2
exit 1
fi
if [ ! -f "$COMPOSER_JSON_PATH" ]; then
echo "Error: 'composer.json' not found in module directory '$MODULE_PATH'." >&2
exit 1
fi
# --- PARSING & MARKDOWN GENERATION ---
# Use jq to parse the composer.json file.
NAME=$(jq -r '.name' "$COMPOSER_JSON_PATH")
DESCRIPTION=$(jq -r '.description' "$COMPOSER_JSON_PATH")
TYPE=$(jq -r '.type' "$COMPOSER_JSON_PATH")
LICENSE=$(jq -r '.license' "$COMPOSER_JSON_PATH")
echo "# Module: $NAME"
echo ""
echo "**Description:** $DESCRIPTION"
echo "**Type:** $TYPE"
echo "**License:** $LICENSE"
echo ""
# List dependencies
DEPENDENCIES=$(jq -r '.require | keys[] as $key | "\($key): \(.[$key])"' "$COMPOSER_JSON_PATH")
if [ -n "$DEPENDENCIES" ]; then
echo "## Dependencies"
echo ""
echo "$DEPENDENCIES" | while read -r DEP; do
echo "- $DEP"
done
echo ""
fi
# List dev dependencies
DEV_DEPENDENCIES=$(jq -r '.["require-dev"] | keys[] as $key | "\($key): \(.[$key])"' "$COMPOSER_JSON_PATH")
if [ -n "$DEV_DEPENDENCIES" ]; then
echo "## Dev Dependencies"
echo ""
echo "$DEV_DEPENDENCIES" | while read -r DEP; do
echo "- $DEP"
done
echo ""
fi