Skip to content

Commit ed8932e

Browse files
committed
Refactored generation.
Added new generation options. Investigated testability of generation. Needs work.
1 parent baf4c1b commit ed8932e

File tree

9 files changed

+158
-53
lines changed

9 files changed

+158
-53
lines changed

src/Typewriter/FileWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal static class FileWriter
2121
/// Writes a metadata file to disk.
2222
/// </summary>
2323
/// <param name="metadata">The metadata to write.</param>
24+
/// <param name="fileName">The output metadata filename.</param>
2425
/// <param name="outputDirectoryPath">Metadata write location.</param>
2526
public static string WriteMetadata(string metadata, string fileName, string outputDirectoryPath = null)
2627
{

src/Typewriter/Generator.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Microsoft.Graph.ODataTemplateWriter.TemplateProcessor;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Runtime.CompilerServices;
5+
using Vipr.Core;
6+
using Vipr.Reader.OData.v4;
7+
8+
[assembly: InternalsVisibleTo("GraphODataTemplateWriter.Test")]
9+
10+
namespace Typewriter
11+
{
12+
internal static class Generator
13+
{
14+
/// <summary>
15+
/// Generate code files from the input metadata and do not preprocess.
16+
/// </summary>
17+
/// <param name="csdlContents">Metadata to process</param>
18+
/// <param name="options">The options bag</param>
19+
static internal void GenerateFiles(string csdlContents, Options options)
20+
{
21+
var filesToWrite = MetadataToClientSource(csdlContents, options.Language);
22+
FileWriter.WriteAsync(filesToWrite, options.Output);
23+
}
24+
25+
/// <summary>
26+
/// Generate a metadata file that has been cleaned and doc annotations added to it.
27+
/// </summary>
28+
/// <param name="csdlContents">Metadata to process</param>
29+
/// <param name="options">The options bag</param>
30+
static internal void WriteCleanAnnotatedMetadata(string csdlContents, Options options)
31+
{
32+
string csdlWithDocAnnotations = CleanMetadata(csdlContents, options);
33+
string metadataFileName = string.Concat(options.OutputMetadataFileName, options.EndpointVersion, ".xml");
34+
FileWriter.WriteMetadata(csdlWithDocAnnotations, metadataFileName, options.Output);
35+
}
36+
37+
/// <summary>
38+
/// Clean and annotate the input metadata and then generate code files.
39+
/// </summary>
40+
/// <param name="csdlContents">Metadata to process</param>
41+
/// <param name="options">The options bag</param>
42+
static internal void GenerateFilesFromCleanMetadata(string csdlContents, Options options)
43+
{
44+
string csdlWithDocAnnotations = CleanMetadata(csdlContents, options);
45+
46+
// Create code files from the CSDL with annotations for the target platform and write those files to disk.
47+
GenerateFiles(csdlWithDocAnnotations, options);
48+
}
49+
50+
static private string CleanMetadata(string csdlContents, Options options)
51+
{
52+
// Clean up EDMX to work with the generators assumptions.
53+
string processedCsdlContents = MetadataPreprocessor.CleanMetadata(csdlContents);
54+
55+
// Create clean metadata and provide a path to it.
56+
string pathToCleanMetadata = FileWriter.WriteMetadata(processedCsdlContents, "cleanMetadata.xml");
57+
58+
// Inject documentation annotations into the CSDL using ApiDoctor and get back the file as a string.
59+
return AnnotationHelper.ApplyAnnotationsToCsdl(options, pathToCleanMetadata).Result;
60+
}
61+
62+
/// <summary>
63+
/// Generates code files from an edmx file.
64+
/// </summary>
65+
/// <param name="edmxString">The EDMX file as a string.</param>
66+
/// <param name="targetLanguage">Specifies the target language. Possible values are csharp, php, etc.</param>
67+
/// <returns></returns>
68+
static private IEnumerable<TextFile> MetadataToClientSource(string edmxString, string targetLanguage)
69+
{
70+
if (String.IsNullOrEmpty(edmxString))
71+
throw new ArgumentNullException("edmxString", "The EDMX file string contains no content.");
72+
73+
var reader = new OdcmReader();
74+
var writer = new TemplateWriter(targetLanguage);
75+
writer.SetConfigurationProvider(new ConfigurationProvider());
76+
77+
var model = reader.GenerateOdcmModel(new List<TextFile> { new TextFile("$metadata", edmxString) });
78+
79+
return writer.GenerateProxy(model);
80+
}
81+
}
82+
}

src/Typewriter/Options.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using CommandLine;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: InternalsVisibleTo("GraphODataTemplateWriter.Test")]
25

