Giter VIP home page Giter VIP logo

fluent.codegen's Introduction

Fluent.CodeGen

This library provides a fluent api that is responsible for generating C# human readable source code. It can generate Class, Method, Constructor, Fields, Properties, Enum and so on.

nuget nuget version

Installation

In order to install the library you must run:

dotnet add package Fluent.CodeGen

Examples

Many examples can be found on unit tests

var fieldTest = new FieldGen("string", "test")
    .Public()
    .Static();

var fieldAmount = new FieldGen("int", "amount")
    .Assign("10");

var classGen = new ClassGen(name: "Program");

var generatedCode = classGen
    .Using("System")
    .Namespace("My.Test")
    .Public()
    .WithField(fieldTest)
    .WithField(fieldAmount)
    .Constructor(ctor => ctor.Public())
    .GenerateCode();

The generated code will be:

using System;

namespace My.Test
{
    public class Program
    {
        public static string test;
        int amount = 10;

        public Program()
        {
        }

    }
}

fluent.codegen's People

Contributors

alanep0922 avatar lucassklp avatar

Stargazers

Jonathan P. Marinho avatar Michael Tanczos avatar  avatar

Watchers

 avatar  avatar Andre Mariano Neres avatar

fluent.codegen's Issues

feature: Add Methods property on ClassGen

Api suggestion:

var classGen = new ClassGen();
//...

var methods = classGen.Methods;

Method class suggestion:

public class Method
{
    public string Name {get;}
    public string Type {get;}
    public List<Parameter> Parameters {get;}
    public string Body {get;}
}

Parameter class suggestion:

public class Parameter
{
    public string Name {get;}
    public string Type {get;}
}

bug: Performing code generation of PropertyGen multiple times breaks the code

Running this code below:

var classGen = new ClassGen("Test")
    .WithMethod(new MethodGen("int", "Count").Public());

string firstMethodCall = classGen.GenerateCode();
string secondMethodCall = classGen.GenerateCode();

the firstMethodCall output is

class Test
{
    public int Count { get; set; }
}

while secondMethodCall output is

class Test
{
    public int Count { get; set; }public int Count { get; set; }
}

They're expected to be equal.

feature: Attributes on class, fields, properties and methods

Api suggestion:

var classGen = new ClassGen(name: "Test")
    .WithAttribute("[MyAttribute]");

// Will generate:

// [MyAttribute]
// class Test
// {
// }

var fieldGen = new FieldGen(name: "Test", type: "string")
    .WithAttribute("[MyAttribute]");

// Will generate:

// [MyAttribute]
// string Test;

var propertyGen = new PropertyGen(name: "Test", type: "string")
    .WithAttribute("[MyAttribute]");

// Will generate:

// [MyAttribute]
// string Test { get; set; }

var methodGen = new MethodGen(name: "Test", type: "string")
    .WithAttribute("[MyAttribute]");

// Will generate:

// [MyAttribute]
// string Test()
// {
// }

feature: EnumGen

var enumGen = new EnumGen(name: "MyEnum")
    .WithValue("MyEnumValue") //order matters here.
    .WithValue("MySecondValue", 2)
    .WithValue("MyThirdValue");

Will generate

enum MyEnum
{
    MyEnumValue,
    MySecondValue = 2,
    MyThirdValue,
}

feature: Property Gen

API proposal

var classGen = new ClassGen("Test")
    .WithProperty(new PropertyGen("int", "Count")
        .Public()
        .NoSet())
    .WithField(new FieldGen("string", "name").Private())
    .WithProperty(new PropertyGen("string", "Name")
            .Public()
            .WithGet("this.name")
            .WithSet("this.name = value"));

Output

class Test
{
    public int Count { get; }
    private string name;
    public string Name { get => this.name; set => this.name = value; }
}

bug: Code generation fails when it is performed more than once

The code is generated correctly only on first call. Subsequent calls will generate incorrect code.

Steps to reproduce:

var classGen = new ClassGen("Test")
    .WithField(new FieldGen("int", "count").Public());

// The first call generates correct code
string firstMethodCall = classGen.GenerateCode();

// From this point, the code generated is incorrect
string secondMethodCall = classGen.GenerateCode();

Expected result (on secondMethodCall):

class Test
{
    public int count;

}

Actual result (on secondMethodCall):

class Test
{
    public int count;

}class Test
{
    public int count;public int count;

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.