Skip to content

Commit 6e92655

Browse files
committed
Merged typescript into master
2 parents 90d50b7 + 6d069d4 commit 6e92655

File tree

10 files changed

+216
-6
lines changed

10 files changed

+216
-6
lines changed

GraphODataTemplateWriter.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ Global
3030
GlobalSection(SolutionProperties) = preSolution
3131
HideSolutionNode = FALSE
3232
EndGlobalSection
33-
EndGlobal
33+
EndGlobal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Source code writers for [VIPR][vipr-source-repo] utilizing T4 templates. The Gra
77
Currently the following target languages are supported by this writer:
88
- Android
99
- CSharp
10-
- JavaScript
1110
- Objective-C
1211
- Python
12+
- TypeScript
1313

1414
# Contents
1515
- [Prerequisites](#prerequisites)
@@ -44,7 +44,7 @@ By default, output source code will be put in a folder named "output" next to th
4444
## Template Writer Settings
4545
### Available Languages
4646

47-
There are four languages to choose from at the moment. Java, ObjC, CSharp, and Python. Specify which language you want to generate in the `TargetLanguage` setting.
47+
There are five languages to choose from at the moment. Java, ObjC, CSharp, TypeScript and Python. Specify which language you want to generate in the `TargetLanguage` setting.
4848

4949
### Templates
5050
You must specify a template directory under the `TemplatesDirectory` Settings. The directory can be a full path or relative to the running directory. The directory must contain a sub directory for each platform you want to generate code for. See the Templates directory for an example.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<# // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. #>
2+
<#@ template debug="true" hostspecific="true" language="C#" #>
3+
<#@ output extension="\\" #>
4+
<#
5+
CustomT4Host host = (CustomT4Host) Host;
6+
CodeWriterTypeScript writer = (CodeWriterTypeScript) host.CodeWriter;
7+
8+
var model = host.CurrentModel;
9+
var entityTypes = model.GetEntityTypes();
10+
var enumTypes = model.GetEnumTypes();
11+
var complexTypes = model.GetComplexTypes();
12+
#>
13+
// Type definitions for the Microsoft Graph API
14+
// Project: https://github.com/microsoftgraph/msgraph-typescript-typings
15+
// Definitions by: Microsoft Graph Team <https://github.com/microsoftgraph>
16+
17+
<#= writer.WriteHeader() #>
18+
19+
20+
<# foreach(var enumType in enumTypes) { #>
21+
export type <#= enumType.Name.UpperCaseFirstChar() #> = <#= enumType.GetEnumValues() #>
22+
<# } #>
23+
<#
24+
foreach(var entityType in entityTypes)
25+
{
26+
var methods = entityType.Methods;
27+
#>
28+
29+
export interface <#= entityType.Name.UpperCaseFirstChar() #><# if (entityType.Base != null) { #> extends <#= entityType.Base.Name.UpperCaseFirstChar() #><# }#> {
30+
<# foreach(var prop in entityType.Properties.ToList()) { #>
31+
<#= prop.Name #>?: <#= prop.GetTypeString() #>
32+
<# } #>
33+
}
34+
<#
35+
}
36+
#>
37+
<#
38+
foreach(var complexType in complexTypes)
39+
{
40+
#>
41+
42+
export interface <#= complexType.Name.UpperCaseFirstChar()#> {
43+
<# foreach(var prop in complexType.Properties) { #>
44+
<#= prop.Name #>?: <#= prop.GetTypeString() #>
45+
<# } #>
46+
}
47+
<# } #>

