Sharp Template engine

SharpTemplate

If it would be needed to generate files in runtime, these classes can be used in conjunction with the compilation utilities to compile the templates.

The target is to have a system like the one used by ASP.NET to compile the Razor templates into class. If you need it you could use these class as Razor classes :) I will add a specific post about this in the near future!

Keywords

Usage - Input and Output

Given a template with the following content, like into the unit tests of the sharp template project (Resources/ClassTemplate.cs)

<#using# SharpTemplate.Test.Resources#>
<#using#SharpTemplate.Test#>
<#model# ParseCompileModel#>
using System;
using System.Collections.Generic;

namespace TestNamespace
{
    public class <# Write(model.ClassName); #>{

        public  <# Write(model.ClassName); #>()
        {
        
        }

        public List<string> <# Write(model.MethodName); #>()
        {
            var result = new List<string>();
            for(int i = 0;i<<# Write(model.UpTo.ToString()); #>;i++){
                result.Add(i.ToString());
            }
            return result;
        }
    }
}

Given a model, with the following value

The following class will be created calling the ISharpResult class

using System;
using System.Collections.Generic;
using SharpTemplate.Test;
using SharpTemplate.Test.Resources;

namespace TestNamespace
{
    public class ResultingClass{

        public  ResultingClass()
        {
        
        }

        public List<string> MethodToInvoke()
        {
            var result = new List<string>();
            for(int i = 0;i<10;i++){
                result.Add(i.ToString());
            }
            return result;
        }
    }
}

Usage - The compilation

To create the template the compilation utils must be used, for example (see the next paragraph for the compilation utils specific stuff)

        const string dllName = "CompileClassTemplate";
        const string resultDllName = "CompileResultingTemplate";
        var path = TestUtils.GetExecutionPath();
        var source = ResourceContentLoader.LoadText("ClassTemplate.cs", Assembly.GetExecutingAssembly());

        var pp = new SharpParser();
        var sharpClass = pp.ParseClass(source, "TestParserClass", "SharpTemplate.Test.Resources.Parsers");

        var loadedAssembly = BuildParserClass(dllName, path, sharpClass);

        var content = File.ReadAllBytes(loadedAssembly);
        var compileSimpleObjectAsm = Assembly.Load(content);
        var instance = (ISharpResult)Activator.CreateInstance(compileSimpleObjectAsm.FullName,
            "SharpTemplate.Test.Resources.Parsers.TestParserClass").Unwrap();


        var model = new ParseCompileModel("ResultingClass", "MethodToInvoke", 10);
        instance.Execute(model);
        var writtenModel = instance.Content;

        var resultingAssembly = BuildGenericClass(resultDllName, path, "TestNamespace", "ResultingClass",writtenModel);
        content = File.ReadAllBytes(resultingAssembly);
        compileSimpleObjectAsm = Assembly.Load(content);

        var instanceFinal = Activator.CreateInstance(compileSimpleObjectAsm.FullName, "TestNamespace.ResultingClass").Unwrap();
        var method = instanceFinal.GetType().GetMethod("MethodToInvoke");
        
        List<string> result = method.Invoke(instanceFinal, new object[] { }) as List<string>;

Suggestion

One thing for which i use this parser is to generate files inside the Visual Studio Extension. With this i can create parser that given a certain model inpute (xml, specific class) can create a complex class using the C# syntax like i could do with cshtml and razor templates.

Compilation Utils

This is a bunch of classes that could be useful to compile objects in runtime. All unit-tested.

Usage

    const string dllName = "CompileSimpleObject";
    var path = Path.GetTempPath();
    var source = ResourceContentLoader.LoadText("SimpleObject.cs", Assembly.GetExecutingAssembly());
    
    var sc = new SourceCompiler(dllName, path);
    sc.UseAppdomain = true;
    sc.AddFile("SharpTemplate.Test.Resources.Compilers", "SimpleObject", source);
    sc.LoadCurrentAssemblies();
    var loadedAssemblyPath = sc.Compile(2);

    var content = File.ReadAllBytes(loadedAssemblyPath);
    var compileSimpleObjectAsm = Assembly.Load(content);
    var instance = (ISimpleObject)Activator.CreateInstance(compileSimpleObjectAsm.FullName, 
        "SharpTemplate.Test.Resources.Compilers.SimpleObject").Unwrap();

Notes

    var content = File.ReadAllBytes(loadedAssemblyPath);
    var compileSimpleObjectAsm = Assembly.Load(content);
    File.Delete(loadedAssemblyPath);

Logging

Given the temporary path in wich the library will write the dll

StillTodo

Licensing

Copyright (C) 2013-2014 Kendar.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Download

See kendar.org for the latest changes.


Last modified on: November 21, 2014