-
Notifications
You must be signed in to change notification settings - Fork 866
/
Copy pathHostServiceCreator.cs
138 lines (123 loc) · 4.78 KB
/
HostServiceCreator.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Collections.Immutable;
using Docfx.Common;
using Docfx.Plugins;
namespace Docfx.Build.Engine;
class HostServiceCreator
{
private readonly DocumentBuildContext _context;
public HostServiceCreator(DocumentBuildContext context)
{
_context = context;
}
public virtual HostService CreateHostService(
DocumentBuildParameters parameters,
TemplateProcessor templateProcessor,
IMarkdownService markdownService,
IEnumerable<IInputMetadataValidator> metadataValidator,
IDocumentProcessor processor,
IEnumerable<FileAndType> files)
{
var (models, invalidFiles) = LoadModels(files, parameters, processor);
var hostService = new HostService(
models,
parameters.VersionName,
parameters.VersionDir,
parameters.GroupInfo)
{
MarkdownService = markdownService,
Processor = processor,
Template = templateProcessor,
Validators = metadataValidator?.ToImmutableList(),
InvalidSourceFiles = invalidFiles.ToImmutableList(),
};
return hostService;
}
public virtual (FileModel model, bool valid) Load(
IDocumentProcessor processor,
ImmutableDictionary<string, object> metadata,
FileMetadata fileMetadata,
FileAndType file)
{
using (new LoggerFileScope(file.File))
{
Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}: Loading...");
var fileMeta = NeedApplyMetadata()
? ApplyFileMetadata(file.FullPath, metadata, fileMetadata)
: ImmutableDictionary<string, object>.Empty;
try
{
return (processor.Load(file, fileMeta), true);
}
catch (Exception e)
{
if (e is not DocumentException)
{
var message = e is ArgumentException or NullReferenceException
? e.ToString()
: e.Message;
Logger.LogError(
$"Unable to load file '{file.File}' via processor '{processor.Name}': {message}",
code: ErrorCodes.Build.InvalidInputFile);
}
return (null, false);
}
}
bool NeedApplyMetadata()
{
return file.Type != DocumentType.Resource;
}
}
private (IEnumerable<FileModel> models, IEnumerable<string> invalidFiles) LoadModels(IEnumerable<FileAndType> files, DocumentBuildParameters parameters, IDocumentProcessor processor)
{
if (files == null)
{
return (Enumerable.Empty<FileModel>(), Enumerable.Empty<string>());
}
var models = new ConcurrentBag<FileModel>();
var invalidFiles = new ConcurrentBag<string>();
files.RunAll(file =>
{
var (model, valid) = Load(processor, parameters.Metadata, parameters.FileMetadata, file);
if (model != null)
{
models.Add(model);
}
if (!valid)
{
invalidFiles.Add(file.File);
}
}, _context.MaxParallelism);
return (models.OrderBy(m => m.File, StringComparer.Ordinal).ToArray(), invalidFiles);
}
private static ImmutableDictionary<string, object> ApplyFileMetadata(
string file,
ImmutableDictionary<string, object> metadata,
FileMetadata fileMetadata)
{
if (fileMetadata == null || fileMetadata.Count == 0)
{
return metadata;
}
var result = new Dictionary<string, object>(metadata);
var baseDir = string.IsNullOrEmpty(fileMetadata.BaseDir) ? Directory.GetCurrentDirectory() : fileMetadata.BaseDir;
var relativePath = PathUtility.MakeRelativePath(baseDir, file);
foreach (var item in fileMetadata)
{
// As the latter one overrides the former one, match the pattern from latter to former
for (int i = item.Value.Length - 1; i >= 0; i--)
{
if (item.Value[i].Glob.Match(relativePath))
{
// override global metadata if metadata is defined in file metadata
result[item.Value[i].Key] = item.Value[i].Value;
Logger.LogDiagnostic($"{relativePath} matches file metadata with glob pattern {item.Value[i].Glob.Raw} for property {item.Value[i].Key}");
break;
}
}
}
return result.ToImmutableDictionary();
}
}