Skip to content

fengb3/EasyCodeBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Easy Code Builder

NuGet Version License: MIT CI NuGet

A powerful and flexible library for dynamic code generation, supporting multiple programming languages including C#, Python, and Lisp.

✨ Features

  • πŸš€ Fluent API: Easy-to-use method chaining for intuitive code building
  • 🎯 Multi-Language Support: Generate C#, Python, and Lisp code
  • πŸ”§ Configurable Indentation: Support for spaces, tabs, and custom indentation
  • πŸ“š Rich Code Constructs: Classes, methods, properties, control structures, and more
  • ⚑ High Performance: Optimized string building with caching mechanisms
  • πŸ›‘οΈ Type Safety: Generic-based design ensures compile-time safety
  • πŸ“– XML Documentation: Complete IntelliSense support

πŸš€ Quick Start

Installation

dotnet add package Fengb3.EasyCodeBuilder

Basic Usage

API Calling:

using Fengb3.EasyCodeBuilder.Csharp;

var builder = new CSharpCodeBuilder();

var code = builder
    .Using("System", "System.Collections.Generic")
    .Namespace("MyProject", ns => ns
        .Class("Person", cls => cls
            .Property("string", "Name", accessors: "{ get; set; }")
            .Property("int", "Age", accessors: "{ get; set; }")
            .Method("GetInfo", method => method
                .AppendLine("return $\"Name: {Name}, Age: {Age}\";")
            , returnType: "string")
        )
    )
    .ToString();

Generated Code:

using System;
using System.Collections.Generic;

namespace MyProject
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        
        public string GetInfo()
        {
            return $"Name: {Name}, Age: {Age}";
        }
    }
}

πŸ“š Documentation

For detailed documentation and examples, please refer to:

πŸ—οΈ Supported Languages

Language Status Builder Class
C# βœ… Full Support CSharpCodeBuilder
Python βœ… Full Support PythonCodeBuilder
Lisp βœ… Basic Support LispCodeBuilder

🎯 Key Components

Core Classes

  • CodeBuilder<T> - Abstract base class with core functionality
  • CSharpCodeBuilder - C# specific code generation
  • PythonCodeBuilder - Python specific code generation
  • LispCodeBuilder - Lisp specific code generation

Features

  • Fluent API - Method chaining for readable code
  • Indentation Management - Automatic and configurable indentation
  • Code Blocks - Structured code generation with proper nesting
  • Performance Optimized - Efficient string building and caching

πŸ“Š Examples

Generate a Complete C# Class

API Calling:

var code = new CSharpCodeBuilder()
    .Using("System")
    .Namespace("MyApp.Models", ns => ns
        .Class("User", cls => cls
            .Field("string", "_name", "private")
            .Property("string", "Name", 
                modifiers: "public",
                accessors: "{ get => _name; set => _name = value ?? throw new ArgumentNullException(); }")
            .Method("ToString", method => method
                .AppendLine("return $\"User: {Name}\";")
            , returnType: "string", modifiers: "public override")
        )
    );

Generated Code:

using System;

namespace MyApp.Models
{
    public class User
    {
        private string _name;
        
        public string Name 
        { 
            get => _name; 
            set => _name = value ?? throw new ArgumentNullException(); 
        }
        
        public override string ToString()
        {
            return $"User: {Name}";
        }
    }
}

Generate Python Code

API Calling:

var pythonCode = new PythonCodeBuilder()
    .Import("json", "datetime")
    .AppendLine()
    .Class("User", cls => cls
        .Function("__init__", init => init
            .AppendLine("self.name = name")
            .AppendLine("self.created_at = datetime.datetime.now()")
        , "self, name: str")
        .AppendLine()
        .Function("to_dict", func => func
            .AppendLine("return {")
            .AppendLine("    'name': self.name,")
            .AppendLine("    'created_at': self.created_at.isoformat()")
            .AppendLine("}")
        , "self", "dict")
    );

Generated Code:

import json
import datetime

class User:
    def __init__(self, name: str):
        self.name = name
        self.created_at = datetime.datetime.now()
    
    def to_dict(self) -> dict:
        return {
            'name': self.name,
            'created_at': self.created_at.isoformat()
        }

Control Structures Example

API Calling:

var code = new CSharpCodeBuilder()
    .Namespace("Examples", ns => ns
        .Class("Calculator", cls => cls
            .Method("Divide", method => method
                .If("b == 0", ifBlock => ifBlock
                    .AppendLine("throw new DivideByZeroException(\"Cannot divide by zero\");")
                )
                .AppendLine("return a / b;")
            , returnType: "double", parameters: "double a, double b")
        )
    );

Generated Code:

namespace Examples
{
    public class Calculator
    {
        public double Divide(double a, double b)
        {
            if (b == 0)
            {
                throw new DivideByZeroException("Cannot divide by zero");
            }
            return a / b;
        }
    }
}

🀝 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.

πŸ”— Links


Made with ❀️ by Bohan Feng

About

A library for dynamic code generation, supporting Multiple Languages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages