# PatternForge [![CI](https://7000pct.gitea.bloupla.net/7000pctAUTO/patternforge/actions/workflows/ci.yml/badge.svg)](https://7000pct.gitea.bloupla.net/7000pctAUTO/patternforge/actions) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/) 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 ```bash pip install patternforge ``` ### From Source ```bash 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 ```bash patternforge analyze ./my-project --language python --output patterns.yaml ``` ### 2. Create a template from detected patterns ```bash patternforge template create my-component --pattern patterns.yaml ``` ### 3. Generate boilerplate ```bash 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: ```bash # 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 project - `entity_name`: Name of the entity being generated - `class_name`: PascalCase version of the name - `function_name`: camelCase/snake_case version of the name - `fields`: List of field names - `methods`: List of method dictionaries with name, params, docstring - `params`: Comma-separated parameters - `docstring`: Generated docstring ## Team Sharing Share patterns and templates across your team: ```bash # 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` ```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: ```bash patternforge config ``` ## Examples ### Analyzing a Python Project ```bash patternforge analyze ./my-python-app --language python --output my-patterns.yaml ``` ### Creating a React Component Template ```bash # 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 ```bash patternforge template create api-endpoint --pattern patterns.yaml --template ./custom-template.j2 patternforge generate api-endpoint --data ./data.json --output ./endpoints/ ``` ### Batch Generation ```python 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 ```bash git clone https://7000pct.gitea.bloupla.net/7000pctAUTO/patternforge.git cd patternforge pip install -e ".[dev]" ``` ### Running Tests ```bash pytest tests/ -v ``` ### Linting ```bash 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](LICENSE) file for details. ## Acknowledgments - [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) for powerful code analysis - [Click](https://click.palletsprojects.com/) for the CLI framework - [Rich](https://rich.readthedocs.io/) for beautiful terminal output - [Jinja2](https://jinja.palletsprojects.com/) for template rendering