-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerate.linq
370 lines (318 loc) · 12.5 KB
/
Generate.linq
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<Query Kind="Program">
<Reference Relative="..\..\src\bin\Debug\net7.0-windows\KAT.Camelot.Extensibility.Excel.AddIn.dll">C:\BTR\Camelot\Extensibility\Excel.AddIn\src\bin\Debug\net7.0-windows\KAT.Camelot.Extensibility.Excel.AddIn.dll</Reference>
<NuGetReference>CliWrap</NuGetReference>
<Namespace>CliWrap</Namespace>
<Namespace>CliWrap.Buffered</Namespace>
<Namespace>ExcelDna.Integration</Namespace>
<Namespace>KAT.Camelot.Extensibility.Excel.AddIn</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<RuntimeVersion>7.0</RuntimeVersion>
</Query>
async Task Main( string[] args )
{
try
{
Func<string, string, string?> getArgument = (a, key) =>
{
if (a.StartsWith(key + ":", StringComparison.InvariantCultureIgnoreCase))
{
return a.Substring(a.IndexOf(":") + 1);
}
return null;
};
formatOutput = args.Select(a => getArgument(a, "tool")).FirstOrDefault(a => !string.IsNullOrEmpty(a)) != "Visual Studio";
var isDebug = args.Select(a => getArgument(a, "configurationname")).FirstOrDefault(a => !string.IsNullOrEmpty(a)) == "Debug";
WriteLine($"\r\n========== KAT Tools Documentation Processing ==========\r\n", false);
WriteLine("Detecting DNA Functions...", true);
var assembly = typeof(Ribbon).Assembly;
var info =
assembly.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => (m.GetCustomAttribute<KatExcelFunctionAttribute>() ?? m.GetCustomAttribute<ExcelFunctionAttribute>()) != null)
.Select(m =>
{
var katFunc = m.GetCustomAttribute<KatExcelFunctionAttribute>();
var dnaFunc = (katFunc as ExcelFunctionAttribute) ?? m.GetCustomAttribute<ExcelFunctionAttribute>()!;
return new
{
Name = dnaFunc.Name ?? m.Name,
Category = dnaFunc.Category,
Description = katFunc?.Summary ?? dnaFunc.Description,
Returns = katFunc?.Returns,
Remarks = katFunc?.Remarks,
Example = katFunc?.Example,
HelpTopic = dnaFunc.HelpTopic,
CreateDebugFunction = katFunc?.CreateDebugFunction ?? false,
Arguments =
m.GetParameters()
.Select(p =>
{
var katArg = p.GetCustomAttribute<KatExcelArgumentAttribute>();
var dnaArg = (katArg as ExcelArgumentAttribute) ?? p.GetCustomAttribute<ExcelArgumentAttribute>();
return new
{
Name = katArg?.DisplayName ?? dnaArg?.Name ?? p.Name!,
Description = katArg?.Summary ?? dnaArg?.Description ?? "TODO: Document this parameter.",
Type = katArg?.Type ?? p.ParameterType,
IsOptional = p.IsOptional,
DefaultValue = katArg?.Default ?? p.DefaultValue?.ToString()
};
})
};
})
.ToArray();
XNamespace ns = "http://schemas.excel-dna.net/intellisense/1.0";
var fileName = isDebug ? "KAT.Extensibility.Excel.Debug.intellisense.xml" : "KAT.Extensibility.Excel.intellisense.xml";
WriteLine($"Generating {fileName}...", true);
var intelliSense =
new XElement(ns + "IntelliSense",
new XElement(ns + "FunctionInfo",
info.Select(i =>
new XElement(ns + "Function",
new XAttribute("Name", i.Name),
new XAttribute("Description", i.Description),
i.HelpTopic != null ? new XAttribute("HelpTopic", i.HelpTopic) : null,
i.Arguments.Select(a =>
new XElement(ns + "Argument",
new XAttribute("Name", a.Name),
new XAttribute("Description", a.Description)
)
)
)
)
)
);
WriteLine($"Saving {fileName}", true);
if ( isDebug )
{
intelliSense.Save(@$"C:\BTR\Camelot\Extensibility\Excel.AddIn\src\bin\Debug\net7.0-windows\{fileName}");
}
else
{
intelliSense.Save(@$"C:\BTR\Camelot\Extensibility\Excel.AddIn\.vscode\DnaDocumentation\{fileName}");
intelliSense.Save(@$"C:\BTR\Camelot\Extensibility\Excel.AddIn\dist\Resources\{fileName}");
intelliSense.Save(@$"C:\BTR\Camelot\Extensibility\Excel.AddIn\dist\Resources\{Path.GetFileNameWithoutExtension( fileName )}.x86.xml");
intelliSense.Save(@$"C:\Program Files\Microsoft Office\root\Office16\Library\KatTools\{fileName}");
}
WriteLine("Generating Markdown Documentation...", true);
var functionCategories = info
.OrderBy(i => i.Name)
.GroupBy(g => g.Category);
var validFiles = new List<string>();
validFiles.Add("RBLe.md");
foreach (var category in functionCategories)
{
var categoryName = category.Key.Replace(" ", "");
var templateMd = @$"C:\BTR\Camelot\Extensibility\Excel.AddIn\.vscode\DnaDocumentation\{categoryName}.md";
var templateContent = await File.ReadAllTextAsync(templateMd);
var categoryTable = new StringBuilder();
foreach (var m in category)
{
categoryTable.AppendLine($"[`{m.Name}`](RBLe{categoryName}.{m.Name}.md) | {m.Description}");
if (m.CreateDebugFunction)
{
categoryTable.AppendLine($"[`{m.Name}Debug`](RBLe{categoryName}.{m.Name}Debug.md) | Debug version of {m.Name} that returns value or exception string (instead of #VALUE on error). {m.Description}");
}
var returns = m.Returns != null
? $"{Environment.NewLine}**Returns:** {m.Returns}"
: null;
var remarks = m.Remarks != null
? $"{Environment.NewLine}## Remarks{Environment.NewLine + Environment.NewLine}{string.Join(" " + Environment.NewLine, m.Remarks.Split(Environment.NewLine))}"
: null;
var example = m.Example != null
? $"{Environment.NewLine}## Example{Environment.NewLine + Environment.NewLine}{m.Example}"
: null;
var parameters =
string.Join(
Environment.NewLine,
m.Arguments.Select(a =>
{
var defaultValue = a.IsOptional ? a.DefaultValue : null;
if (defaultValue != null && !defaultValue.StartsWith("`"))
{
defaultValue = a.Type == typeof(string)
? $"`\"{defaultValue}\"`"
: $"`{defaultValue}`";
}
return $"`{a.Name}` | {a.Type.Name + (a.IsOptional ? "?" : "")} | {defaultValue} | {a.Description.Replace("|", "\\|")}";
})
);
var functionTemplate = @$"# {m.Name} Function
{m.Description}
{string.Join("", new[] { returns, remarks }.Where(i => i != null))}
## Syntax
```excel
={m.Name}({string.Join(", ", m.Arguments.Select(a => a.Name))})
```
Parameter | Type | Default | Description
---|---|---|---
{parameters}
{example}
[Back to {category.Key}](RBLe{categoryName}.md) | [Back to All RBLe Functions](RBLe.md#function-documentation)";
var functionPath = @$"C:\BTR\Documentation\Camelot\RBLe\RBLe{categoryName}.{m.Name}.md";
validFiles.Add(Path.GetFileName(functionPath));
await File.WriteAllTextAsync(functionPath, functionTemplate);
if (m.CreateDebugFunction)
{
functionPath = @$"C:\BTR\Documentation\Camelot\RBLe\RBLe{categoryName}.{m.Name}Debug.md";
validFiles.Add(Path.GetFileName(functionPath));
await File.WriteAllTextAsync(
functionPath,
functionTemplate
.Replace(m.Name, m.Name + "Debug")
.Replace(m.Description, $"Debug version of {m.Name} that returns value or exception string (instead of #VALUE on error). {m.Description}")
);
}
}
var categoryPath = @$"C:\BTR\Documentation\Camelot\RBLe\RBLe{categoryName}.md";
validFiles.Add(Path.GetFileName(categoryPath));
await File.WriteAllTextAsync(
categoryPath,
templateContent.Replace("{FUNCTIONS}", categoryTable.ToString())
);
}
var filesToDelete = new DirectoryInfo(@"C:\BTR\Documentation\Camelot\RBLe\").GetFiles("RBLe*.*").Where(f => !validFiles.Contains(f.Name));
foreach (var f in filesToDelete)
{
File.Delete(f.FullName);
}
var gitPath = @"C:\BTR\Documentation\Camelot";
var currentBuildBranch = (await GetRepositoryBranchesAsync(gitPath)).Single(b => b.IsActive);
if (currentBuildBranch.NeedsCommit && currentBuildBranch.StatusLog.Any(s => s.Contains("RBLe/RBLe")))
{
WriteLine("Committing Markdown Documentation...", true);
await CallGitCommandLineAsync(gitPath, new[] { "add", "RBLe/RBLe*" });
await CallGitCommandLineAsync(gitPath, new[] { "com", "-m", $"RBLe Function Documentation" });
WriteLine("Pushing Markdown Documentation...", true);
await CallGitCommandLineAsync(gitPath, new[] { "push" });
}
#if !CMD
functionCategories.Dump();
#endif
WriteLine($"\r\n========== KAT Tools Documentation Complete ==========\r\n", true);
Environment.Exit(Environment.ExitCode = 0);
}
catch (Exception ex)
{
WriteLine($"\r\n========== KAT Tools Documentation Processing: \x1b[31mfailed\x1b[0m ==========\r\n");
WriteLine("\x1b[31m");
WriteLine($"{ex.GetType().ToString()}: {ex.Message}");
var inner = ex.InnerException;
if (inner != null)
{
WriteLine($"{Environment.NewLine}Inner: {inner.GetType().ToString()}: {inner.Message}");
}
WriteLine($"{Environment.NewLine}{(inner ?? ex).StackTrace}");
WriteLine("\x1b[0m");
Environment.Exit(Environment.ExitCode = -1);
}
}
public static class Extensions
{
public static bool IsNullable(this Type type) => Nullable.GetUnderlyingType( type ) != null;
}
Regex branchRegEx = new Regex(@"^(?<branch>\S*)\s*(?<commit>\S*)\s*(?:\[ahead (?<ahead>\d+)(?:, behind (?<behind>\d+))?\]|\[behind (?<behindOnly>\d+)\])?\s*(?<comment>.*)", RegexOptions.Compiled);
async Task<GitBranch[]> GetRepositoryBranchesAsync(string repositoryPath)
{
var statusRaw = await CallGitCommandLineAsync(repositoryPath, new[] { "st" });
var branchesRaw = await CallGitCommandLineAsync(repositoryPath, new[] { "br", "-v" });
return (
from b in branchesRaw
let match = branchRegEx.Match(b.Substring(2))
let branch = match.Groups["branch"].Value
let branchParts = branch.Split('/')
let isRemote = string.Compare(branchParts[0], "remotes", true) == 0
let branchName = isRemote
? string.Join("/", branchParts.Skip(2))
: string.Join("/", branchParts)
let remote = isRemote
? branchParts[1]
: null
let ahead = match.Groups["ahead"].Success ? int.Parse(match.Groups["ahead"].Value) : 0
let behindGroup = match.Groups["behind"].Success ? match.Groups["behind"] : match.Groups["behindOnly"]
let behind = behindGroup.Success ? int.Parse(behindGroup.Value) : 0
let needsCommit = b.StartsWith("*") && statusRaw.Length > 1
where string.Compare(branchName, "HEAD", true) != 0
select new GitBranch
{
Branch = branchName,
Remote = remote ?? "origin",
Commit = match.Groups["commit"].Value,
Comment = match.Groups["comment"].Value,
IsRemote = isRemote,
IsActive = b.StartsWith("*"),
NeedsCommit = needsCommit,
NeedsSync = b.StartsWith("*") && !needsCommit && (ahead + behind) > 0,
Ahead = ahead,
Behind = behind,
StatusLog = statusRaw
// FeatureBranch = ( string.Compare( branchName, "main", true ) != 0 && string.Compare( branchName, "_Test", true ) != 0 )
}
).ToArray();
}
class GitBranch
{
public required string Branch { get; set; }
public required string Remote { get; set; }
public required string Commit { get; set; }
public required string Comment { get; set; }
public bool IsRemote { get; set; }
public bool IsActive { get; set; }
public bool NeedsCommit { get; set; }
public bool NeedsSync { get; set; }
public int Ahead { get; set; }
public int Behind { get; set; }
public string[] StatusLog { get; set; } = Array.Empty<string>();
}
async Task<string[]> CallGitCommandLineAsync(string repoPath, string[] arguments)
{
// http://stackoverflow.com/questions/6127063/running-git-from-windows-cmd-line-where-are-key-files
// SSH requires HOME environment variable to be set.
try
{
var results =
await Cli.Wrap(@"C:\Program Files\Git\bin\git.exe")
.WithWorkingDirectory(repoPath)
.WithArguments(arguments)
.ExecuteBufferedAsync();
var logRaw = string.IsNullOrEmpty(results.StandardOutput) && !string.IsNullOrEmpty(results.StandardError)
? results.StandardError.Split('\n').ToArray()
: results.StandardOutput.Split('\n').ToArray();
return logRaw.Where(b => !string.IsNullOrEmpty(b)).ToArray();
}
catch (Exception ex)
{
throw new ApplicationException($"Unable to issue 'git {string.Join(" ", arguments)}' to {repoPath}", ex);
}
}
StringBuilder? output = null;
bool formatOutput = true;
void WriteLine(string line, bool flush = true)
{
var l =
line.IndexOf("FAIL") > -1 ? $"\x1b[31m{line}\x1b[0m" :
line.IndexOf("WARN") > -1 ? $"\x1b[33m{line}\x1b[0m" :
line;
if (!formatOutput)
{
// For Visual Studio, install VSColorOutput extension and add .* failed .* regular expression as the first expression it processes
l = l.Replace("\x1b[31m", "").Replace("\x1b[33m", "").Replace("\x1b[0m", "");
}
if (!flush)
{
if (output == null)
{
output = new StringBuilder();
}
output.AppendLine(l);
}
else
{
if (output != null)
{
Console.WriteLine(output.ToString());
output = null;
}
Console.WriteLine(l);
}
}