-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAssembleSources.cs
245 lines (216 loc) · 7.89 KB
/
AssembleSources.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
// 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.Collections.Frozen;
using System.IO.Abstractions;
using Documentation.Assembler.Building;
using Documentation.Assembler.Configuration;
using Documentation.Assembler.Navigation;
using Documentation.Assembler.Sourcing;
using Elastic.Markdown.CrossLinks;
using Elastic.Markdown.IO.Configuration;
using Elastic.Markdown.IO.Navigation;
using Microsoft.Extensions.Logging.Abstractions;
using Spectre.Console;
using YamlDotNet.RepresentationModel;
namespace Documentation.Assembler;
public record TocTopLevelMapping
{
public required Uri Source { get; init; }
public required string SourcePathPrefix { get; init; }
public required Uri TopLevelSource { get; init; }
public required Uri ParentSource { get; init; }
}
public record TocConfigurationMapping
{
public required TocTopLevelMapping TopLevel { get; init; }
public required ConfigurationFile RepositoryConfigurationFile { get; init; }
public required TableOfContentsConfiguration TableOfContentsConfiguration { get; init; }
}
public class AssembleSources
{
public AssembleContext AssembleContext { get; }
public FrozenDictionary<string, AssemblerDocumentationSet> AssembleSets { get; }
public FrozenDictionary<Uri, TocTopLevelMapping> TocTopLevelMappings { get; }
public FrozenDictionary<Uri, TocConfigurationMapping> TocConfigurationMapping { get; }
public TableOfContentsTreeCollector TreeCollector { get; } = new();
public PublishEnvironmentUriResolver UriResolver { get; }
public static async Task<AssembleSources> AssembleAsync(AssembleContext context, Checkout[] checkouts, Cancel ctx)
{
var sources = new AssembleSources(context, checkouts);
foreach (var (_, set) in sources.AssembleSets)
await set.DocumentationSet.ResolveDirectoryTree(ctx);
return sources;
}
private AssembleSources(AssembleContext assembleContext, Checkout[] checkouts)
{
AssembleContext = assembleContext;
TocTopLevelMappings = GetConfiguredSources(assembleContext);
var crossLinkFetcher = new AssemblerCrossLinkFetcher(NullLoggerFactory.Instance, assembleContext.Configuration);
UriResolver = new PublishEnvironmentUriResolver(TocTopLevelMappings, assembleContext.Environment);
var crossLinkResolver = new CrossLinkResolver(crossLinkFetcher, UriResolver);
AssembleSets = checkouts
.Where(c => !c.Repository.Skip)
.Select(c => new AssemblerDocumentationSet(NullLoggerFactory.Instance, assembleContext, c, crossLinkResolver, TreeCollector))
.ToDictionary(s => s.Checkout.Repository.Name, s => s)
.ToFrozenDictionary();
TocConfigurationMapping = TocTopLevelMappings
.Select(kv =>
{
var repo = kv.Value.Source.Scheme;
if (!AssembleSets.TryGetValue(repo, out var set))
throw new Exception($"Unable to find repository: {repo}");
var fs = set.BuildContext.ReadFileSystem;
var config = set.BuildContext.Configuration;
var tocDirectory = Path.Combine(config.ScopeDirectory.FullName, kv.Value.Source.Host, kv.Value.Source.AbsolutePath.TrimStart('/'));
var relative = Path.GetRelativePath(config.ScopeDirectory.FullName, tocDirectory);
IFileInfo[] tocFiles =
[
fs.FileInfo.New(Path.Combine(tocDirectory, "toc.yml")),
fs.FileInfo.New(Path.Combine(tocDirectory, "_toc.yml")),
fs.FileInfo.New(Path.Combine(tocDirectory, "docset.yml")),
fs.FileInfo.New(Path.Combine(tocDirectory, "_docset.yml"))
];
var file = tocFiles.FirstOrDefault(f => f.Exists);
if (file is null)
{
assembleContext.Collector.EmitWarning(assembleContext.ConfigurationPath.FullName, $"Unable to find toc file in {tocDirectory}");
file = tocFiles.First();
}
var toc = new TableOfContentsConfiguration(config, file, fs.DirectoryInfo.New(tocDirectory), set.BuildContext, 0, relative);
var mapping = new TocConfigurationMapping
{
TopLevel = kv.Value,
RepositoryConfigurationFile = config,
TableOfContentsConfiguration = toc
};
return new KeyValuePair<Uri, TocConfigurationMapping>(kv.Value.Source, mapping);
})
.ToFrozenDictionary();
}
public static FrozenDictionary<Uri, TocTopLevelMapping> GetConfiguredSources(AssembleContext context)
{
var dictionary = new Dictionary<Uri, TocTopLevelMapping>();
var reader = new YamlStreamReader(context.NavigationPath, context.Collector);
var entries = new List<KeyValuePair<Uri, TocTopLevelMapping>>();
foreach (var entry in reader.Read())
{
switch (entry.Key)
{
case "toc":
ReadTocBlocks(entries, reader, entry.Entry, null, 0, null, null);
break;
}
}
foreach (var (source, block) in entries)
dictionary[source] = block;
return dictionary.ToFrozenDictionary();
static void ReadTocBlocks(
List<KeyValuePair<Uri, TocTopLevelMapping>> entries,
YamlStreamReader reader,
KeyValuePair<YamlNode, YamlNode> entry,
string? parent,
int depth,
Uri? topLevelSource,
Uri? parentSource
)
{
if (entry.Key is not YamlScalarNode { Value: not null } scalarKey)
{
reader.EmitWarning($"key '{entry.Key}' is not string");
return;
}
if (entry.Value is not YamlSequenceNode sequence)
{
reader.EmitWarning($"'{scalarKey.Value}' is not an array");
return;
}
var i = 0;
foreach (var tocEntry in sequence.Children.OfType<YamlMappingNode>())
{
ReadBlock(entries, reader, tocEntry, parent, depth, i, topLevelSource, parentSource);
i++;
}
}
static void ReadBlock(
List<KeyValuePair<Uri, TocTopLevelMapping>> entries,
YamlStreamReader reader,
YamlMappingNode tocEntry,
string? parent,
int depth,
int order,
Uri? topLevelSource,
Uri? parentSource
)
{
string? repository = null;
string? source = null;
string? pathPrefix = null;
foreach (var entry in tocEntry.Children)
{
var key = ((YamlScalarNode)entry.Key).Value;
switch (key)
{
case "toc":
source = reader.ReadString(entry);
if (source.AsSpan().IndexOf("://") == -1)
{
parent = source;
pathPrefix = source;
source = ContentSourceMoniker.CreateString(NarrativeRepository.RepositoryName, source);
}
break;
case "repo":
repository = reader.ReadString(entry);
break;
case "path_prefix":
pathPrefix = reader.ReadString(entry);
break;
}
}
if (repository is not null)
{
if (source is not null)
reader.EmitError($"toc config defines 'repo' can not be combined with 'toc': {source}", tocEntry);
pathPrefix = string.Join("/", [parent, repository]);
source = ContentSourceMoniker.CreateString(repository, parent);
}
if (source is null)
return;
if (!Uri.TryCreate(source.TrimEnd('/') + '/', UriKind.Absolute, out var sourceUri))
{
reader.EmitError($"Source toc entry is not a valid uri: {source}", tocEntry);
return;
}
var sourcePrefix = $"{sourceUri.Host}/{sourceUri.AbsolutePath.TrimStart('/')}";
if (string.IsNullOrEmpty(pathPrefix))
reader.EmitError($"Path prefix is not defined for: {source}, falling back to {sourcePrefix} which may be incorrect", tocEntry);
pathPrefix ??= sourcePrefix;
topLevelSource ??= sourceUri;
parentSource ??= sourceUri;
var tocTopLevelMapping = new TocTopLevelMapping
{
Source = sourceUri,
SourcePathPrefix = pathPrefix,
TopLevelSource = topLevelSource,
ParentSource = parentSource
};
entries.Add(new KeyValuePair<Uri, TocTopLevelMapping>(sourceUri, tocTopLevelMapping));
foreach (var entry in tocEntry.Children)
{
var key = ((YamlScalarNode)entry.Key).Value;
switch (key)
{
case "children":
if (source is null && pathPrefix is null)
{
reader.EmitWarning("toc entry has no toc or path_prefix defined");
continue;
}
ReadTocBlocks(entries, reader, entry, parent, depth + 1, topLevelSource, tocTopLevelMapping.Source);
break;
}
}
}
}
}