|
| 1 | +using ApiDoctor.Publishing.CSDL; |
| 2 | +using ApiDoctor.Validation; |
| 3 | +using ApiDoctor.Validation.Error; |
| 4 | +using ApiDoctor.Validation.OData; |
| 5 | +using ApiDoctor.Validation.OData.Transformation; |
| 6 | +using NLog; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Typewriter |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Creates a CSDL file with documentation annotations sourced from documentation. |
| 16 | + /// </summary> |
| 17 | + internal class DocAnnotationWriter : ApiDoctor.Publishing.CSDL.CsdlWriter |
| 18 | + { |
| 19 | + private static Logger Logger => LogManager.GetLogger("DocAnnotationWriter"); |
| 20 | + |
| 21 | + private readonly CsdlWriterOptions options; |
| 22 | + |
| 23 | + internal DocAnnotationWriter(DocSet docSet, CsdlWriterOptions options, string csdl) : base(docSet, options) |
| 24 | + { |
| 25 | + this.options = options; // Can change the base access modifier so we could use it. |
| 26 | + } |
| 27 | + |
| 28 | + public async Task<string> PublishToStringAsync(IssueLogger issues) |
| 29 | + { |
| 30 | + string outputFilenameSuffix = ""; |
| 31 | + |
| 32 | + Logger.Info("Begin creating metadata file with documentation annotations."); |
| 33 | + |
| 34 | + // Step 1: Generate an EntityFramework OM from the documentation and/or template file |
| 35 | + EntityFramework framework = CreateEntityFrameworkFromDocs(issues); |
| 36 | + if (null == framework) |
| 37 | + return string.Empty; |
| 38 | + |
| 39 | + // Step 1a: Apply an transformations that may be defined in the documentation |
| 40 | + if (!string.IsNullOrEmpty(options.TransformOutput)) |
| 41 | + { |
| 42 | + PublishSchemaChangesConfigFile transformations = DocSet.TryLoadConfigurationFiles<PublishSchemaChangesConfigFile>(options.DocumentationSetPath).Where(x => x.SchemaChanges.TransformationName == options.TransformOutput).FirstOrDefault(); |
| 43 | + if (null == transformations) |
| 44 | + { |
| 45 | + throw new KeyNotFoundException($"Unable to locate a transformation set named {options.TransformOutput}. Aborting."); |
| 46 | + } |
| 47 | + |
| 48 | + string[] versionsToPublish = options.Version?.Split(new char[] { ',', ' ' }); |
| 49 | + framework.ApplyTransformation(transformations.SchemaChanges, versionsToPublish); |
| 50 | + if (!string.IsNullOrEmpty(options.Version)) |
| 51 | + { |
| 52 | + outputFilenameSuffix += $"-{options.Version}"; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (options.Sort) |
| 57 | + { |
| 58 | + // Sorts the objects in collections, so that we have consistent output regardless of input |
| 59 | + framework.SortObjectGraph(); |
| 60 | + } |
| 61 | + |
| 62 | + if (options.ValidateSchema) |
| 63 | + { |
| 64 | + framework.ValidateSchemaTypes(); |
| 65 | + } |
| 66 | + |
| 67 | + // Step 2: Generate XML representation of EDMX |
| 68 | + string xmlData = ODataParser.Serialize<EntityFramework>(framework, options.AttributesOnNewLines); |
| 69 | + |
| 70 | + Logger.Info("Finish creating metadata file with documentation annotations."); |
| 71 | + |
| 72 | + return xmlData; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + internal static class AnnotationHelper |
| 77 | + { |
| 78 | + private static Logger Logger => LogManager.GetLogger("AnnotationHelper"); |
| 79 | + |
| 80 | + internal async static Task<string> ApplyAnnotationsToCsdl(string csdl, Options options) |
| 81 | + { |
| 82 | + // Get DocSet |
| 83 | + DocSet docs = GetDocSet(options, new IssueLogger()); |
| 84 | + |
| 85 | + var csdlWriterOptions = new CsdlWriterOptions() |
| 86 | + { |
| 87 | + DocumentationSetPath = options.DocsRoot + "\\api-reference\\v1.0\\", |
| 88 | + Annotations = AnnotationOptions.Properties, |
| 89 | + SourceMetadataPath = options.Metadata, |
| 90 | + SkipMetadataGeneration = true, |
| 91 | + Formats = MetadataFormat.EdmxInput |
| 92 | + }; |
| 93 | + |
| 94 | + DocAnnotationWriter docWriter = new DocAnnotationWriter(docs, csdlWriterOptions, csdl); |
| 95 | + |
| 96 | + return await docWriter.PublishToStringAsync(new IssueLogger()); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private static DocSet GetDocSet(Options options, IssueLogger issues) |
| 101 | + { |
| 102 | + Logger.Info("Opening documentation from {0}", options.DocsRoot); |
| 103 | + DocSet docSet = null; |
| 104 | + |
| 105 | + try |
| 106 | + { |
| 107 | + docSet = new DocSet(options.DocsRoot + "\\api-reference\\v1.0\\"); |
| 108 | + } |
| 109 | + catch (System.IO.FileNotFoundException ex) |
| 110 | + { |
| 111 | + Logger.Error(ex.Message); |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + Logger.Info("Parsing documentation files"); |
| 116 | + var stopwatch = new Stopwatch(); |
| 117 | + stopwatch.Start(); |
| 118 | + docSet.ScanDocumentation(string.Empty, issues); |
| 119 | + stopwatch.Stop(); |
| 120 | + Logger.Info($"Took {stopwatch.Elapsed} to parse {docSet.Files.Length} source files."); |
| 121 | + |
| 122 | + return docSet; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments