|
| 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 | +} |
0 commit comments