PatternForge
PatternForge is a CLI tool that analyzes existing codebases to learn your coding patterns, naming conventions, and project structure, then generates consistent boilerplate code for new files, components, and modules that match your established style.
Features
- Multi-language Pattern Detection: Supports Python, JavaScript, TypeScript, Java, C++, C, Rust, Go, and Ruby
- Automatic Boilerplate Generation: Creates consistent code based on detected patterns
- Customizable Templates: Uses Jinja2 templates with pattern enrichment
- Team Pattern Sharing: Export and import patterns for consistent styling across teams
- Interactive CLI Interface: Beautiful terminal UI powered by Rich
Installation
From PyPI
pip install patternforge
From Source
git clone https://7000pct.gitea.bloupla.net/7000pctAUTO/patternforge.git
cd patternforge
pip install -e .
Quick Start
1. Analyze a codebase to learn its patterns
patternforge analyze ./my-project --language python --output patterns.yaml
2. Create a template from detected patterns
patternforge template create my-component --pattern patterns.yaml
3. Generate boilerplate
patternforge generate my-component --output ./src/new-feature --name UserService
Commands Overview
| Command | Description |
|---|---|
analyze |
Analyze a codebase and extract patterns |
template create |
Create a new template from detected patterns |
template list |
List all available templates |
template remove |
Remove a template |
generate |
Generate boilerplate from a template |
export |
Export patterns or templates for team sharing |
import |
Import patterns from team repository |
config |
View or modify configuration |
Pattern Detection
PatternForge uses Tree-sitter for static analysis to detect:
- Naming Conventions: camelCase, PascalCase, snake_case, SCREAMING_SNAKE_CASE
- Code Structure: class definitions, function signatures, import patterns
- Project Organization: directory structure, file naming patterns
- Style Patterns: indentation, brace placement, comment styles
Supported Languages
- Python (.py, .pyi)
- JavaScript (.js, .mjs)
- TypeScript (.ts, .tsx)
- Java (.java)
- C++ (.cpp, .cc, .cxx, .hpp)
- C (.c, .h)
- Rust (.rs)
- Go (.go)
- Ruby (.rb)
Template Management
Templates are Jinja2 templates enriched with pattern data:
# Create a template from detected patterns
patternforge template create component --pattern patterns.yaml
# List all templates
patternforge template list
# Remove a template
patternforge template remove component
# Use a custom template file
patternforge template create api-endpoint --pattern patterns.yaml --template ./my-template.j2
Template Variables
Templates have access to the following variables:
project_name: Name of the projectentity_name: Name of the entity being generatedclass_name: PascalCase version of the namefunction_name: camelCase/snake_case version of the namefields: List of field namesmethods: List of method dictionaries with name, params, docstringparams: Comma-separated parametersdocstring: Generated docstring
Team Sharing
Share patterns and templates across your team:
# Export patterns for sharing
patternforge export patterns.yaml --team team-patterns/
# Import patterns from team repository
patternforge import team-patterns/
Supports both YAML and JSON formats for export.
Configuration
Configuration file location: ~/.config/patternforge/config.yaml
# Default configuration
default_patterns: ~/.config/patternforge/patterns
template_dir: ~/.config/patternforge/templates
language: python
indent_size: 4
Configuration Options
| Option | Description | Default |
|---|---|---|
default_patterns |
Directory for pattern files | ~/.config/patternforge/patterns |
template_dir |
Directory for templates | ~/.config/patternforge/templates |
language |
Default programming language | python |
indent_size |
Default indent size | 4 |
View current configuration:
patternforge config
Examples
Analyzing a Python Project
patternforge analyze ./my-python-app --language python --output my-patterns.yaml
Creating a React Component Template
# Analyze existing components
patternforge analyze ./react-app/src/components --language typescript --output react-patterns.yaml
# Create a template
patternforge template create button --pattern react-patterns.yaml
# Generate a new component
patternforge generate button --output ./src/components/Button.tsx --name SubmitButton
Using a Custom Template
patternforge template create api-endpoint --pattern patterns.yaml --template ./custom-template.j2
patternforge generate api-endpoint --data ./data.json --output ./endpoints/
Batch Generation
from patternforge.generator import BoilerplateGenerator
from patternforge.config import Config
config = Config()
generator = BoilerplateGenerator(config)
entities = [
{"name": "UserService", "data_file": "user_data.json"},
{"name": "ProductService", "data_file": "product_data.json"},
{"name": "OrderService"},
]
generator.generate_multiple("service", "./src/services", entities)
Architecture
patternforge/
├── src/patternforge/
│ ├── __init__.py # Package initialization
│ ├── cli.py # Click CLI commands
│ ├── analyzer.py # Code analysis with Tree-sitter
│ ├── config.py # Configuration management
│ ├── generator.py # Boilerplate generation
│ └── template.py # Template management
├── tests/
│ ├── test_analyzer.py # Analyzer tests
│ ├── test_cli.py # CLI tests
│ ├── test_config.py # Config tests
│ ├── test_generator.py # Generator tests
│ └── test_template.py # Template tests
├── pyproject.toml # Project configuration
└── README.md # This file
Development
Setup
git clone https://7000pct.gitea.bloupla.net/7000pctAUTO/patternforge.git
cd patternforge
pip install -e ".[dev]"
Running Tests
pytest tests/ -v
Linting
ruff check .
mypy src/
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Tree-sitter for powerful code analysis
- Click for the CLI framework
- Rich for beautiful terminal output
- Jinja2 for template rendering