Using the Generator SDK

This page shows you how to download, install and use the CodeSmith Generator SDK in your applications. It also walks through the most common uses of the Generator API:

  • Compiling a template
  • Retrieving compile errors
  • Creating a template instance
  • Filling in template metadata
  • Rendering a template

Download Generator

Sign in to your account, then download CodeSmith Generator from the downloads page.

Tip

Download the zipped version of Generator. It includes all the assemblies you need to reference in your SDK application.

Install the license

When you buy an SDK license from the online store, you receive a license key file by email. Make it available to your application in one of these ways:

  • Embed the license file as an embedded resource in the assembly that calls CodeSmith.Engine.
  • Put the license file in the same folder as your application.
  • Set the license key in the Generator:License environment variable, or in code with the CodeSmithLicense.SetLicenseKey(key) method. Use this option only on build and web servers, because it overrides any locally defined licenses.

Create a project

Create a project that targets the .NET Framework 4.8. This example uses a console application.

Important

A project that uses an SDK license must be strong-named.

Tip

Generator's samples include C# and Visual Basic SDK sample projects, in Documents\CodeSmith Generator\Samples\<VERSION>\Projects\CSharp\APISample (and the matching VisualBasic folder). You may need to make small changes, such as updating the connection string. The sample is also in the CodeSmith Generator Templates repository.

Add package references

Your project must reference this NuGet package:

The example below also uses these packages:

Write the code

Add this code to the console application:

public static void Main() {
    string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\StoredProcedures.cst");
    var engine = new TemplateEngine(new DefaultEngineHost(System.IO.Path.GetDirectoryName(path)));

    CompileTemplateResult result = engine.Compile(path);
    if (result.Errors.Count == 0) {
        var database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=.;Database=PetShop;Integrated Security=True;");
        TableSchema table = database.Tables["Inventory"];

        CodeTemplate template = result.CreateTemplateInstance();
        template.SetProperty("SourceTable", table);
        template.SetProperty("IncludeDrop", false);
        template.SetProperty("InsertPrefix", "Insert");
        template.Render(Console.Out);
    } else {
        foreach (var error in result.Errors)
            Console.Error.WriteLine(error.ToString());
    }

    Console.WriteLine("\r\nPress any key to continue.");
    Console.ReadKey();
}

The code compiles and runs, but it needs the StoredProcedures.cst template from the APISample project.

Step API
Compile the template TemplateEngine.Compile returns a CompileTemplateResult.
Retrieve compile errors CompileTemplateResult.Errors.
Create a template instance CompileTemplateResult.CreateTemplateInstance() returns a CodeTemplate.
Fill in template metadata CodeTemplate.SetProperty(name, value).
Render the template CodeTemplate.Render(TextWriter).

Tip

TemplateEngine also has async methods for compiling templates asynchronously.

Tip

Besides Render, you can use CodeTemplate.RenderToFile() and CodeTemplate.RenderToString() to write a template's output straight to a file or a string. See Rendering a sub-template.

If you have questions about the SDK API, contact support.