Skip to content

Commit

Permalink
Merge pull request #64 from Davy803/feature/typescript-templates
Browse files Browse the repository at this point in the history
Added code generation templates for TypeScript types and constants.
  • Loading branch information
blipson89 authored Nov 12, 2023
2 parents 44e3a04 + 155f57e commit 426f343
Show file tree
Hide file tree
Showing 2 changed files with 414 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/Leprechaun.CodeGen.Roslyn/Scripts/JssConstants.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Generates Constants for TemplateIDs

using System.Linq;
using System.Collections.Generic;

Log.Debug($"Emitting TypeScript interfaces for {ConfigurationName}...");

Code.AppendLine($@"
/**
* <auto-generated>
* This code was generated by a tool.
*
* Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
* </auto-generated>
*/
{RenderTemplates()}
");

public string RenderTemplates()
{
var localCode = new System.Text.StringBuilder();

// Render the Item mappings
var oldNamespace = "";
foreach (var template in Templates)
{
if (template.Namespace != oldNamespace)
{
if (!string.IsNullOrWhiteSpace(oldNamespace))
{
localCode.AppendLine($@"
}}");
}
localCode.AppendLine($@"export namespace {GetShortNameSpace(template)}.Constants {{");
oldNamespace = template.Namespace;
}

localCode.Append($@"
export const {template.CodeName} = {{
TemplateId: ""{template.Id}"",
");
localCode.Append($@"
BaseTemplateIds: [");
foreach (var baseTemplate in GetBaseTemplates(template.BaseTemplates))
{
localCode.Append($@"
""{baseTemplate.Id}"", ");
}
localCode.Append($@"
]");

localCode.Append($@"
}}");
}
if (Templates.Any())
{

localCode.Append($@"
}}");
}

return localCode.ToString();
}

public List<TemplateCodeGenerationMetadata> GetBaseTemplates(IEnumerable<TemplateCodeGenerationMetadata> templates, List<TemplateCodeGenerationMetadata> foundTemplates = null)
{
if (foundTemplates == null)
{
foundTemplates = new List<TemplateCodeGenerationMetadata>();
}

foreach (var template in templates)
{
if (!foundTemplates.Any(_ => _.Id == template.Id))
{
foundTemplates.Add(template);
GetBaseTemplates(template.BaseTemplates, foundTemplates);
}
}

return foundTemplates;
}


public string GetShortNameSpace(TemplateCodeGenerationMetadata template)
{
var shortNameSpace = template.RelativeNamespace;

if(string.IsNullOrWhiteSpace(shortNameSpace)) {
return template.Namespace;
}
return shortNameSpace;
}
Loading

0 comments on commit 426f343

Please sign in to comment.