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

  1. Single Responsibility: Each generator should have one clear purpose

  2. Consistent Interface: Follow the base generator interface

  3. Error Handling: Provide meaningful error messages

  4. Validation: Validate inputs before generation

  5. Documentation: Document generator options and behavior

Template Design

  1. Readable Code: Generate clean, readable code

  2. Consistent Style: Follow Crystal coding conventions

  3. Flexible: Make templates adaptable to different use cases

  4. Well-Documented: Include comments in generated code

  5. Testable: Generate code that's easy to test

Performance

  1. Template Caching: Cache frequently used templates

  2. Batch Operations: Support generating multiple files efficiently

  3. Lazy Loading: Load templates only when needed

  4. Memory Management: Clean up resources after generation

Last updated