Skip to content

Commit 37b79e3

Browse files
committed
Generates list of possible graph endpoints
1 parent 331c1dd commit 37b79e3

File tree

7 files changed

+184
-2
lines changed

7 files changed

+184
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
CodeWriterGraphEndpointList writer = (CodeWriterGraphEndpointList) host.CodeWriter;
7+
8+
var model = host.CurrentModel;
9+
var entityTypes = model.GetEntityTypes();
10+
var entitySet = model.EntityContainer.Properties;
11+
#>
12+
<# foreach(var entity in entitySet) { #>
13+
<# foreach (var path in entity.GetNavigationPaths("", entityTypes, 0)) { #>
14+
<#= path #>
15+
<# } #>
16+
17+
18+
<# } #>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"TemplateMapping": {
3+
"GraphEndpointList": [
4+
{ "Template": "graph_endpoint_list", "SubProcessor": "Other", "Name": "graph_endpoint_list" }
5+
]
6+
}
7+
}

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", "JavaScript" ],
3-
"TargetLanguage": "Android",
2+
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "Python", "JavaScript", "GraphEndpointList" ],
3+
"TargetLanguage": "GraphEndpointList",
44
"Plugins": [ ],
55
"PrimaryNamespaceName": "com",
66
"NamespacePrefix": "com",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.GraphEndpointList
4+
{
5+
using System;
6+
using Vipr.Core.CodeModel;
7+
8+
public class CodeWriterGraphEndpointList : CodeWriterBase
9+
{
10+
public CodeWriterGraphEndpointList() : base() { }
11+
12+
public CodeWriterGraphEndpointList(OdcmModel model) : base(model) { }
13+
14+
public override string WriteClosingCommentLine()
15+
{
16+
throw new NotImplementedException();
17+
}
18+
19+
public override string WriteInlineCommentChar()
20+
{
21+
throw new NotImplementedException();
22+
}
23+
24+
public override string WriteOpeningCommentLine()
25+
{
26+
throw new NotImplementedException();
27+
}
28+
}
29+
30+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.GraphEndpointList
4+
{
5+
using Vipr.Core.CodeModel;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
10+
public static class TypeHelperGraphEndpointList
11+
{
12+
13+
public static OdcmClass FindEntityByName(this IEnumerable<OdcmClass> entities, String name)
14+
{
15+
foreach (var entity in entities)
16+
{
17+
if (entity.Name.Equals(name))
18+
{
19+
return entity;
20+
}
21+
}
22+
return null;
23+
}
24+
25+
public static List<string> GetNavigationPaths(this OdcmProperty prop, string baseString, IEnumerable<OdcmClass> entities, int depth)
26+
{
27+
List<String> paths = new List<string>();
28+
if (depth > 5) return paths;
29+
OdcmClass entity = entities.FindEntityByName(prop.Type.Name);
30+
31+
string newBase;
32+
if (prop.IsCollection)
33+
{
34+
newBase = baseString + "/" + prop.Name + "/{" + prop.Type.Name + "Id}";
35+
paths.Add(baseString + "/" + prop.Name);
36+
}
37+
else
38+
{
39+
newBase = baseString + "/" + prop.Name;
40+
41+
}
42+
43+
paths.Add(newBase);
44+
if (entity.Methods.Any()) // check for actions
45+
{
46+
foreach (var method in entity.Methods)
47+
{
48+
paths.Add(newBase + "/" + method.Name);
49+
}
50+
}
51+
52+
foreach (var eprop in entity.Properties)
53+
{
54+
if (eprop.IsLink)
55+
{
56+
paths.AddRange(GetNavigationPaths(eprop, newBase, entities, depth + 1));
57+
}
58+
}
59+
60+
61+
return paths;
62+
}
63+
64+
}
65+
66+
}

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\GraphEndpointList\CodeWriterGraphEndpointList.cs" />
59+
<Compile Include="CodeHelpers\GraphEndpointList\TypeHelperGraphEndpointList.cs" />
5860
<Compile Include="CodeHelpers\JavaScript\CodeWriterJavaScript.cs" />
5961
<Compile Include="CodeHelpers\JavaScript\TypeHelperJavaScript.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\GraphEndpointListPathWriter.cs" />
6568
<Compile Include="PathWriters\JavaScriptPathWriter.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\GraphEndpointListSettings.json">
99+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100+
</None>
95101
<None Include=".config\JavaScriptSettings.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 GraphEndpointListPathWriter : 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 splittedPaths = @namespace.Split('.');
49+
50+
var destinationPath = splittedPaths.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)