-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Davy803/feature/typescript-templates
Added code generation templates for TypeScript types and constants.
- Loading branch information
Showing
2 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.