-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME_TEMPLATE.tt
172 lines (153 loc) · 5.31 KB
/
README_TEMPLATE.tt
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="netstandard" #>
<#@ output extension=".md" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#
var currentPath = Host.ResolvePath(string.Empty);
var scenariosPath = Path.Combine(currentPath, "UsageScenarios");
var solutionPath = Path.Combine(currentPath, "..");
var infos = new List<Dictionary<string, string>>();
var commentsDir = Path.Combine(scenariosPath, "Comments");
if (Directory.Exists(commentsDir))
{
foreach (var file in Directory.GetFiles(commentsDir, "*.txt"))
{
File.Delete(file);
}
}
else
{
Directory.CreateDirectory(commentsDir);
}
foreach (var file in Directory.GetFiles(scenariosPath, "*.cs"))
{
var isBody = false;
var vars = new Dictionary<string, string>();
var body = new List<string>();
int? offset = null;
foreach (var line in File.ReadAllLines(file))
{
if (vars.Count == 0)
{
vars["visible"] = "false";
vars["tag"] = string.Empty;
vars["priority"] = string.Empty;
vars["description"] = string.Empty;
vars["header"] = string.Empty;
vars["footer"] = string.Empty;
vars["file"] = file;
infos.Add(vars);
}
var str = line.Trim();
switch (str)
{
case "// {":
isBody = true;
continue;
case "// }":
isBody = false;
continue;
}
if (str.StartsWith("// $"))
{
var parts = str.Substring(4, str.Length - 4).Split([
'='
], 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
var key = parts[0];
if (key is "header" or "footer")
{
var curVar = vars[key];
if (curVar != string.Empty)
{
curVar += "\n";
}
vars[key] = curVar + parts[1];
}
else
{
vars[key] = parts[1];
}
}
continue;
}
if (isBody)
{
if (line.TrimStart().StartsWith("// ## "))
{
body.Add(line.Replace("// ## ", string.Empty));
continue;
}
if (!string.IsNullOrWhiteSpace(line))
{
var curOffset = line.Length - line.TrimStart().Length;
if (curOffset < (offset ?? int.MaxValue))
{
offset = curOffset;
}
}
body.Add(line);
}
}
offset ??= 0;
var content =
from line in body
select line.Length > offset ? line.Substring(offset.Value, line.Length - offset.Value) : line.Trim();
vars["body"] = string.Join(Environment.NewLine, content) + Environment.NewLine;
}
#>
## API
<#
foreach (var group in infos.Where(info => info["visible"] == "true").GroupBy(i => i["tag"]).OrderBy(i => i.Key))
{
var groupNameIndex = group.Key.IndexOf(' ');
var groupName = group.Key.Substring(groupNameIndex + 1, group.Key.Length - groupNameIndex - 1);
#>- <#= groupName #>
<#
foreach (var info in group.OrderBy(i => i["priority"] + "_" + i["description"]))
{
var description = info["description"];
var reference = "#" + description.Replace(" ", "-")
.Replace("'", string.Empty)
.Replace("/", string.Empty)
.Replace("`", string.Empty)
.Replace("\\", string.Empty)
.ToLowerInvariant();
#> - [<#= description #>](<#= reference #>)
<#
}
}
#>
<#
foreach (var group in infos.Where(info => info["visible"] == "true").GroupBy(i => i["tag"]).OrderBy(i => i.Key))
{
var groupNameIndex = group.Key.IndexOf(' ');
var groupName = group.Key.Substring(groupNameIndex + 1, group.Key.Length - groupNameIndex - 1);
#>
## <#= groupName #>
<#
foreach (var info in group.OrderBy(i => i["priority"] + "_" + i["description"]))
{
var file = info["file"];
var body = info["body"];
File.WriteAllText(Path.Combine(commentsDir, Path.GetFileNameWithoutExtension(file) + ".txt"), body);
#>
### <#= info["description"] #>
<#= info["header"] #>
``` CSharp
<#= body #>```
<#= info["footer"] #>
<#
}
}
var readmeBodyFile = Path.Combine(solutionPath, "README_BODY.md");
var readmeFile = Path.Combine(solutionPath, "README.md");
var samplesFile = Path.Combine(currentPath, "README_TEMPLATE.md");
File.Copy(readmeBodyFile, readmeFile, true);
var samplesContent = File.ReadAllText(samplesFile);
File.AppendAllText(readmeFile, samplesContent);
#>