Skip to content

Instantly share code, notes, and snippets.

@DenisPitcher
Created November 30, 2018 16:00
Show Gist options
  • Save DenisPitcher/4b895e3be5be9efa8f14529df1dbb1e4 to your computer and use it in GitHub Desktop.
Save DenisPitcher/4b895e3be5be9efa8f14529df1dbb1e4 to your computer and use it in GitHub Desktop.
Typewriter Typescript dependency generation
${
// Enable extension methods by adding using Typewriter.Extensions.*
using Typewriter.Extensions.Types;
using System.Text;
// Uncomment the constructor to change template settings.
Template(Settings settings)
{
settings.IncludeCurrentProject();
settings.IncludeReferencedProjects();
}
string ClassNameWithExtends(Class c) {
return c.Name + (c.BaseClass!=null ? " extends " + c.BaseClass.Name : "");
}
static StringBuilder Debug = new StringBuilder();
static List<string> VisitedTypes = new List<string>();
string WriteDebug(Class @class){
return Debug.ToString();
}
string TypeMap(Type @type)
{
switch(@type.Name)
{
case "LocalDate":
case "LocalDateTime":
return "string";
default:
return @type.Name;
}
}
bool IsInherited(Class @class)
{
return @class.BaseClass != null;
}
bool IsInherited(Type @type)
{
return @type.BaseClass != null;
}
IEnumerable<Type> VisitEnumDependencies(Class c)
{
return VisitDependencies(c, true);
}
IEnumerable<Type> VisitTypeDependencies(Class c)
{
return VisitDependencies(c, false);
}
IEnumerable<Type> VisitDependencies(Class @class, bool parseEnums)
{
VisitedTypes = new List<string>();
foreach(Property p in @class.Properties)
{
Type propertyType = p.Type.Unwrap();
if (VisitedTypes.Contains(propertyType.FullName))
{
continue;
}
if (propertyType.IsDefined)
{
if (propertyType.IsEnum == parseEnums)
{
VisitedTypes.Add(propertyType.FullName);
yield return propertyType;
}
int count = 0;
foreach(Type t in RecurseProperties(propertyType, parseEnums, count))
{
yield return t;
}
}
}
foreach(Field f in @class.Fields)
{
Type fieldType = f.Type.Unwrap();
if (VisitedTypes.Contains(fieldType.FullName))
{
continue;
}
if (fieldType.IsDefined)
{
if (fieldType.IsEnum == parseEnums)
{
VisitedTypes.Add(fieldType.FullName);
yield return fieldType;
}
int count = 0;
foreach(Type t in RecurseProperties(fieldType, parseEnums, count))
{
yield return t;
}
}
}
}
IEnumerable<Type> RecurseProperties(Type @type, bool parseEnums, int count)
{
count++;
if (count < 10)
{
if (@type.BaseClass != null)
{
// ????
}
foreach(Property prop in @type.Properties)
{
Type propertyType = prop.Type.Unwrap();
if (VisitedTypes.Contains(propertyType.FullName))
{
continue;
}
if (propertyType.IsDefined)
{
if (propertyType.IsEnum == parseEnums)
{
VisitedTypes.Add(propertyType.FullName);
yield return propertyType;
}
foreach(Type t in RecurseProperties(propertyType, parseEnums, count))
{
yield return t;
}
}
}
foreach(Field f in @type.Fields)
{
Type fieldType = f.Type.Unwrap();
if (VisitedTypes.Contains(fieldType.FullName))
{
continue;
}
if (fieldType.IsDefined)
{
if (fieldType.IsEnum == parseEnums)
{
Debug.AppendLine(fieldType.FullName);
VisitedTypes.Add(fieldType.FullName);
yield return fieldType;
}
foreach(Type t in RecurseProperties(fieldType, parseEnums, count))
{
yield return t;
}
}
}
}
}
}
$Classes(Namespace.*)[
$VisitTypeDependencies[
export interface $Name$IsInherited[ extends $BaseClass] {$Properties[
$Name: $Type[$TypeMap]]$Fields[
$Name: $Type[$TypeMap]]
}
]
]
$Classes(Namespace.*)[
$VisitEnumDependencies[
export const enum $Name
{ $Constants[
$Name = $Value,]
}
]
]
$Classes(Namespace.*)[
export class $ClassNameWithExtends {
$Properties[
public $Name: $Type = $Type[$Default];]$Fields[
public $Name: $Type = $Type[$Default];]
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment