Skip to content

Commit 6cf2032

Browse files
committed
Centralize all configuration files under /config
1 parent 47157a3 commit 6cf2032

31 files changed

+266
-131
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageVersion Include="AWSSDK.Core" Version="4.0.0.2" />
1717
<PackageVersion Include="AWSSDK.SQS" Version="4.0.0.1" />
1818
<PackageVersion Include="AWSSDK.S3" Version="4.0.0.1" />
19+
<PackageVersion Include="Chorn.EmbeddedResourceAccessGenerator" Version="1.1.5" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
1920
<PackageVersion Include="FakeItEasy" Version="8.3.0" />
2021
<PackageVersion Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
2122
<PackageVersion Include="Microsoft.OpenApi" Version="2.0.0-preview9" />
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs-builder.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Legac
111111
EndProject
112112
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.LegacyDocs.Tests", "tests\Elastic.Documentation.LegacyDocs.Tests\Elastic.Documentation.LegacyDocs.Tests.csproj", "{164F55EC-9412-4CD4-81AD-3598B57632A6}"
113113
EndProject
114+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{6FAB566B-76E3-4E90-8A5C-E75B0F1E1C63}"
115+
ProjectSection(SolutionItems) = preProject
116+
config\versions.yml = config\versions.yml
117+
config\assembler.yml = config\assembler.yml
118+
config\legacy-url-mappings.yml = config\legacy-url-mappings.yml
119+
config\navigation.yml = config\navigation.yml
120+
EndProjectSection
121+
EndProject
114122
Global
115123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
116124
Debug|Any CPU = Debug|Any CPU

src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Text.RegularExpressions;
6+
using Elastic.Documentation.Extensions;
67
using YamlDotNet.Serialization;
78
using YamlStaticContext = Elastic.Documentation.Configuration.Serialization.YamlStaticContext;
89

910
namespace Elastic.Documentation.Configuration.Assembler;
1011

1112
public record AssemblyConfiguration
1213
{
14+
public static AssemblyConfiguration Create(ConfigurationFileProvider provider) =>
15+
Deserialize(provider.AssemblerFile.ReadToEnd());
16+
1317
public static AssemblyConfiguration Deserialize(string yaml)
1418
{
1519
var input = new StringReader(yaml);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.IO.Abstractions;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace Elastic.Documentation.Configuration;
9+
10+
public class ConfigurationFileProvider
11+
{
12+
private readonly IFileSystem _fileSystem;
13+
14+
public ConfigurationFileProvider(IFileSystem fileSystem)
15+
{
16+
_fileSystem = fileSystem;
17+
TemporaryDirectory = fileSystem.Directory.CreateTempSubdirectory("docs-builder-config");
18+
19+
VersionFile = CreateTemporaryConfigurationFile(EmbeddedResource.______config_versions_yml);
20+
AssemblerFile = CreateTemporaryConfigurationFile(EmbeddedResource.______config_assembler_yml);
21+
NavigationFile = CreateTemporaryConfigurationFile(EmbeddedResource.______config_navigation_yml);
22+
LegacyUrlMappingsFile = CreateTemporaryConfigurationFile(EmbeddedResource.______config_legacy_url_mappings_yml);
23+
}
24+
25+
private IDirectoryInfo TemporaryDirectory { get; }
26+
27+
public IFileInfo NavigationFile { get; }
28+
29+
public IFileInfo VersionFile { get; }
30+
31+
public IFileInfo AssemblerFile { get; }
32+
33+
public IFileInfo LegacyUrlMappingsFile { get; }
34+
35+
private IFileInfo CreateTemporaryConfigurationFile(EmbeddedResource resource)
36+
{
37+
var fileName = resource.GetResourceName().Split('_').Last();
38+
using var stream = GetLocalOrEmbedded(resource);
39+
var context = stream.ReadToEnd();
40+
var fi = _fileSystem.FileInfo.New(Path.Combine(TemporaryDirectory.FullName, fileName));
41+
_fileSystem.File.WriteAllText(fi.FullName, context);
42+
return fi;
43+
}
44+
45+
private StreamReader GetLocalOrEmbedded(EmbeddedResource resource)
46+
{
47+
var fileName = resource.GetResourceName().Split('_').Last();
48+
var configPath = GetLocalPath(fileName);
49+
if (!_fileSystem.File.Exists(configPath))
50+
return GetEmbeddedStream(resource);
51+
var stream = resource.GetStream();
52+
var reader = new StreamReader(stream, leaveOpen: false);
53+
return reader;
54+
}
55+
56+
private static StreamReader GetEmbeddedStream(EmbeddedResource resource)
57+
{
58+
var stream = resource.GetStream();
59+
var reader = new StreamReader(stream, leaveOpen: false);
60+
return reader;
61+
}
62+
63+
public static string LocalConfigurationDirectory => Path.Combine(Paths.WorkingDirectoryRoot.FullName, "config");
64+
65+
private static string GetLocalPath(string file) => Path.Combine(LocalConfigurationDirectory, file);
66+
}
67+
68+
public static class ConfigurationFileProviderServiceCollectionExtensions
69+
{
70+
public static IServiceCollection AddConfigurationFileProvider(this IServiceCollection services,
71+
Action<IServiceCollection, ConfigurationFileProvider> configure)
72+
{
73+
var provider = new ConfigurationFileProvider(new FileSystem());
74+
_ = services.AddSingleton(provider);
75+
configure(services, provider);
76+
return services;
77+
}
78+
}

src/Elastic.Documentation.Configuration/Elastic.Documentation.Configuration.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Chorn.EmbeddedResourceAccessGenerator" />
1415
<PackageReference Include="DotNet.Glob" />
1516
<PackageReference Include="Samboy063.Tomlet" />
1617
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator"/>
1718
<PackageReference Include="YamlDotNet"/>
1819
</ItemGroup>
1920

2021
<ItemGroup>
21-
<EmbeddedResource Include="versions.yml" />
22+
<EmbeddedResource Include="$(SolutionRoot)\config\versions.yml" />
23+
<EmbeddedResource Include="$(SolutionRoot)\config\assembler.yml" />
24+
<EmbeddedResource Include="$(SolutionRoot)\config\navigation.yml" />
25+
<EmbeddedResource Include="$(SolutionRoot)\config\legacy-url-mappings.yml" />
2226
</ItemGroup>
2327
</Project>

0 commit comments

Comments
 (0)