TypewriterX

A fork of the amazing Typewriter extension

About

TypewriterX is a hard fork of the amazing Typewriter extension by Fredrik Hagnelius

Why did you fork Typewriter?

Typewriter works as a 1/1 mapper/template engine between a class and a single file, this was simple, but limiting.

I needed to parse code and generate multiple outputs, depending on complex conditions, something the Typewriter syntax doesn't support

I also needed more access to generic types (GenericDefClass) and better ways to generalize my template (AsClass,AsEnum)

TypewriterX features

TypewriterX is backwards compatible with Typewriter, for new features it uses the .tstx extension

all the features in the original Typewriter are preserved (at version 1.20), with these changes:

  • a TypewriterX's template root context is now a RootContext object, that allows access to all files,classes,enums etc in the solution
  • added new file output syntax to collections in the template, templates can generate any number of files and even nest them, see examples
  • a Type can be converted to a Class or an Enum using the new AsClass and AsEnum properties.
  • you can get a generic Class generic definition using GenericDefClass property
  • the tstx parser is more conservative about newlines, omitting them when no text is outputted from a template variable or collection
  • Template code can now be automatically formatted (uses Roslyn formatter)

Example

Below is an example of a TypeScript Template for generating simple model classes.

$Files(*Models) will find files classes with a name ending in "Model" defined in the same project as the template or in a project referenced by that project.

The section between the brackets ([]) is repeated for each file.

$Classes will find all public classes in the file being processed, each class will be outputed to a file named between the arrows (<>)

$Name will print the name of the class and $Properties will loop each public property of the class. $name is the name of the property (by using the lower case $name the property name will be printed in camel case) and $Type is a TypeScript friendly version of the property type.

$Files(*Models)[
$Classes<$Name.ts>[
 export class $Name {
$Properties[
    public $name : $Type;
]
 }
]
]
using System;
using System.Collections.Generic;

namespace Demo 
{
    public class CustomerModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<OrderModel> Orders { get; set; }
    }

    public class EmployerModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
    export class CustomerModel {
        public id: number;
        public name: string;
        public orders: OrderModel[];
    }
    export class EmployerModel {
        public id: number;
        public name: string;
    }