36
namespace Typewriter
47
{
@@ -29,7 +32,7 @@ public enum GenerationMode
2932
Files
3033
}
3134

32-
class Options
35+
public class Options
3336
{
3437
[Option('l', "language", Default = "CSharp", HelpText = "The target language for the generated code files. The values can be: Android, Java, ObjC, CSharp, PHP, Python, TypeScript, or GraphEndpointList")]
3538
public string Language { get; set; }
@@ -50,6 +53,12 @@ class Options
5053
"the output code files by cleaning the input metadata, parsing the documentation, and adding annotations before generating the output files. Metadata generation mode" +
5154
"produces an output metadata file by cleaning metadata, documentation parsing, and adding documentation annotations. Files generation mode produces code files from" +
5255
"an input metadata and bypasses the cleaning, documentation parsing, and adding documentation annotations.")]
53-
public GenerationMode GenerateMode { get; set; }
56+
public GenerationMode GenerationMode { get; set; }
57+
58+
[Option('f', "outputMetadataFileName", Default = "cleanMetadataWithDescriptions", HelpText = "The output metadata filename. Only applicable for GenerationMode.Metadata.")]
59+
public string OutputMetadataFileName { get; set; }
60+
61+
[Option('e', "endpointVersion", Default = "v1.0", HelpText = "The endpoint version. Expected values are 'v1.0' and 'beta'. Only applicable for GenerationMode.Metadata.")]
62+
public string EndpointVersion { get; set; }
5463
}
5564
}

src/Typewriter/Program.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,20 @@ private static void GenerateSDK(Options options)
3131

3232
string csdlContents = MetadataResolver.GetMetadata(options.Metadata);
3333

34-
// Generate the files from the input metadata and do not preprocess.
35-
if (options.GenerateMode == GenerationMode.Files)
34+
switch (options.GenerationMode)
3635
{
37-
var codeFiles = MetadataToClientSource(csdlContents, options.Language);
38-
FileWriter.WriteAsync(codeFiles, options.Output);
39-
40-
stopwatch.Stop();
41-
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
42-
43-
return;
36+
case GenerationMode.Files:
37+
Generator.GenerateFiles(csdlContents, options);
38+
break;
39+
case GenerationMode.Metadata:
40+
Generator.WriteCleanAnnotatedMetadata(csdlContents, options);
41+
break;
42+
case GenerationMode.Full:
43+
default:
44+
Generator.GenerateFilesFromCleanMetadata(csdlContents, options);
45+
break;
4446
}
4547

46-
// Clean up EDMX to work with the generators assumptions.
47-
string processedCsdlContents = MetadataPreprocessor.CleanMetadata(csdlContents);
48-
49-
// Create clean metadata and provide a path to it.
50-
string pathToCleanMetadata = FileWriter.WriteMetadata(processedCsdlContents, "cleanMetadata.xml");
51-
52-
// Inject documentation annotations into the clean CSDL using ApiDoctor and get back the file as a string.
53-
string csdlWithDocAnnotations = AnnotationHelper.ApplyAnnotationsToCsdl(options, pathToCleanMetadata).Result;
54-
55-
// Output the clean and annotated metadata.
56-
if (options.GenerateMode == GenerationMode.Metadata)
57-
{
58-
FileWriter.WriteMetadata(csdlWithDocAnnotations, "cleanMetadataWithDescriptions_v10.xml", options.Output);
59-
60-
stopwatch.Stop();
61-
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
62-
63-
return;
64-
}
65-
66-
// Default GenerationMode.Full.
67-
// Create code files from the CSDL with annotations for the target platform and write those files to disk.
68-
var files = MetadataToClientSource(csdlWithDocAnnotations, options.Language);
69-
FileWriter.WriteAsync(files, options.Output);
70-
7148
stopwatch.Stop();
7249
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
7350
}
@@ -118,18 +95,18 @@ private static void HandleError(IEnumerable<Error> errors)
11895
/// <param name="edmxString">The EDMX file as a string.</param>
11996
/// <param name="targetLanguage">Specifies the target language. Possible values are csharp, php, etc.</param>
12097
/// <returns></returns>
121-
static private IEnumerable<TextFile> MetadataToClientSource(string edmxString, string targetLanguage)
122-
{
123-
if (String.IsNullOrEmpty(edmxString))
124-
throw new ArgumentNullException("edmxString", "The EDMX file string contains no content.");
98+
//static private IEnumerable<TextFile> MetadataToClientSource(string edmxString, string targetLanguage)
99+
//{
100+
// if (String.IsNullOrEmpty(edmxString))
101+
// throw new ArgumentNullException("edmxString", "The EDMX file string contains no content.");
125102

126-
var reader = new OdcmReader();
127-
var writer = new TemplateWriter(targetLanguage);
128-
writer.SetConfigurationProvider(new ConfigurationProvider());
103+
// var reader = new OdcmReader();
104+
// var writer = new TemplateWriter(targetLanguage);
105+
// writer.SetConfigurationProvider(new ConfigurationProvider());
129106

130-
var model = reader.GenerateOdcmModel(new List<TextFile> { new TextFile("$metadata", edmxString) });
107+
// var model = reader.GenerateOdcmModel(new List<TextFile> { new TextFile("$metadata", edmxString) });
131108

132-
return writer.GenerateProxy(model);
133-
}
109+
// return writer.GenerateProxy(model);
110+
//}
134111
}
135112
}

