-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentGenerator.cs
121 lines (97 loc) · 3.96 KB
/
DocumentGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.Extensions.Configuration;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using swagger2md.Helpers;
using System.Diagnostics.CodeAnalysis;
public class DocumentGenerator
{
private readonly IConfiguration configuration;
public DocumentGenerator(IConfiguration configuration) => this.configuration = configuration;
public void GenerateDocument()
{
var inputFile = configuration["inputFile"];
if (!TryGetFileStream(out var swaggerFileStream))
{
Console.WriteLine($"Input file not found. Use -i or --input argument.");
return;
}
var outputFile = configuration["outputFile"];
var openApiDocument = new OpenApiStreamReader().Read(swaggerFileStream, out var diagnostic);
var textWriter = GetTargetFileTextWriter(outputFile);
if (!string.Equals(configuration["skipTitle"], bool.TrueString, StringComparison.OrdinalIgnoreCase))
{
MarkdownHelper.BoldText(textWriter, openApiDocument.Info.Title);
}
MarkdownHelper.Text(textWriter, openApiDocument.Info.Description);
var tags = new Dictionary<string, List<(string Path, string Type, OpenApiOperation Operation)>>();
// group paths by tags
foreach (var pathInfo in openApiDocument.Paths)
foreach (var operation in pathInfo.Value.Operations)
foreach (var tag in operation.Value.Tags)
{
if (tags.TryGetValue(tag.Name, out var tagsValue))
tagsValue.Add((pathInfo.Key, operation.Key.ToString(), operation.Value));
else
tags.Add(tag.Name, [(pathInfo.Key, operation.Key.ToString(), operation.Value)]);
}
var generateSubFiles = string.Equals(configuration["subPages"], bool.TrueString, StringComparison.OrdinalIgnoreCase);
var outputDir = Path.GetDirectoryName(outputFile) ?? ".";
if (generateSubFiles)
{
if (string.IsNullOrEmpty(outputFile))
{
Console.WriteLine("Can not generate subfiles without outputFile property.");
return;
}
else
{
MarkdownHelper.TableOfSubpages(textWriter);
outputDir = Directory.CreateDirectory(Path.Combine(outputDir, Path.GetFileNameWithoutExtension(outputFile).Replace(' ', '-'))).FullName;
}
}
else
{
MarkdownHelper.TableOfContents(textWriter);
}
foreach (var tagInfo in tags)
{
var tagTextWriter = generateSubFiles && outputDir is not null
? File.CreateText(Path.Combine(outputDir, $"{tagInfo.Key.Replace(' ', '-')}.md"))
: textWriter;
MarkdownHelper.Header1(tagTextWriter, tagInfo.Key);
if (textWriter != tagTextWriter)
{
MarkdownHelper.TableOfContents(tagTextWriter);
}
foreach (var operationInfo in tagInfo.Value)
{
OpenApiHelpers.WriteOperationInfo(tagTextWriter, operationInfo.Path, operationInfo.Type, operationInfo.Operation, openApiDocument.Components.Schemas);
tagTextWriter.WriteLine();
}
if (textWriter != tagTextWriter)
{
tagTextWriter.Close();
}
}
textWriter.Close();
}
private bool TryGetFileStream([NotNullWhen(true)] out Stream? fileStream)
{
var inputFile = configuration["inputFile"];
if (File.Exists(inputFile))
{
fileStream = File.OpenRead(inputFile);
return true;
}
fileStream = default;
return false;
}
private static TextWriter GetTargetFileTextWriter(string? outputFile)
{
if (!string.IsNullOrEmpty(outputFile))
{
return File.CreateText(outputFile);
}
return Console.Out;
}
}