src/GraphODataTemplateWriter/.config/TemplateWriterSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "Python", "GraphEndpointList" ],
3-
"TargetLanguage": "GraphEndpointList",
2+
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "Python", "TypeScript", "GraphEndpointList" ],
3+
"TargetLanguage": "TypeScript",
44
"Plugins": [ ],
55
"PrimaryNamespaceName": "com",
66
"NamespacePrefix": "com",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"TemplateMapping": {
3+
"TypeScript": [
4+
{ "Template": "entity_types", "SubProcessor": "Other", "Name": "entity_types" }
5+
]
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
2+
3+
namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.TypeScript
4+
{
5+
using System;
6+
using Vipr.Core.CodeModel;
7+
8+
public class CodeWriterTypeScript : CodeWriterBase
9+
{
10+
public CodeWriterTypeScript() : base() { }
11+
12+
public CodeWriterTypeScript(OdcmModel model) : base(model) { }
13+
14+
public override String WriteOpeningCommentLine()
15+
{
16+
return "//" + this.NewLineCharacter;
17+
}
18+
19+
public override String WriteClosingCommentLine()
20+
{
21+
return "//" + this.NewLineCharacter;
22+
}
23+
24+
public override string WriteInlineCommentChar()
25+
{
26+
return "// ";
27+
}
28+
29+
public override String NewLineCharacter
30+
{
31+
get { return "\n"; }
32+
}
33+
34+
}
35+
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
2+
3+
namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.TypeScript
4+
{
5+
using Vipr.Core.CodeModel;
6+
using System;
7+
using System.Linq;
8+
9+
public static class TypeHelperTypeScript
10+
{
11+
12+
// enum value string, ex: "low" | "normal" | "high"
13+
public static String GetEnumValues(this OdcmEnum _enum) {
14+
return _enum.Members.Select(m => "\"" + m.Name + "\"").Aggregate((cur, next) => cur + " | " + next);
15+
}
16+
17+
18+
public static string GetTypeString(this OdcmProperty prop)
19+
{
20+
string typeStr = prop.Type.Name.UpperCaseFirstChar();
21+
22+
switch (typeStr)
23+
{
24+
case "Stream":
25+
case "Json":
26+
typeStr = "any";
27+
break;
28+
case "Int16":
29+
case "Int32":
30+
case "Int64":
31+
case "Double":
32+
case "Binary": // let binary: number = 0b1010;
33+
typeStr = "number";
34+
break;
35+
case "Guid":
36+
case "String":
37+
typeStr = "string"; //lowercase
38+
break;
39+
// all dates need to be of type string so they can be wrapped in new Date(___)
40+
case "DateTimeOffset": // ISO 8601 format in UTC time, ex 2014-01-01T00:00:00Z
41+
case "Date":
42+
typeStr = "string";
43+
break;
44+
case "Boolean":
45+
typeStr = "boolean";
46+
break;
47+
48+
}
49+
return (prop.IsCollection) ? "[" + typeStr + "]" : typeStr;
50+
51+
}
52+
public static String UpperCaseFirstChar(this String s)
53+
{
54+
return char.ToUpper(s[0]) + s.Substring(1);
55+
}
56+
57+
58+
}
59+
}

src/GraphODataTemplateWriter/GraphODataTemplateWriter.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@
5555
<Compile Include="CodeHelpers\CSharp\CodeWriterCSharp.cs" />
5656
<Compile Include="CodeHelpers\CSharp\TypeHelperCSharp.cs" />
5757
<Compile Include="CodeHelpers\Android\CodeWriterAndroid.cs" />
58+
<Compile Include="CodeHelpers\TypeScript\CodeWriterTypeScript.cs" />
59+
<Compile Include="CodeHelpers\TypeScript\TypeHelperTypeScript.cs" />
5860
<Compile Include="CodeHelpers\GraphEndpointList\CodeWriterGraphEndpointList.cs" />
5961
<Compile Include="CodeHelpers\GraphEndpointList\TypeHelperGraphEndpointList.cs" />
6062
<Compile Include="CodeHelpers\ObjC\CodeWriterObjC.cs" />
6163
<Compile Include="CodeHelpers\Python\CodeWriterPython.cs" />
6264
<Compile Include="CodeHelpers\Python\TypeHelperPython.cs" />
6365
<Compile Include="Extensions\FeatureExtensions.cs" />
6466
<Compile Include="PathWriters\CSharpPathWriter.cs" />
67+
<Compile Include="PathWriters\TypeScriptPathWriter.cs" />
6568
<Compile Include="PathWriters\GraphEndpointListPathWriter.cs" />
6669
<Compile Include="PathWriters\PythonPathWriter.cs" />
6770
<Compile Include="TemplateProcessor\CustomT4Host.cs" />
@@ -92,6 +95,9 @@
9295
<None Include=".config\CSharpSettings.json">
9396
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
9497
</None>
98+
<None Include=".config\TypeScriptSettings.json">
99+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100+
</None>
95101
<None Include=".config\GraphEndpointListSettings.json">
96102
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97103
</None>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
2+
3+
namespace Microsoft.Graph.ODataTemplateWriter.PathWriters
4+
{
5+
using System;
6+
using System.IO;
7+
using System.Linq;
8+
using Microsoft.Graph.ODataTemplateWriter.Extensions;
9+
using Microsoft.Graph.ODataTemplateWriter.Settings;
10+
using Microsoft.Graph.ODataTemplateWriter.TemplateProcessor;
11+
12+
public class TypeScriptPathWriter : PathWriterBase
13+
{
14+
15+
public override string WritePath(ITemplateInfo template, String baseFileName)
16+
{
17+
var theNamespace = this.CreateNamespace(template.OutputParentDirectory.ToLower());
18+
var namespacePath = this.CreatePathFromNamespace(theNamespace);
19+
var fileName = this.TransformFileName(template, baseFileName);
20+
String filePath = Path.Combine(namespacePath, fileName);
21+
return filePath;
22+
}
23+
24+
private string CreateNamespace(string folderName)
25+
{
26+
var @namespace = this.Model.GetNamespace();
27+
var prefix = ConfigurationService.Settings.NamespacePrefix;
28+
29+
if (String.IsNullOrEmpty(ConfigurationService.Settings.NamespaceOverride))
30+
{
31+
if (string.IsNullOrEmpty(folderName))
32+
{
33+
return string.IsNullOrEmpty(prefix) ? @namespace
34+
: string.Format("{0}.{1}", prefix, @namespace);
35+
}
36+
37+
return string.IsNullOrEmpty(prefix) ? string.Format("{0}.{1}", @namespace, folderName)
38+
: string.Format("{0}.{1}.{2}", prefix, @namespace, folderName);
39+
}
40+
41+
@namespace = ConfigurationService.Settings.NamespaceOverride;
42+
return folderName != null ? string.Format("{0}.{1}", @namespace, folderName)
43+
: @namespace;
44+
}
45+
46+
private string CreatePathFromNamespace(string @namespace)
47+
{
48+
var splitPaths = @namespace.Split('.');
49+
50+
var destinationPath = splitPaths.Aggregate(string.Empty, (current, path) =>
51+
current + string.Format("{0}{1}", path, Path.DirectorySeparatorChar));
52+
return destinationPath;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)