-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathConfigurationFileProvider.cs
More file actions
314 lines (268 loc) · 12.2 KB
/
Copy pathConfigurationFileProvider.cs
File metadata and controls
314 lines (268 loc) · 12.2 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
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
// 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.IO.Abstractions;
using System.Text.RegularExpressions;
using Elastic.Documentation.Configuration.Assembler;
using Elastic.Documentation.Configuration.Converters;
using Elastic.Documentation.Configuration.Serialization;
using Elastic.Documentation.Configuration.Toc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Elastic.Documentation.Configuration;
public partial class ConfigurationFileProvider
{
private readonly IFileSystem _fileSystem;
private readonly string _assemblyName;
private readonly ILogger<ConfigurationFileProvider> _logger;
public static IDeserializer Deserializer { get; } = new StaticDeserializerBuilder(new YamlStaticContext())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new HintTypeSetConverter())
.WithTypeConverter(new TocItemCollectionYamlConverter())
.WithTypeConverter(new TocItemYamlConverter())
.WithTypeConverter(new SiteTableOfContentsCollectionYamlConverter())
.WithTypeConverter(new SiteTableOfContentsRefYamlConverter())
.WithTypeConverter(new ApiConfigurationConverter())
.Build();
public static IDeserializer NavV2Deserializer { get; } = new StaticDeserializerBuilder(new YamlStaticContext())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new NavV2FileYamlConverter())
.Build();
public ConfigurationSource ConfigurationSource { get; }
public string? GitReference { get; }
public ConfigurationFileProvider(
ILoggerFactory logFactory,
IFileSystem fileSystem,
bool skipPrivateRepositories = false,
ConfigurationSource? configurationSource = null
)
{
_logger = logFactory.CreateLogger<ConfigurationFileProvider>();
_fileSystem = fileSystem;
_assemblyName = typeof(ConfigurationFileProvider).Assembly.GetName().Name!;
SkipPrivateRepositories = skipPrivateRepositories;
// Use a unique subdirectory per instance to avoid file-locking collisions when
// multiple processes or parallel tests share the same ApplicationData path.
var configRuntimeDir = Path.Join(Paths.ApplicationData.FullName, "config-runtime", Guid.NewGuid().ToString("N"));
TemporaryDirectory = fileSystem.DirectoryInfo.New(configRuntimeDir);
TemporaryDirectory.Create();
// TODO: This doesn't work as expected if a github actions consumer repo has a `config` directory.
// ConfigurationSource = configurationSource ?? (
// fileSystem.Directory.Exists(LocalConfigurationDirectory)
// ? ConfigurationSource.Local : ConfigurationSource.Embedded
// );
if (configurationSource is { } source)
ConfigurationSource = source;
else
{
string[] spotChecks = ["navigation.yml", "versions.yml", "products.yml", "assembler.yml", "search.yml"];
var defaultSource =
fileSystem.Directory.Exists(LocalConfigurationDirectory)
&& spotChecks.All(f => fileSystem.File.Exists(Path.Join(LocalConfigurationDirectory, f)))
? ConfigurationSource.Local
: ConfigurationSource.Embedded;
ConfigurationSource = defaultSource;
}
if (ConfigurationSource == ConfigurationSource.Local && !fileSystem.Directory.Exists(LocalConfigurationDirectory))
throw new Exception($"Required directory form {nameof(ConfigurationSource)}.{nameof(ConfigurationSource.Local)} directory {LocalConfigurationDirectory} does not exist.");
if (ConfigurationSource == ConfigurationSource.Remote && !fileSystem.Directory.Exists(AppDataConfigurationDirectory))
throw new Exception($"Required directory form {nameof(ConfigurationSource)}.{nameof(ConfigurationSource.Remote)} directory {AppDataConfigurationDirectory} does not exist.");
var path = GetAppDataPath("git-ref.txt");
if (_fileSystem.File.Exists(path))
GitReference = _fileSystem.File.ReadAllText(path);
else if (ConfigurationSource == ConfigurationSource.Remote)
throw new Exception($"Can not read git-ref.txt in directory {LocalConfigurationDirectory}");
if (ConfigurationSource == ConfigurationSource.Remote)
{
_logger.LogInformation("{ConfigurationSource}: git ref '{GitReference}', in {Directory}",
$"{nameof(ConfigurationSource)}.{nameof(ConfigurationSource.Remote)}", GitReference, AppDataConfigurationDirectory);
}
if (ConfigurationSource == ConfigurationSource.Local)
{
_logger.LogInformation("{ConfigurationSource}: located {Directory}",
$"{nameof(ConfigurationSource)}.{nameof(ConfigurationSource.Local)}", LocalConfigurationDirectory);
}
if (ConfigurationSource == ConfigurationSource.Embedded)
{
_logger.LogInformation("{ConfigurationSource} using embedded in binary configuration",
$"{nameof(ConfigurationSource)}.{nameof(ConfigurationSource.Embedded)}");
}
VersionFile = CreateTemporaryConfigurationFile("versions.yml");
ProductsFile = CreateTemporaryConfigurationFile("products.yml");
AssemblerFile = CreateTemporaryConfigurationFile("assembler.yml");
NavigationFile = CreateTemporaryConfigurationFile("navigation.yml");
NavigationV2File = TryCreateTemporaryConfigurationFile("navigation-v2.yml");
LegacyUrlMappingsFile = CreateTemporaryConfigurationFile("legacy-url-mappings.yml");
// reading from synonyms.yml is temporary. If you spot this again as a future reader, feel free to remove it.
SearchFile = CreateTemporaryConfigurationFile("search.yml", "synonyms.yml");
}
public bool SkipPrivateRepositories { get; }
private IDirectoryInfo TemporaryDirectory { get; }
public IFileInfo NavigationFile { get; private set; }
/// <summary>Optional — present only when <c>config/navigation-v2.yml</c> exists.</summary>
public IFileInfo? NavigationV2File { get; }
public IFileInfo VersionFile { get; }
public IFileInfo ProductsFile { get; }
public IFileInfo AssemblerFile { get; }
public IFileInfo LegacyUrlMappingsFile { get; }
public IFileInfo SearchFile { get; }
public IFileInfo CreateNavigationFile(AssemblyConfiguration configuration)
{
var privateRepositories = configuration.PrivateRepositories;
if (privateRepositories.Count == 0 || !SkipPrivateRepositories)
return NavigationFile;
var targets = string.Join("|", privateRepositories.Keys);
var tempFile = Path.Join(TemporaryDirectory.FullName, "navigation.filtered.yml");
if (_fileSystem.File.Exists(tempFile))
return NavigationFile;
_logger.LogInformation("Filtering navigation file to remove private repositories");
// This routine removes `toc: `'s linking to private repositories and reindents any later lines if needed.
// This will make any public children in the nav move up one place.
var spacing = -1;
var reindenting = -1;
foreach (var l in _fileSystem.File.ReadAllLines(NavigationFile.FullName))
{
var line = l;
if (spacing > -1 && !string.IsNullOrWhiteSpace(line) && !line.StartsWith(new string(' ', spacing), StringComparison.Ordinal))
{
spacing = -1;
reindenting = -1;
}
if (spacing != -1 && Regex.IsMatch(line, $@"^\s{{{spacing}}}\S"))
{
spacing = -1;
reindenting = -1;
}
else if (spacing != -1 && Regex.IsMatch(line, $@"^(\s{{{spacing + 3},}})\S"))
{
var matches = Regex.Match(line, $@"^(?<spacing>\s{{{spacing}}})(?<remainder>.+)$");
line = $"{new string(' ', Math.Max(0, spacing - 4))}{matches.Groups["remainder"].Value}";
reindenting = spacing;
}
if (spacing == -1 && TocPrefixRegex().IsMatch(line))
{
var matches = Regex.Match(line, $@"^(?<spacing>\s+)-\s?toc:\s?(?:{targets})\:");
if (matches.Success)
spacing = matches.Groups["spacing"].Value.Length;
if (spacing == 0)
spacing = -1;
}
if (spacing == -1 || reindenting > 0)
_fileSystem.File.AppendAllLines(tempFile, [line]);
}
if (configuration.AvailableRepositories.TryGetValue("docs-builder", out var docsBuildRepository) && docsBuildRepository is { Skip: false, Path: not null })
{
// language=yaml
_fileSystem.File.AppendAllText(tempFile,
"""
- toc: docs-builder://
path_prefix: reference/docs-builder
children:
- toc: docs-builder://development
path_prefix: reference/docs-builder/dev
children:
- toc: docs-builder://development/link-validation
path_prefix: reference/docs-builder/dev/link-val
""");
}
NavigationFile = _fileSystem.FileInfo.New(tempFile);
return NavigationFile;
}
private IFileInfo? TryCreateTemporaryConfigurationFile(string fileName)
{
if (ConfigurationSource == ConfigurationSource.Local)
{
var localPath = GetLocalPath(fileName);
if (!_fileSystem.File.Exists(localPath))
return null;
}
else if (ConfigurationSource == ConfigurationSource.Remote)
{
var appDataPath = GetAppDataPath(fileName);
if (!_fileSystem.File.Exists(appDataPath))
return null;
}
else
{
// Embedded — check if resource exists
var resourceName = $"{_assemblyName}.{fileName}";
if (typeof(ConfigurationFileProvider).Assembly.GetManifestResourceStream(resourceName) is null)
return null;
}
return CreateTemporaryConfigurationFile(fileName);
}
private IFileInfo CreateTemporaryConfigurationFile(string fileName, string? fallback = null)
{
using var stream = GetLocalOrEmbedded(fileName, fallback);
var context = stream.ReadToEnd();
var fi = _fileSystem.FileInfo.New(Path.Join(TemporaryDirectory.FullName, fileName));
_fileSystem.File.WriteAllText(fi.FullName, context);
return fi;
}
private StreamReader GetLocalOrEmbedded(string fileName, string? fallback = null)
{
var localPath = GetLocalPath(fileName);
if (ConfigurationSource == ConfigurationSource.Local)
{
if (_fileSystem.File.Exists(localPath))
{
var reader = _fileSystem.File.OpenText(localPath);
return reader;
}
var fallbackPath = fallback is not null ? GetLocalPath(fallback) : null;
if (fallbackPath is not null && _fileSystem.File.Exists(fallbackPath))
{
var reader = _fileSystem.File.OpenText(fallbackPath);
return reader;
}
throw new Exception($"Can not read {fileName} in directory {LocalConfigurationDirectory}");
}
var appDataPath = GetAppDataPath(fileName);
if (ConfigurationSource == ConfigurationSource.Remote)
{
if (_fileSystem.File.Exists(appDataPath))
{
var reader = _fileSystem.File.OpenText(appDataPath);
return reader;
}
var fallbackPath = fallback is not null ? GetAppDataPath(fallback) : null;
if (fallbackPath is not null && _fileSystem.File.Exists(fallbackPath))
{
var reader = _fileSystem.File.OpenText(fallbackPath);
return reader;
}
throw new Exception($"Can not read {fileName} in directory {AppDataConfigurationDirectory}");
}
return GetEmbeddedStream(fileName);
}
private StreamReader GetEmbeddedStream(string fileName)
{
var resourceName = $"{_assemblyName}.{fileName}";
var resourceStream = typeof(ConfigurationFileProvider).Assembly.GetManifestResourceStream(resourceName)!;
var reader = new StreamReader(resourceStream, leaveOpen: false);
return reader;
}
public static string AppDataConfigurationDirectory { get; } = Path.Join(Paths.ApplicationData.FullName, "config-clone", "config");
public static string LocalConfigurationDirectory { get; } = Path.Join(Directory.GetCurrentDirectory(), "config");
private static string GetLocalPath(string file) => Path.Join(LocalConfigurationDirectory, file);
private static string GetAppDataPath(string file) => Path.Join(AppDataConfigurationDirectory, file);
[GeneratedRegex(@"^\s+-?\s?toc:\s?")]
private static partial Regex TocPrefixRegex();
}
public static class ConfigurationFileProviderServiceCollectionExtensions
{
public static IServiceCollection AddConfigurationFileProvider(this IServiceCollection services,
bool skipPrivateRepositories,
ConfigurationSource? configurationSource,
Action<IServiceCollection, ConfigurationFileProvider> configure)
{
using var sp = services.BuildServiceProvider();
var logFactory = sp.GetRequiredService<ILoggerFactory>();
var provider = new ConfigurationFileProvider(logFactory, FileSystemFactory.RealRead, skipPrivateRepositories, configurationSource);
_ = services.AddSingleton(provider);
configure(services, provider);
return services;
}
}