src/Typewriter/Typewriter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="ConfigurationProvider.cs" />
4646
<Compile Include="DocAnnotationWriter.cs" />
4747
<Compile Include="FileWriter.cs" />
48+
<Compile Include="Generator.cs" />
4849
<Compile Include="MetadataPreprocessor.cs" />
4950
<Compile Include="MetadataResolver.cs" />
5051
<Compile Include="Options.cs" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.IO;
3+
4+
namespace Typewriter.Test
5+
{
6+
[TestClass]
7+
[Ignore] // Work is needed to generate from the test project.
8+
public class GeneratorTests
9+
{
10+
public string testMetadata;
11+
12+
/// <summary>
13+
/// Load metadata from file into a string so we can validate MetadataPreprocessor.
14+
/// </summary>
15+
[TestInitialize]
16+
public void Initialize()
17+
{
18+
testMetadata = Typewriter.Test.Properties.Resources.dirtyMetadata;
19+
}
20+
21+
[TestMethod]
22+
public void GenerateFilesTest()
23+
{
24+
const string outputDirectory = "output";
25+
26+
Options options = new Options()
27+
{
28+
Output = outputDirectory,
29+
Language = "TypeScript"
30+
};
31+
32+
Generator.GenerateFiles(testMetadata, options);
33+
34+
FileInfo fileInfo = new FileInfo(outputDirectory + @"\com\microsoft\graph\src\Microsoft-graph.d.ts");
35+
Assert.IsTrue(fileInfo.Exists);
36+
}
37+
}
38+
}

test/Typewriter.Test/MetadataPreprocessorTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Xml.Linq;
3-
using Microsoft.VisualStudio.TestTools.UnitTesting;
4-
using System.Text;
5-
using Typewriter;
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
62
using System.Linq;
7-
using System.Collections.Generic;
3+
using System.Xml.Linq;
84

95
namespace Typewriter.Test
106
{

test/Typewriter.Test/Resources/dirtyMetadata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<Property Name="url" Type="Edm.String" />
3030
<Property Name="width" Type="Edm.Int32" />
3131
</ComplexType>
32-
<EntityType Name="onenotePage" BaseType="microsoft.graph.onenoteEntitySchemaObjectModel" HasStream="true">
32+
<EntityType Name="onenotePage" HasStream="true">
3333
<Property Name="content" Type="Edm.Stream" />
3434
<NavigationProperty Name="parentNotebook" Type="microsoft.graph.notebook" ContainsTarget="true" />
3535
</EntityType>

test/Typewriter.Test/Typewriter.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<DesignTime>True</DesignTime>
5757
<DependentUpon>Resources.resx</DependentUpon>
5858
</Compile>
59+
<Compile Include="GeneratorTests.cs" />
5960
</ItemGroup>
6061
<ItemGroup>
6162
<None Include="packages.config" />

0 commit comments

Comments
 (0)