-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildProjectHandler.cs
328 lines (272 loc) · 12.8 KB
/
BuildProjectHandler.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
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
using DNDocs.Application.Commands.Projects;
using DNDocs.Application.Shared;
using DNDocs.Docs.Api.Client;
using DNDocs.Domain.Entity.App;
using DNDocs.Domain.Enums;
using DNDocs.Domain.Service;
using DNDocs.Domain.Services;
using DNDocs.Domain.UnitOfWork;
using DNDocs.Domain.Utils;
using DNDocs.Domain.Utils.Docfx;
using DNDocs.Shared.Utils;
using Ganss.Xss;
using Markdig;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace DNDocs.Application.CommandHandlers.Projects
{
internal class BuildProjectHandler : CommandHandlerA<BuildProjectCommand>
{
private IAppUnitOfWork appUow;
private ISystemMessages systemMessages;
private IAppManager appManager;
private IDocfxManager docfxManager;
private IDDocsApiClient ddocsApiClient;
private INugetRepositoryFacade nugetRepositoryFacade;
public BuildProjectHandler(
IAppUnitOfWork appUow,
ISystemMessages systemMessages,
IAppManager appManager,
IDocfxManager docfxManager,
IDDocsApiClient ddocsApiClient,
INugetRepositoryFacade nugetRepositoryFacade)
{
this.appUow = appUow;
this.systemMessages = systemMessages;
this.appManager = appManager;
this.docfxManager = docfxManager;
this.ddocsApiClient = ddocsApiClient;
this.nugetRepositoryFacade = nugetRepositoryFacade;
}
class BuildChainContext
{
public Project Project
{
get { return project; }
set
{
if (project != null) throw new InvalidOperationException();
project = value;
}
}
public IOSTempFolder DocfxBuildFolder
{
get { return docfxBuildFolder; }
set { docfxBuildFolder = docfxBuildFolder == null ? value : throw new InvalidOperationException(); }
}
public IDocfxManager DocfxManager
{
get { return docfxManager; }
set { docfxManager = docfxManager == null ? value : throw new InvalidOperationException(); }
}
private Project project = null;
private IOSTempFolder docfxBuildFolder;
private IDocfxManager docfxManager;
}
public override async Task Handle(BuildProjectCommand command)
{
logger.LogInformation("starting to build project id: {0}", command.ProjectId);
var context = new BuildChainContext();
var p = await this.appUow.GetSimpleRepository<Project>().GetByIdCheckedAsync(command.ProjectId);
// load related data ef will setup
this.appUow.GetSimpleRepository<NugetPackage>().Query().Where(t => t.ProjectId == p.Id).ToList();
Validation.ThrowError(p.StateDetails == ProjectStateDetails.Building, "Project is building now, cannot run build again");
context.Project = p;
p.LastDocfxBuildTime = DateTime.UtcNow;
List<Action<BuildChainContext>> steps = new List<Action<BuildChainContext>>()
{
Step_DocfxInit,
Step_FetchDataIntoDocfxProject,
Step_DocfxBuildCurrentState,
Step_SendDocfxBuildResultToDndocs,
Step_CleanDocfxAfterBuild,
Step_BuildCompletedSuccess
};
Exception exception = null;
foreach (var step in steps)
{
try
{
logger.LogInformation("starting to execute build step project id: {0}, step: {1}", command.ProjectId, step.Method.Name);
step(context);
}
catch (Exception e)
{
logger.LogError(e, "failed build project on step: {0}, projectid: {1}", step.GetMethodInfo().Name, command.ProjectId);
p.State = ProjectState.NotActive;
p.StateDetails = ProjectStateDetails.BuildFailed;
p.LastDocfxBuildErrorDateTime = DateTime.UtcNow;
p.StateDetails = ProjectStateDetails.BuildFailed;
p.State = ProjectState.NotActive;
p.LastDocfxBuildErrorLog = Helpers.ExceptionToStringForLogs(e);
await appUow.SaveChangesAsync();
Validation.ThrowError($"Failed to build project on step: {step.GetMethodInfo().Name}");
}
}
await appUow.SaveChangesAsync();
}
void Step_BuildCompletedSuccess(BuildChainContext c)
{
var project = c.Project;
project.LastDocfxBuildTime = DateTime.UtcNow;
project.State = ProjectState.Active;
project.StateDetails = ProjectStateDetails.Ready;
systemMessages.Success(project, "Build project completed successfully", $"Build project completed successfully");
}
void Step_CleanDocfxAfterBuild(BuildChainContext c)
{
var docfxManager = c.DocfxManager;
var project = c.Project;
docfxManager.CleanAfterBuild();
c.DocfxBuildFolder.Dispose();
}
void Step_SendDocfxBuildResultToDndocs(BuildChainContext c)
{
var project = c.Project;
using (var tempZipFolder = appManager.CreateTempFolder())
{
var zipOsPath = Path.Combine(tempZipFolder.OSFullPath, "site.zip");
ZipFile.CreateFromDirectory(docfxManager.OSPathSiteDirectory, zipOsPath);
using (var zipStream = new FileStream(zipOsPath, FileMode.Open, FileAccess.Read))
{
logger.LogInformation("Starting createorreplace on docs.dndocs. id: {0} name: {1} urlprefix: {2} project_type: {3}", project.Id, project.ProjectName, project.UrlPrefix, project.ProjectType);
var task = ddocsApiClient.Management_CreateProject(
project.Id,
project.ProjectName,
$"",
project.UrlPrefix,
project.PVGitTag,
project.NugetOrgPackageName,
project.NugetOrgPackageVersion,
(int)project.ProjectType,
zipStream);
task.Wait();
logger.LogInformation("completed createorreplace on docs.dndocs. id: {0} name: {1} urlprefix: {2} project_type: {3} ", project.Id, project.ProjectName, project.UrlPrefix, project.ProjectType);
var result = task.Result;
}
}
}
void Step_FetchDataIntoDocfxProject(BuildChainContext c)
{
var project = c.Project;
FetchNugetXmlAndDlls(project.ProjectNugetPackages, docfxManager.OSPathBinDir);
FetchMdDocs(project, docfxManager.OSPathIndexHtml, docfxManager.OSPathArticlesDir);
}
void Step_DocfxBuildCurrentState(BuildChainContext c)
{
docfxManager.AutogenerateArticlesTOC();
docfxManager.SetTemplate(c.Project.DocfxTemplate);
docfxManager.Build();
}
void Step_DocfxInit(BuildChainContext c)
{
var tempFolder = appManager.CreateTempFolder();
var project = c.Project;
systemMessages.Trace(project, "Build project started", $"Starting to build a project {project}");
project.State = ProjectState.NotActive;
project.StateDetails = ProjectStateDetails.Building;
docfxManager.Init(tempFolder.OSFullPath, project);
c.DocfxBuildFolder = tempFolder;
c.DocfxManager = docfxManager;
}
void FetchNugetXmlAndDlls(List<NugetPackage> newPackages, string outputFolder)
{
foreach (var package in newPackages)
{
try
{
var packageBinData = nugetRepositoryFacade.FetchDllAndXmlFromPackage(package.IdentityId, package.IdentityVersion);
foreach (var item in packageBinData)
{
File.WriteAllBytes(Path.Combine(outputFolder, item.FileName), item.ByteData);
}
}
catch (Exception e)
{
logger.LogError(e, "failed to fetch nuget packages");
throw e;
}
}
}
private IGit GetGit(Project project)
{
IGit git = appManager.OpenGitRepo(project.GitMdRepoUrl);
git.CheckoutBranch(project.GitMdBranchName);
git.Pull();
return git;
}
private void FetchMdDocs(Project project, string outputReadmeFilePath, string outputArticlesFolder)
{
if (string.IsNullOrWhiteSpace(project.GitMdRepoUrl)) return;
using (var git = GetGit(project))
{
FetchMdDocs2(project, outputReadmeFilePath, outputArticlesFolder, git);
}
}
private void FetchMdDocs2(Project project, string outputReadmeFilePath, string outputArticlesFolder, IGit git)
{
if (string.IsNullOrWhiteSpace(project.GitMdRelativePathReadme) && string.IsNullOrWhiteSpace(project.GitMdRelativePathDocs))
return;
Validation.AppEx(string.IsNullOrWhiteSpace(project.GitDocsCommitHash), "git commit hash empty cannot build docs (application exception)");
systemMessages.Trace(project,
"Starting to prepare MD docs.",
$"System started to prepare MD docs.\r\nMD docs git commit hash: {project.GitDocsCommitHash}");
git.PruneRemote();
git.FetchAll();
git.CheckoutBranch(project.GitMdBranchName);
git.Pull();
git.CheckoutCommit(project.GitDocsCommitHash);
string projectRepoOsPath = git.RepoOSPath;
// TODO: this is expensive to create,
// but not sure if this is thread safe, maybe create static prop and use semaphore for building all concurrent project??
// only in single thread?
var pp = (new Markdig.MarkdownPipelineBuilder())
.UseAdvancedExtensions()
.Build();
var htmlSanitizer = new HtmlSanitizer();
if (!string.IsNullOrWhiteSpace(project.GitMdRelativePathDocs))
{
// first directory has all project files
var docsRelPath = project.GitMdRelativePathDocs;
string articlesInRepoFolderOSPath = Path.Combine(projectRepoOsPath, docsRelPath);
if (!Directory.Exists(articlesInRepoFolderOSPath))
{
var userMsg = $"Directory '{docsRelPath}' (relative path) does not exists in repo folder\n " +
$"{project}\nGit Branch Name: {project.GitMdBranchName}\nGitDocsCommitHash: {project.GitDocsCommitHash}";
logger.LogError($"{userMsg}\nospathRepo:{projectRepoOsPath};\nGithubMdRelativePathDocs: {docsRelPath}");
Validation.ThrowError(true, userMsg);
}
string[] articles = Directory.GetFiles(articlesInRepoFolderOSPath, "*.md", SearchOption.AllDirectories);
foreach (var mdOsPath in articles)
{
var md = File.ReadAllText(mdOsPath);
var mdHtml = Markdig.Markdown.ToHtml(md, pp);
mdHtml = htmlSanitizer.Sanitize(mdHtml);
var fileRelativePath = mdOsPath.Substring(articlesInRepoFolderOSPath.Length + 1);
var saveOsPath = Path.Combine(outputArticlesFolder, fileRelativePath);
var saveOsDir = Path.GetDirectoryName(saveOsPath);
if (!Directory.Exists(saveOsDir))
Directory.CreateDirectory(saveOsDir);
File.WriteAllText(saveOsPath, mdHtml);
}
}
if (!string.IsNullOrWhiteSpace(project.GitMdRelativePathReadme))
{
var readmeRelPath = project.GitMdRelativePathReadme;
var readmeOsPath = Path.Combine(projectRepoOsPath, readmeRelPath);
Validation.ThrowError(!File.Exists(readmeOsPath), $"Readme file '{readmeRelPath}' does not exists in repo folder");
string readmeMdFileContent = File.ReadAllText(readmeOsPath);
var homeContentHtml = Markdig.Markdown.ToHtml(readmeMdFileContent, pp);
File.WriteAllText(outputReadmeFilePath, homeContentHtml);
}
}
}
}