Skip to content

Commit ac55a14

Browse files
committed
Namespaces support - TypeScript (#269)
* refactored main generation logic into a .cs file, achieved the same output utils.tt is removed taken SplitString function from utils.tt to a helper and refactored Created models for typescript code generation from namespaces * generate namespaces with correct indentation * correct cross namespace references in V1 output * fix namespace references as main namespace is microsoftgraph for Typescript * add TypeScript tests * add NullableOption changes, fix write path
1 parent 5d78a5b commit ac55a14

File tree

14 files changed

+887
-208
lines changed

14 files changed

+887
-208
lines changed
Lines changed: 5 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<# // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. #>
22
<#@ template debug="true" hostspecific="true" language="C#" #>
3-
<#@ include file="utils.tt" #>
43
<#@ output extension="\\" #>
54
<#
65
CustomT4Host host = (CustomT4Host) Host;
76
CodeWriterTypeScript writer = (CodeWriterTypeScript) host.CodeWriter;
87

98
var model = host.CurrentModel;
10-
var entityTypes = model.GetEntityTypes();
11-
var enumTypes = model.GetEnumTypes();
12-
var complexTypes = model.GetComplexTypes();
13-
14-
var maxLineLength = 120;
9+
var typeScriptNamespaces = model.GetTypeScriptNamespaces();
1510
#>
1611
// Project: https://github.com/microsoftgraph/msgraph-typescript-typings
1712
// Definitions by: Microsoft Graph Team <https://github.com/microsoftgraph>
@@ -27,168 +22,13 @@ export as namespace microsoftgraph;
2722

2823
export type NullableOption<T> = T | null;
2924

30-
<#
31-
foreach(var enumType in enumTypes) {
32-
var enumTypeName = enumType.Name.UpperCaseFirstChar();
33-
var enumValues = enumType.GetEnumValues();
34-
var exportTypeLength = "export type".Length + enumTypeName.Length + enumValues.Length + 3;
35-
if (exportTypeLength < maxLineLength) {
36-
#>
37-
export type <#= enumTypeName #> = <#= enumValues #>;
38-
<#
39-
} else {
40-
#>
41-
export type <#= enumTypeName #> =<#
42-
var enums = enumValues.Split('|');
43-
for(var i = 0; i < enums.Length; i++) {
44-
#>
45-
46-
| <#= enums[i].Trim() #><#
47-
}
48-
#>;
49-
<#
50-
}
51-
}
25+
<#=typeScriptNamespaces.MainNamespace.ToString()#>
5226

53-
foreach(var entityType in entityTypes) {
54-
var propCount = entityType.Properties.ToList().Count;
55-
var entityTypeName = entityType.Name.UpperCaseFirstChar();
56-
if(propCount == 0 && entityTypeName[0] == 'I') {
57-
#>
58-
// tslint:disable-next-line: interface-name no-empty-interface
5927
<#
60-
} else if (entityTypeName[0] == 'I') {
28+
foreach (var subNamespace in typeScriptNamespaces.SubNamespaces)
29+
{
6130
#>
62-
// tslint:disable-next-line: interface-name
31+
<#=subNamespace.Value.ToString()#>
6332
<#
64-
} else if(propCount == 0) {
65-
#>
66-
// tslint:disable-next-line: no-empty-interface
67-
<#
68-
}
69-
#>
70-
export interface <#= entityTypeName #><#
71-
if (entityType.Base != null) {
72-
#>
73-
extends <#= entityType.Base.Name.UpperCaseFirstChar() #><#
74-
}
75-
#> {<#
76-
if(propCount == 0){
77-
#>}
78-
<#
79-
} else {
80-
#>
81-
82-
<#
83-
foreach(var prop in entityType.Properties.ToList()) {
84-
if (prop.LongDescription != null || prop.Description != null) {
85-
List<string> multiLineDescriptions = Utils.splitString(prop.GetSanitizedLongDescription(), maxLineLength);
86-
if(multiLineDescriptions.Count() == 1) {
87-
#>
88-
// <#= multiLineDescriptions.First() #>
89-
<#
90-
} else {
91-
#>
92-
/**
93-
<#
94-
foreach(var descriptionLine in multiLineDescriptions) {
95-
#>
96-
* <#= descriptionLine #>
97-
<#
98-
}
99-
#>
100-
*/
101-
<#
102-
}
103-
}
104-
#>
105-
<#
106-
if(prop.IsNullable) {
107-
#>
108-
<#= prop.Name #>?: NullableOption<<#= prop.GetTypeString() #>>;
109-
<#
110-
} else {
111-
#>
112-
<#= prop.Name #>?: <#= prop.GetTypeString() #>;
113-
<#
114-
}
115-
#>
116-
<#
117-
}
118-
#>
11933
}
120-
<#
121-
}
122-
}
123-
124-
foreach(var complexType in complexTypes) {
125-
var propCount = complexType.Properties.ToList().Count;
126-
var complexTypeName = complexType.Name.UpperCaseFirstChar();
127-
if(propCount == 0 && complexTypeName[0] == 'I') {
128-
#>
129-
// tslint:disable-next-line: interface-name no-empty-interface
130-
<#
131-
} else if (complexTypeName[0] == 'I') {
132-
#>
133-
// tslint:disable-next-line: interface-name
134-
<#
135-
} else if(propCount == 0) {
136-
#>
137-
// tslint:disable-next-line: no-empty-interface
138-
<#
139-
}
140-
#>
141-
export interface <#= complexTypeName #><#
142-
if (complexType.Base != null) {
143-
#>
144-
extends <#= complexType.Base.Name.UpperCaseFirstChar() #><#
145-
}
146-
#> {<#
147-
if(propCount == 0){
148-
#>}
149-
<#
150-
} else {
151-
#>
152-
153-
<#
154-
foreach(var prop in complexType.Properties) {
155-
if (prop.LongDescription != null || prop.Description != null) {
156-
List<string> multiLineDescriptions = Utils.splitString(prop.GetSanitizedLongDescription(), maxLineLength);
157-
if(multiLineDescriptions.Count() == 1) {
158-
#>
159-
// <#= multiLineDescriptions.First() #>
160-
<#
161-
} else {
162-
#>
163-
/**
164-
<#
165-
foreach(var descriptionLine in multiLineDescriptions) {
166-
#>
167-
* <#= descriptionLine #>
168-
<#
169-
}
170-
#>
171-
*/
172-
<#
173-
}
174-
}
175-
#>
176-
<#
177-
if(prop.IsNullable) {
178-
#>
179-
<#= prop.Name #>?: NullableOption<<#= prop.GetTypeString() #>>;
180-
<#
181-
} else {
182-
#>
183-
<#= prop.Name #>?: <#= prop.GetTypeString() #>;
184-
<#
185-
}
186-
#>
187-
<#
188-
}
189-
#>
190-
}
191-
<#
192-
}
193-
}
19434
#>

