Generator System
The Azu CLI generator system is responsible for creating new files, code structures, and project scaffolding. It provides a flexible, extensible framework for code generation using templates and dynamic content.
Overview
The generator system follows a modular architecture that separates concerns between different types of generators while sharing common functionality through a base generator class. Each generator is responsible for creating specific types of files and follows consistent patterns.
Architecture
┌─────────────────────────────────────────────────────────────┐
│ Generator System │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Base Gen. │ │ Model Gen. │ │ Endpoint Gen│ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Service Gen│ │ Page Gen. │ │ Contract Gen│ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Middleware │ │ Component │ │ Validator │ │
│ │ Generator │ │ Generator │ │ Generator │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ Template Engine (ECR) │
├─────────────────────────────────────────────────────────────┤
│ File System │
└─────────────────────────────────────────────────────────────┘Core Components
Base Generator
All generators inherit from the base generator class, which provides common functionality:
Attribute System
The generator system includes a flexible attribute system for defining model fields:
Generator Types
Model Generator
Creates CQL ORM models with proper structure and validations:
Endpoint Generator
Creates HTTP endpoints with contracts and pages:
Service Generator
Creates domain services for business logic:
Component Generator
Creates interactive UI components with real-time features:
Validator Generator
Creates custom CQL validators:
Template System
ECR Templates
The generator system uses Crystal's ECR (Embedded Crystal) for template rendering:
Template Variables
Templates have access to a rich set of variables:
Template Organization
Templates are organized by generator type:
Generator Registry
The generator system includes a registry for managing available generators:
Command Integration
Generate Command
The generate command orchestrates the generator system:
Error Handling
Validation
The generator system includes comprehensive validation:
File Safety
The generator system ensures safe file operations:
Performance Considerations
Template Caching
Templates are cached for better performance:
Batch Operations
The generator system supports batch operations for multiple files:
Extensibility
Custom Generators
Users can create custom generators by extending the base class:
Template Customization
Users can customize templates by creating their own template files:
Best Practices
Generator Design
Single Responsibility: Each generator should have one clear purpose
Consistent Interface: Follow the base generator interface
Error Handling: Provide meaningful error messages
Validation: Validate inputs before generation
Documentation: Document generator options and behavior
Template Design
Readable Code: Generate clean, readable code
Consistent Style: Follow Crystal coding conventions
Flexible: Make templates adaptable to different use cases
Well-Documented: Include comments in generated code
Testable: Generate code that's easy to test
Performance
Template Caching: Cache frequently used templates
Batch Operations: Support generating multiple files efficiently
Lazy Loading: Load templates only when needed
Memory Management: Clean up resources after generation
Related Documentation
Template Engine (ECR) - ECR template system details
CLI Framework (Topia) - Command-line interface framework
Configuration System - Configuration management
Commands Reference - Generate command documentation
Last updated