forked from loresoft/TypeScriptGenerator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TypeScript.Interface.cst
92 lines (75 loc) · 3.11 KB
/
TypeScript.Interface.cst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<%@ Template Language="C#" TargetLanguage="TypeScript" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Assembly Name="Mono.Cecil" %>
<%@ Assembly Src="Generator.cs" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="Mono.Cecil" %>
<%@ Property Category="1.Type" Name="AssemblyFile"
Type="System.String" Default="" Optional="False"
Description="The assembly file to load."
Editor="System.Windows.Forms.Design.FileNameEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ Property Category="1.Type" Name="ClassType"
Type="System.String" Default="" Optional="False"
Description="The full type name to use." %>
<%@ Property Category="2.Interface" Name="ModuleName"
Type="System.String" Default="" Optional="False"
Description="The TypeScript Module name."%>
<%@ Property Category="2.Interface" Name="Extends"
Type="System.String" Default="" Optional="True"
Description="The type this interface extends."%>
<%@ Property Category="2.Interface" Name="FileHeader"
Type="System.String" Default="" Optional="True"
Description="Text to put in the begining of the file"%>
<%@ Property Category="2.Interface" Name="IgnoreProperties"
Type="CodeSmith.CustomProperties.StringCollection" Default="" Optional="True"
Description="Name of propries to ignore"%>
<%@ Property Category="2.Interface" Name="IgnoreAnyType"
Type="System.Boolean" Default="False" Optional="False"
Description="Ignore properties of script type any."%>
<%@ Property Category="2.Interface" Name="OptionalProperty"
Type="OptionalType" Default="None" Optional="False"
Description="Controls property optional syntax."%>
<%@ Property Category="2.Interface" Name="UseOData"
Type="System.Boolean" Default="False" Optional="False"
Description="Use OData 4 types."%>
<% var scriptInterface = Generator.CreateInterface(AssemblyFile, ClassType, UseOData); %>
<%= FileHeader ?? string.Empty %>
module <%= ModuleName %> {
"use strict";
export interface I<%= scriptInterface.Name %><% if (!string.IsNullOrEmpty(Extends)) { %> extends <%= Extends %><% } %> {
<% foreach (var property in scriptInterface.Properties.Where(p => IsIgnored(p) == false)) { %>
<%= property.Name %><%= OptionalFlag(property) %>: <%= property.Type %>;
<% } %>
}
}
<script runat="template">
public enum OptionalType
{
All,
Nullable,
None
}
public string OptionalFlag(TypeScriptProperty property)
{
if (OptionalProperty == OptionalType.All)
return "?";
if (OptionalProperty == OptionalType.None)
return "";
return property.IsNullable ? "?" : "";
}
public bool IsIgnored(TypeScriptProperty property)
{
if (IgnoreAnyType && string.Equals("any", property.Type, StringComparison.OrdinalIgnoreCase))
return true;
return IsIgnored(property.Name);
}
public bool IsIgnored(string name)
{
if (IgnoreProperties == null)
return false;
return IgnoreProperties.Contains(name);
}
</script>