Codebase Analysis
Generate quantitative, language-agnostic analysis reports for any codebase. Reports are point-in-time snapshots saved as markdown in docs/codebase-analytics/.
When to Activate
- User asks to "analyze the codebase", "generate codebase report", or "profile the project"
- User wants an overview of project size, structure, dependencies, or complexity
- User asks about module coupling, dependency graphs, or largest files
- User says "codebase analytics" or "codebase analysis"
Prerequisites
cloc (required for stats report)
Check if installed:
which cloc
If not found, install it:
- macOS:
brew install cloc
- npm:
npm install -g cloc
- Debian/Ubuntu:
sudo apt install cloc
- Red Hat/Fedora:
sudo yum install cloc
git (required for all reports)
The project must be a git repository. All file discovery uses git ls-files or cloc --vcs=git to respect .gitignore and exclude generated/vendored files.
Reports
The skill produces up to 6 reports. The user can request all reports or specific ones.
| Report |
File |
What It Answers |
| Stats |
stats.md |
How big is the codebase? What languages? How much test code? |
| Dependency Graph |
dependency-graph.md |
How do internal modules depend on each other? |
| Third-Party Dependencies |
third-party-dependencies.md |
What external packages are used and why? |
| Module Coupling |
module-coupling.md |
How tightly coupled are modules? (import frequency) |
| Largest Files |
largest-files.md |
Where are the complexity hotspots? |
| Service Inventory |
service-inventory.md |
What interfaces exist and what implements them? |
Workflow
1. Initialize
If docs/codebase-analytics/ does not exist, create it with a README.md index.
2. Detect Project Structure
Before generating reports, understand the project layout:
- Identify the primary language(s) — run
cloc --vcs=git --quiet to see the language breakdown
- Identify module boundaries — look for:
- Package manifests:
Package.swift, package.json, Cargo.toml, go.mod, build.gradle, pyproject.toml, *.csproj, pom.xml
- Module directories:
src/, lib/, packages/, modules/, features/, apps/
- Test directories:
tests/, test/, __tests__/, *Tests/, spec/
- Identify dependency management — look for lock files and dependency declarations
- Identify interface/implementation patterns — protocols, interfaces, abstract classes, traits
3. Generate Reports
For each requested report, follow the format specification below. Always include **Generated:** YYYY-MM-DD at the top of each report.
4. Update Index
After generating reports, update docs/codebase-analytics/README.md with links to all reports and a quick summary table.
Report Specifications
Stats (stats.md)
Data collection:
# Overall summary
cloc --vcs=git --quiet
# Per-module breakdown (adapt directory patterns to the project)
for dir in <module_dirs>; do
name=$(basename "$dir")
cloc --vcs=git "$dir" --include-lang=<primary_lang> --quiet --csv 2>/dev/null | grep '<primary_lang>' | awk -F, '{print $NF}'
done
Required sections:
- Overall summary table (language, files, blank, comment, code)
- Per-module source LOC breakdown (sorted by size descending)
- Per-module test LOC breakdown
- Distribution visualization (text-based bar chart)
- Test coverage ratio by module (test LOC / source LOC)
Notes section explaining what was counted and what was excluded.
Dependency Graph (dependency-graph.md)
Data collection:
- Read all package manifests to extract declared dependencies
- For monorepos, map which internal modules depend on which
Required sections:
- Module hierarchy with dependency levels (level 0 = no deps, level N = depends on level N-1)
- ASCII dependency diagram showing direction of dependencies
- Inter-module dependency table (what depends on what, why)
- Key constraints (circular dependency prevention, layering rules)
Formatting:
- Use
→ for dependency direction
- Group by layer (foundation, infrastructure, feature, orchestrator)
- Note any circular or unusual dependencies
Third-Party Dependencies (third-party-dependencies.md)
Data collection:
- Parse package manifests for external dependency declarations
- Extract: package name, version constraint, URL/registry
- Check lock files for transitive dependencies
Required sections:
- Direct dependencies table (name, version, URL, used in which modules, purpose)
- Per-dependency description — what it does and why it was chosen
- Transitive dependencies table (name, pulled by, purpose)
- Dependency minimalism notes — what the project deliberately does NOT use externally and why
If ADRs exist for dependency choices, link to them.
Module Coupling (module-coupling.md)
Data collection:
# Count import/require/use statements per module
# Adapt the import pattern to the language:
# Swift: ^import ModuleName
# TypeScript: ^import .* from ['"]module
# Python: ^(from module|import module)
# Go: "module/path"
# Rust: ^use crate::module
# Java: ^import package\.module
grep -r "^import " <module>/Sources/ | sort | uniq -c | sort -rn
Required sections:
- Import frequency table by module (rows = importing module, columns = imported module)
- Observations — which modules have unusually high or low coupling
- Cross-module coupling — which modules import other same-level modules (horizontal coupling)
- Coupling health summary table (metric, value, assessment)
Largest Files (largest-files.md)
Data collection:
git ls-files '*.<ext>' | xargs wc -l | sort -rn | head -25
Required sections:
- Top 20 files by line count (rank, lines, file path, category)
- Distribution by category (test, view/UI, model, service, etc.)
- Notable source files (non-test) — the largest non-test files with brief descriptions of why they are large and whether they warrant attention
Service Inventory (service-inventory.md)
Applicability: Only generate this report if the codebase uses an interface/implementation pattern (protocols, interfaces, abstract classes, traits).
Data collection:
- Find all interface/protocol definitions
- For each, find implementations and test doubles
- Find dependency registration/wiring (DI container, module declarations)
Required sections:
- Overview — total count, where interfaces/implementations/mocks live
- Mapping table grouped by domain (name, implementation(s), mock, purpose)
- Implementation patterns — naming conventions, multi-implementation services
- Service directory layout (tree view of implementation files)
Output Directory Structure
docs/
└── codebase-analytics/
├── README.md ← Index with summary + regeneration commands
├── stats.md ← Lines of code
├── dependency-graph.md ← Internal module DAG
├── third-party-dependencies.md ← External packages
├── module-coupling.md ← Import frequency analysis
├── largest-files.md ← Complexity hotspots
└── service-inventory.md ← Interface-implementation mapping
README Index Format
# Codebase Analytics
Quantitative analysis of the codebase. These reports are point-in-time snapshots.
**Last updated:** YYYY-MM-DD
## Reports
| Report | Description |
|--------|-------------|
| [stats.md](stats.md) | Lines of code by language, directory, and module |
| [dependency-graph.md](dependency-graph.md) | Internal module dependency DAG |
| ... |
## Quick Summary
| Metric | Value |
|--------|-------|
| Total source LOC | X |
| Files | X |
| Modules | X |
| External dependencies | X |
| Test LOC ratio | X% |
## Regenerating
<shell commands to regenerate each report>
Guidelines
- Language-agnostic — adapt import patterns, file extensions, and package manifests to whatever language the project uses. Do not assume any specific language.
- Git-aware — always use
--vcs=git or git ls-files to exclude generated files, build artifacts, and vendored dependencies.
- Point-in-time — every report includes the generation date. Reports become stale as the codebase evolves.
- Non-destructive — if reports already exist, regenerate them in place (overwrite with fresh data). Never delete reports the user may have manually edited without asking.
- Focused — the user can request a single report (e.g., "just the dependency graph") or all reports. Default to all when the user says "analyze the codebase".
- Actionable observations — don't just list data. Add brief observations highlighting unusual patterns, potential issues, or notable strengths.
- Link to other docs — if ADRs, feature docs, or architecture docs exist in the project, link to them where relevant.
1---2name: codebase-analysis3description: Analyze a codebase and generate quantitative reports (LOC stats, dependency graph, module coupling, largest files, service/interface inventory, third-party dependencies). Outputs markdown reports to docs/codebase-analytics/. Use when the user asks to analyze, profile, or get an overview of a codebase.4---56# Codebase Analysis78Generate quantitative, language-agnostic analysis reports for any codebase. Reports are point-in-time snapshots saved as markdown in `docs/codebase-analytics/`.910## When to Activate1112- User asks to "analyze the codebase", "generate codebase report", or "profile the project"13- User wants an overview of project size, structure, dependencies, or complexity14- User asks about module coupling, dependency graphs, or largest files15- User says "codebase analytics" or "codebase analysis"1617## Prerequisites1819### cloc (required for stats report)2021Check if installed:22```23which cloc24```2526If not found, install it:27- macOS: `brew install cloc`28- npm: `npm install -g cloc`29- Debian/Ubuntu: `sudo apt install cloc`30- Red Hat/Fedora: `sudo yum install cloc`3132### git (required for all reports)3334The project must be a git repository. All file discovery uses `git ls-files` or `cloc --vcs=git` to respect `.gitignore` and exclude generated/vendored files.3536## Reports3738The skill produces up to 6 reports. The user can request all reports or specific ones.3940| Report | File | What It Answers |41|--------|------|-----------------|42| **Stats** | `stats.md` | How big is the codebase? What languages? How much test code? |43| **Dependency Graph** | `dependency-graph.md` | How do internal modules depend on each other? |44| **Third-Party Dependencies** | `third-party-dependencies.md` | What external packages are used and why? |45| **Module Coupling** | `module-coupling.md` | How tightly coupled are modules? (import frequency) |46| **Largest Files** | `largest-files.md` | Where are the complexity hotspots? |47| **Service Inventory** | `service-inventory.md` | What interfaces exist and what implements them? |4849## Workflow5051### 1. Initialize5253If `docs/codebase-analytics/` does not exist, create it with a `README.md` index.5455### 2. Detect Project Structure5657Before generating reports, understand the project layout:58591. **Identify the primary language(s)** — run `cloc --vcs=git --quiet` to see the language breakdown602. **Identify module boundaries** — look for:61 - Package manifests: `Package.swift`, `package.json`, `Cargo.toml`, `go.mod`, `build.gradle`, `pyproject.toml`, `*.csproj`, `pom.xml`62 - Module directories: `src/`, `lib/`, `packages/`, `modules/`, `features/`, `apps/`63 - Test directories: `tests/`, `test/`, `__tests__/`, `*Tests/`, `spec/`643. **Identify dependency management** — look for lock files and dependency declarations654. **Identify interface/implementation patterns** — protocols, interfaces, abstract classes, traits6667### 3. Generate Reports6869For each requested report, follow the format specification below. Always include `**Generated:** YYYY-MM-DD` at the top of each report.7071### 4. Update Index7273After generating reports, update `docs/codebase-analytics/README.md` with links to all reports and a quick summary table.7475## Report Specifications7677### Stats (`stats.md`)7879**Data collection:**80```bash81# Overall summary82cloc --vcs=git --quiet8384# Per-module breakdown (adapt directory patterns to the project)85for dir in <module_dirs>; do86 name=$(basename "$dir")87 cloc --vcs=git "$dir" --include-lang=<primary_lang> --quiet --csv 2>/dev/null | grep '<primary_lang>' | awk -F, '{print $NF}'88done89```9091**Required sections:**921. Overall summary table (language, files, blank, comment, code)932. Per-module source LOC breakdown (sorted by size descending)943. Per-module test LOC breakdown954. Distribution visualization (text-based bar chart)965. Test coverage ratio by module (test LOC / source LOC)9798**Notes section** explaining what was counted and what was excluded.99100---101102### Dependency Graph (`dependency-graph.md`)103104**Data collection:**105- Read all package manifests to extract declared dependencies106- For monorepos, map which internal modules depend on which107108**Required sections:**1091. Module hierarchy with dependency levels (level 0 = no deps, level N = depends on level N-1)1102. ASCII dependency diagram showing direction of dependencies1113. Inter-module dependency table (what depends on what, why)1124. Key constraints (circular dependency prevention, layering rules)113114**Formatting:**115- Use `→` for dependency direction116- Group by layer (foundation, infrastructure, feature, orchestrator)117- Note any circular or unusual dependencies118119---120121### Third-Party Dependencies (`third-party-dependencies.md`)122123**Data collection:**124- Parse package manifests for external dependency declarations125- Extract: package name, version constraint, URL/registry126- Check lock files for transitive dependencies127128**Required sections:**1291. Direct dependencies table (name, version, URL, used in which modules, purpose)1302. Per-dependency description — what it does and why it was chosen1313. Transitive dependencies table (name, pulled by, purpose)1324. Dependency minimalism notes — what the project deliberately does NOT use externally and why133134If ADRs exist for dependency choices, link to them.135136---137138### Module Coupling (`module-coupling.md`)139140**Data collection:**141```bash142# Count import/require/use statements per module143# Adapt the import pattern to the language:144# Swift: ^import ModuleName145# TypeScript: ^import .* from ['"]module146# Python: ^(from module|import module)147# Go: "module/path"148# Rust: ^use crate::module149# Java: ^import package\.module150151grep -r "^import " <module>/Sources/ | sort | uniq -c | sort -rn152```153154**Required sections:**1551. Import frequency table by module (rows = importing module, columns = imported module)1562. Observations — which modules have unusually high or low coupling1573. Cross-module coupling — which modules import other same-level modules (horizontal coupling)1584. Coupling health summary table (metric, value, assessment)159160---161162### Largest Files (`largest-files.md`)163164**Data collection:**165```bash166git ls-files '*.<ext>' | xargs wc -l | sort -rn | head -25167```168169**Required sections:**1701. Top 20 files by line count (rank, lines, file path, category)1712. Distribution by category (test, view/UI, model, service, etc.)1723. Notable source files (non-test) — the largest non-test files with brief descriptions of why they are large and whether they warrant attention173174---175176### Service Inventory (`service-inventory.md`)177178**Applicability:** Only generate this report if the codebase uses an interface/implementation pattern (protocols, interfaces, abstract classes, traits).179180**Data collection:**181- Find all interface/protocol definitions182- For each, find implementations and test doubles183- Find dependency registration/wiring (DI container, module declarations)184185**Required sections:**1861. Overview — total count, where interfaces/implementations/mocks live1872. Mapping table grouped by domain (name, implementation(s), mock, purpose)1883. Implementation patterns — naming conventions, multi-implementation services1894. Service directory layout (tree view of implementation files)190191## Output Directory Structure192193```194docs/195└── codebase-analytics/196 ├── README.md ← Index with summary + regeneration commands197 ├── stats.md ← Lines of code198 ├── dependency-graph.md ← Internal module DAG199 ├── third-party-dependencies.md ← External packages200 ├── module-coupling.md ← Import frequency analysis201 ├── largest-files.md ← Complexity hotspots202 └── service-inventory.md ← Interface-implementation mapping203```204205## README Index Format206207```markdown208# Codebase Analytics209210Quantitative analysis of the codebase. These reports are point-in-time snapshots.211212**Last updated:** YYYY-MM-DD213214## Reports215216| Report | Description |217|--------|-------------|218| [stats.md](stats.md) | Lines of code by language, directory, and module |219| [dependency-graph.md](dependency-graph.md) | Internal module dependency DAG |220| ... |221222## Quick Summary223224| Metric | Value |225|--------|-------|226| Total source LOC | X |227| Files | X |228| Modules | X |229| External dependencies | X |230| Test LOC ratio | X% |231232## Regenerating233234<shell commands to regenerate each report>235```236237## Guidelines238239- **Language-agnostic** — adapt import patterns, file extensions, and package manifests to whatever language the project uses. Do not assume any specific language.240- **Git-aware** — always use `--vcs=git` or `git ls-files` to exclude generated files, build artifacts, and vendored dependencies.241- **Point-in-time** — every report includes the generation date. Reports become stale as the codebase evolves.242- **Non-destructive** — if reports already exist, regenerate them in place (overwrite with fresh data). Never delete reports the user may have manually edited without asking.243- **Focused** — the user can request a single report (e.g., "just the dependency graph") or all reports. Default to all when the user says "analyze the codebase".244- **Actionable observations** — don't just list data. Add brief observations highlighting unusual patterns, potential issues, or notable strengths.245- **Link to other docs** — if ADRs, feature docs, or architecture docs exist in the project, link to them where relevant.