Templates/TypeScript/src/utils.tt

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/GraphODataTemplateWriter/CodeHelpers/TypeScript/TypeHelperTypeScript.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.TypeScript
55
using Vipr.Core.CodeModel;
66
using System;
77
using System.Linq;
8+
using System.Collections.Generic;
9+
using Microsoft.Graph.ODataTemplateWriter.Extensions;
810

911
public static class TypeHelperTypeScript
1012
{
@@ -14,7 +16,6 @@ public static String GetEnumValues(this OdcmEnum _enum) {
1416
return _enum.Members.Select(m => "\"" + m.Name + "\"").Aggregate((cur, next) => cur + " | " + next);
1517
}
1618

17-
1819
public static string GetTypeString(this OdcmProperty prop)
1920
{
2021
string typeStr = prop.Type.Name.UpperCaseFirstChar();
@@ -73,5 +74,35 @@ public static string GetSanitizedLongDescription(this OdcmProperty property)
7374
}
7475
return null;
7576
}
77+
78+
/// <summary>
79+
/// Converts an OdcmModel into printable TypeScript namespaces
80+
/// </summary>
81+
/// <param name="model">Odcm model</param>
82+
/// <returns>Main and Subnamespaces</returns>
83+
public static TypeScriptNamespaces GetTypeScriptNamespaces(this OdcmModel model)
84+
{
85+
TypeScriptNamespace mainNamespace = null;
86+
var subNamespaces = new Dictionary<string, TypeScriptNamespace>();
87+
88+
foreach(var odcmNamespace in model.GetOdcmNamespaces())
89+
{
90+
var typeScriptNamespace = new TypeScriptNamespace(odcmNamespace);
91+
if (typeScriptNamespace.IsMainNamespace)
92+
{
93+
mainNamespace = typeScriptNamespace;
94+
}
95+
else
96+
{
97+
subNamespaces[typeScriptNamespace.NamespaceName] = typeScriptNamespace;
98+
}
99+
}
100+
101+
return new TypeScriptNamespaces()
102+
{
103+
MainNamespace = mainNamespace,
104+
SubNamespaces = subNamespaces
105+
};
106+
}
76107
}
77108
}

0 commit comments

Comments
 (0)