This repository was archived by the owner on Jan 1, 2021. It is now read-only.
forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLitUp.cs
More file actions
204 lines (164 loc) · 6.44 KB
/
Copy pathLitUp.cs
File metadata and controls
204 lines (164 loc) · 6.44 KB
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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Buildalyzer;
using Buildalyzer.Workspaces;
using DocGenerator.Documentation.Files;
using Microsoft.CodeAnalysis;
namespace DocGenerator
{
public static class LitUp
{
private static readonly string[] SkipFolders = { "Debug", "Release" };
private static string GetTestProjectDir(string projectName) => Path.Combine(Program.InputDirPath, "..", "tests", projectName);
public static IEnumerable<DocumentationFile> InputFiles(string path) =>
from f in Directory.GetFiles(GetTestProjectDir("Tests"), $"{path}", SearchOption.AllDirectories)
let dir = new DirectoryInfo(f)
where dir?.Parent != null && !SkipFolders.Contains(dir.Parent.Name)
select DocumentationFile.Load(new FileInfo(f));
public static IEnumerable<IEnumerable<DocumentationFile>> GetDocumentFiles(Dictionary<string, Project> projects)
{
var testProject = projects["Tests"];
yield return testProject.Documents
.Where(d => d.Name.EndsWith(".doc.cs", StringComparison.OrdinalIgnoreCase))
.Select(d => new CSharpDocumentationFile(d, projects));
yield return testProject.Documents
.Where(d => d.Name.EndsWith("UsageTests.cs", StringComparison.OrdinalIgnoreCase))
.Select(d => new CSharpDocumentationFile(d, projects));
yield return InputFiles("*.png");
yield return InputFiles("*.gif");
yield return InputFiles("*.jpg");
// process asciidocs last as they may have generated
// includes to other output asciidocs
yield return InputFiles("*.asciidoc");
}
public static HashSet<string> ProjectsWeWant { get; } = new HashSet<string>
{
"Elasticsearch.Net", "Nest", "Tests"
};
public static async Task<int> GoAsync(string[] args)
{
//.NET core csprojects are not supported all that well.
// https://github.com/dotnet/roslyn/issues/21660 :sadpanda:
// Use Buildalyzer to get a workspace from the solution.
var options = new AnalyzerManagerOptions()
{
ProjectFilter = p => ProjectsWeWant.Contains(p.ProjectName)
};
var analyzer = new AnalyzerManager(Path.Combine(Program.InputDirPath, "..", "Elasticsearch.sln"), options);
var workspace = analyzer.GetWorkspace();
var seenFailures = false;
workspace.WorkspaceFailed += (s, e) =>
{
Console.Error.WriteLine($"Workplace failure: {e.Diagnostic.Message}");
seenFailures = true;
};
// Buildalyzer, similar to MsBuildWorkspace with the new csproj file format, does
// not pick up source documents in the project directory. Manually add them
// AddDocumentsToWorkspace(workspace);
var projects = workspace.CurrentSolution.Projects
.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
if (seenFailures)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Documentation failed to generate.");
Console.ResetColor();
return 2;
}
DeleteExistingTmpDocs();
foreach (var file in GetDocumentFiles(projects).SelectMany(s => s))
await file.SaveToDocumentationFolderAsync();
if (seenFailures)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Documentation failed to generate.");
Console.ResetColor();
return 2;
}
CopyBreakingChangesDocs();
DeleteExistingDocsAndSwap();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Documentation generated.");
Console.ResetColor();
if (Debugger.IsAttached)
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
return 0;
}
private static void DeleteExistingTmpDocs()
{
var outputDir = new DirectoryInfo(Program.TmpOutputDirPath);
if (!outputDir.Exists) return;
foreach (var file in outputDir.EnumerateFiles())
file.Delete();
}
private static void CopyBreakingChangesDocs()
{
var outputDir = new DirectoryInfo(Program.OutputDirPath);
var tmpDir = new DirectoryInfo(Program.TmpOutputDirPath);
if (!outputDir.Exists) throw new Exception($"Docs folder should be present in repos but does not exist at: {Program.OutputDirPath}");
if (!tmpDir.Exists)
throw new Exception($"Temp docs folder should be present in repos after generation ran but does not exist at: {Program.TmpOutputDirPath}");
foreach (var dir in outputDir.EnumerateDirectories())
{
if (!dir.Name.EndsWith("breaking-changes")) continue;
var newLocation = Path.Combine(tmpDir.FullName, dir.Name);
Console.WriteLine($"Moving {dir.Name} to: {tmpDir.FullName}");
dir.MoveTo(newLocation);
}
}
private static void DeleteExistingDocsAndSwap()
{
var outputDir = new DirectoryInfo(Program.OutputDirPath);
var tmpDir = new DirectoryInfo(Program.TmpOutputDirPath);
foreach (var file in outputDir.EnumerateFiles())
file.Delete();
foreach (var dir in outputDir.EnumerateDirectories())
dir.Delete(true);
WaitForActualDelete(outputDir);
Console.WriteLine($"Swapping {tmpDir.FullName} to {outputDir.FullName}");
tmpDir.MoveTo(Program.OutputDirPath);
static void WaitForActualDelete(FileSystemInfo toDelete)
{
Console.WriteLine($"Attempting to delete {toDelete.FullName}");
toDelete.Delete();
var x = 0;
for (x = 0; toDelete.Exists && x < 100; x++)
{
Thread.Sleep(100);
x++;
}
}
}
private static void AddDocumentsToWorkspace(AdhocWorkspace workspace)
{
// we only need source for the Tests project.
var projects = workspace.CurrentSolution.Projects.Where(p => p.Name == "Tests").ToList();
for (var i = 0; i < projects.Count; i++)
{
var project = projects[i];
var files = (from f in Directory.GetFiles(Path.GetDirectoryName(project.FilePath), "*.cs", SearchOption.AllDirectories)
let dir = new DirectoryInfo(f)
where dir?.Parent != null && !SkipFolders.Contains(dir.Parent.Name)
select new FileInfo(f)).ToList();
for (var index = 0; index < files.Count; index++)
{
var file = files[index];
var document = project.AddDocument(file.Name, File.ReadAllText(file.FullName), filePath: file.FullName);
project = document.Project;
}
if (!workspace.TryApplyChanges(project.Solution))
Console.WriteLine($"failed to apply changes to workspace from project {project.Name}");
}
}
}
}