Skip to content

Commit baf4c1b

Browse files
committed
Enable metadata generation options.
1 parent a67af1e commit baf4c1b

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/Typewriter/DocAnnotationWriter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class DocAnnotationWriter : ApiDoctor.Publishing.CSDL.CsdlWriter
2020

2121
private readonly CsdlWriterOptions options;
2222

23-
internal DocAnnotationWriter(DocSet docSet, CsdlWriterOptions options, string csdl) : base(docSet, options)
23+
internal DocAnnotationWriter(DocSet docSet, CsdlWriterOptions options) : base(docSet, options)
2424
{
2525
this.options = options; // Can change the base access modifier so we could use it.
2626
}
@@ -77,21 +77,31 @@ internal static class AnnotationHelper
7777
{
7878
private static Logger Logger => LogManager.GetLogger("AnnotationHelper");
7979

80-
internal async static Task<string> ApplyAnnotationsToCsdl(string csdl, Options options)
80+
/// <summary>
81+
/// Applies annotations to CSDL file.
82+
/// </summary>
83+
/// <param name="options">The typewriter input options.</param>
84+
/// <param name="pathToCleanMetadata">Optional. Contains the path to a clean metadata to use when applying annotations. Overrides Option.Metadata.</param>
85+
/// <returns>An annotated metadata file.</returns>
86+
internal async static Task<string> ApplyAnnotationsToCsdl(Options options, string pathToCleanMetadata = null)
8187
{
82-
// Get DocSet
8388
DocSet docs = GetDocSet(options, new IssueLogger());
8489

8590
var csdlWriterOptions = new CsdlWriterOptions()
8691
{
8792
DocumentationSetPath = options.DocsRoot + "\\api-reference\\v1.0\\",
8893
Annotations = AnnotationOptions.Properties,
89-
SourceMetadataPath = options.Metadata,
9094
SkipMetadataGeneration = true,
9195
Formats = MetadataFormat.EdmxInput
9296
};
9397

94-
DocAnnotationWriter docWriter = new DocAnnotationWriter(docs, csdlWriterOptions, csdl);
98+
// We only intend to ues the source metadata when we don't pass in a CSDL.
99+
if (string.IsNullOrEmpty(pathToCleanMetadata))
100+
csdlWriterOptions.SourceMetadataPath = options.Metadata;
101+
else
102+
csdlWriterOptions.SourceMetadataPath = pathToCleanMetadata;
103+
104+
DocAnnotationWriter docWriter = new DocAnnotationWriter(docs, csdlWriterOptions);
95105

96106
return await docWriter.PublishToStringAsync(new IssueLogger());
97107
}

src/Typewriter/FileWriter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ internal static class FileWriter
1717
private static ConcurrentDictionary<string, Lazy<AsyncLock>> lockDictionary = new ConcurrentDictionary<string, Lazy<AsyncLock>>();
1818
internal static Logger Logger => LogManager.GetLogger("FileWriter");
1919

20+
/// <summary>
21+
/// Writes a metadata file to disk.
22+
/// </summary>
23+
/// <param name="metadata">The metadata to write.</param>
24+
/// <param name="outputDirectoryPath">Metadata write location.</param>
25+
public static string WriteMetadata(string metadata, string fileName, string outputDirectoryPath = null)
26+
{
27+
if (!string.IsNullOrWhiteSpace(outputDirectoryPath) && !Directory.Exists(outputDirectoryPath))
28+
Directory.CreateDirectory(outputDirectoryPath);
29+
if (string.IsNullOrWhiteSpace(outputDirectoryPath))
30+
outputDirectoryPath = Environment.CurrentDirectory;
31+
32+
var fullFileName = string.Concat(outputDirectoryPath, "\\", fileName);
33+
34+
File.WriteAllText(fullFileName, metadata);
35+
Logger.Info($"Metadata written to {fullFileName}");
36+
37+
return fullFileName;
38+
}
39+
2040
/// <summary>
2141
/// Write all generated files to disk
2242
/// </summary>

src/Typewriter/Program.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ private static void GenerateSDK(Options options)
3434
// Generate the files from the input metadata and do not preprocess.
3535
if (options.GenerateMode == GenerationMode.Files)
3636
{
37-
var files = MetadataToClientSource(csdlContents, options.Language);
38-
FileWriter.WriteAsync(files, options.Output);
37+
var codeFiles = MetadataToClientSource(csdlContents, options.Language);
38+
FileWriter.WriteAsync(codeFiles, options.Output);
3939

4040
stopwatch.Stop();
4141
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
@@ -46,13 +46,16 @@ private static void GenerateSDK(Options options)
4646
// Clean up EDMX to work with the generators assumptions.
4747
string processedCsdlContents = MetadataPreprocessor.CleanMetadata(csdlContents);
4848

49-
// Inject documentation annotations into the CSDL using ApiDoctor.
50-
string csdlWithDocAnnotations = AnnotationHelper.ApplyAnnotationsToCsdl(processedCsdlContents, options).Result;
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;
5154

5255
// Output the clean and annotated metadata.
5356
if (options.GenerateMode == GenerationMode.Metadata)
5457
{
55-
// TODO: Write the metadata to the output directory using csdlWithDocAnnotations.
58+
FileWriter.WriteMetadata(csdlWithDocAnnotations, "cleanMetadataWithDescriptions_v10.xml", options.Output);
5659

5760
stopwatch.Stop();
5861
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
@@ -65,7 +68,7 @@ private static void GenerateSDK(Options options)
6568
var files = MetadataToClientSource(csdlWithDocAnnotations, options.Language);
6669
FileWriter.WriteAsync(files, options.Output);
6770

68-
stopwatch.Stop();
71+
stopwatch.Stop();
6972
Logger.Info($"Generation time: {stopwatch.Elapsed } seconds.");
7073
}
7174

0 commit comments

Comments
 (0)