Git Product home page Git Product logo

testura.code's Introduction

Testura Logo

Testura.Code is a wrapper around the Roslyn API and used for generation, saving and compiling C# code. It provides methods and helpers to generate classes, methods, statements and expressions.

It provide helpers to generate:

  • Classes
  • Methods
  • Parameters
  • Arguments
  • Attributes
  • Fields
  • Properties

But also simple statements like:

  • Declaration statements (for example declare and assign variables)
  • Iterations statements (for example for-loop)
  • Jump statements (for example return)
  • Selection statement (for example if-statements)
  • Expression statements (for example invoke methods)

Install

NuGet NuGet Status

https://www.nuget.org/packages/Testura.Code

PM> Install-Package Testura.Code

Usage

Testura.Code have three different types of helpers:

  • Generators - The most basic kinds of code generators, for example fields, properties and modifiers.
  • Statement - Helpers for regular statements and expressions, for example declare and assign a variable or invoke a method.
  • Builders - Currently we have two builder - One class builder and one method builder. These have the highest abstraction and are easy to use.

Documentation

Examples

Hello world

Here is an example on how to generate, save and compile a simple hello world.

Generate

var @class = new ClassBuilder("Program", "HelloWorld")
	.WithUsings("System") 
	.WithModifiers(Modifiers.Public)
	.WithMethods(
		new MethodBuilder("Main")
		.WithModifiers(Modifiers.Public, Modifiers.Static)
		.WithParameters(new Parameter("args", typeof(string[])))
		.WithBody(
			BodyGenerator.Create(
				Statement.Expression.Invoke("Console", "WriteLine", new List<IArgument>() { new ValueArgument("Hello world") }).AsStatement(),
				Statement.Expression.Invoke("Console", "ReadLine").AsStatement()
				))
		.Build())
	.Build();

This code will generate following code:

using System;

namespace HelloWorld
{
   public class Program
   {
       public static void Main(String[] args)
       {
           Console.WriteLine("Hello world");
           Console.ReadLine();
       }
   }
}

Save

var saver = new CodeSaver();

// As a string
var generatedCode = saver.SaveCodeAsString(@class);

// Or to file
saver.SaveCodeToFile(@class, @"/path/HelloWorld.cs");

Compile

var compiler = new Compiler();

//To a dll

// From string
var result = await compiler.CompileSourceAsync(@"/path/HelloWorld.dll", generatedCode);

// From file
var result = await compiler.CompileFilesAsync(@"/path/HelloWorld.dll",  @"/path/HelloWorld.cs");

//In memory (without creating a dll)

// From string
var result = await compiler.CompileSourceInMemoryAsync(generatedCode);

// From file
var result = await compiler.CompileFilesInMemoryAsync(@"/path/HelloWorld.cs");

Missing anything?

If we miss a feature, syntax or statements - just create an issue or contact us and I'm sure we can add it.

It is also possible for you to contribute with your own feature. Simply add a pull request and we will look at it.

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Contact

Visit www.testura.net, twitter at @testuranet or email at [email protected]

testura.code's People

Contributors

br0xy avatar millebo avatar weshaggard avatar markgaze avatar forcewake avatar

Watchers

 avatar

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.