-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTypeScript.Interface.cst
141 lines (117 loc) · 4.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<%@ 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="System.Text" %>
<%@ 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="True"
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."%>
<%@ Property Category="2.Interface" Name="PropertyNaming"
Type="Naming" Default="PascalCase" Optional="False"
Description="Controls property name syntax."%>
<% var scriptInterface = Generator.CreateInterface(AssemblyFile, ClassType, UseOData); %>
<%= FileHeader ?? string.Empty %>
<% if (!string.IsNullOrEmpty(ModuleName)) { %>
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)) { %>
<%= PropertyName(property.Name) %><%= OptionalFlag(property) %>: <%= property.Type %>;
<% } %>
}
<% if (!string.IsNullOrEmpty(ModuleName)) { %>
}
<% } %>
<script runat="template">
public enum OptionalType
{
All,
Nullable,
None
}
public enum Naming
{
PascalCase,
CamalCase,
LowerCaseUnderscore
}
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);
}
public string PropertyName(string name)
{
if (string.IsNullOrEmpty(name))
return string.Empty;
switch(PropertyNaming)
{
case Naming.CamalCase:
return StringUtil.ToCamelCase(name);
case Naming.LowerCaseUnderscore:
return ToLowerUnderscoredWords(name);
default:
return StringUtil.ToPascalCase(name);
}
}
public string ToLowerUnderscoredWords(string value) {
var builder = new StringBuilder(value.Length + 10);
for (int index = 0; index < value.Length; index++) {
char c = value[index];
if (Char.IsUpper(c)) {
if (index > 0 && value[index - 1] != '_')
builder.Append('_');
builder.Append(Char.ToLower(c));
} else {
builder.Append(c);
}
}
return builder.ToString();
}
</script>