Skip to content

Commit d379363

Browse files
committed
Move inference logic to a service and use it
1 parent d4fbf93 commit d379363

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

config/products.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,25 @@ products:
9191
versioning: 'stack'
9292
edot-ios:
9393
display: 'Elastic Distribution of OpenTelemetry iOS'
94+
repository: 'apm-agent-ios'
9495
edot-android:
9596
display: 'Elastic Distribution of OpenTelemetry Android'
97+
repository: 'apm-agent-android'
9698
edot-dotnet:
9799
display: 'Elastic Distribution of OpenTelemetry .NET'
100+
repository: 'elastic-otel-dotnet'
98101
edot-java:
99102
display: 'Elastic Distribution of OpenTelemetry Java'
103+
repository: 'elastic-otel-java'
100104
edot-node:
101105
display: 'Elastic Distribution of OpenTelemetry Node'
106+
repository: 'elastic-otel-node'
102107
edot-php:
103108
display: 'Elastic Distribution of OpenTelemetry PHP'
109+
repository: 'elastic-otel-php'
104110
edot-python:
105111
display: 'Elastic Distribution of OpenTelemetry Python'
112+
repository: 'elastic-otel-python'
106113
edot-cf-aws:
107114
display: 'EDOT Cloud Forwarder for AWS'
108115
edot-cf-azure:

src/Elastic.Documentation.Configuration/Products/Product.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public record Product
1919
public required string Id { get; init; }
2020
public required string DisplayName { get; init; }
2121
public VersioningSystem? VersioningSystem { get; init; }
22+
public string? Repository { get; init; }
2223
}
2324

src/Elastic.Documentation.Configuration/Products/ProductExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static ProductsConfiguration CreateProducts(this ConfigurationFileProvide
2121
{
2222
Id = kvp.Key,
2323
DisplayName = kvp.Value.Display,
24-
VersioningSystem = versionsConfiguration.GetVersioningSystem(VersionsConfigurationExtensions.ToVersioningSystemId(kvp.Value.Versioning ?? kvp.Key))
24+
VersioningSystem = versionsConfiguration.GetVersioningSystem(VersionsConfigurationExtensions.ToVersioningSystemId(kvp.Value.Versioning ?? kvp.Key)),
25+
Repository = kvp.Value.Repository
2526
});
2627

2728
return new ProductsConfiguration
@@ -41,4 +42,5 @@ internal sealed record ProductDto
4142
{
4243
public string Display { get; set; } = string.Empty;
4344
public string? Versioning { get; set; }
45+
public string? Repository { get; set; }
4446
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Elastic.Documentation.Configuration.LegacyUrlMappings;
6+
using Elastic.Documentation.Configuration.Products;
7+
8+
namespace Elastic.Documentation.Configuration.Versions;
9+
10+
public interface IVersionInferrerService
11+
{
12+
VersioningSystem InferVersion(IReadOnlyCollection<LegacyPageMapping>? legacyPages);
13+
}
14+
15+
public class ProductVersionInferrerService(ProductsConfiguration productsConfiguration, VersionsConfiguration versionsConfiguration, string repositoryName) : IVersionInferrerService
16+
{
17+
private ProductsConfiguration ProductsConfiguration { get; } = productsConfiguration;
18+
private VersionsConfiguration VersionsConfiguration { get; } = versionsConfiguration;
19+
private string RepositoryName { get; } = repositoryName;
20+
public VersioningSystem InferVersion(IReadOnlyCollection<LegacyPageMapping>? legacyPages)
21+
{
22+
var versioning = legacyPages is not null && legacyPages.Count > 0
23+
? legacyPages.ElementAt(0).Product.VersioningSystem! // If the page has a legacy page mapping, use the versioning system of the legacy page
24+
: ProductsConfiguration.Products.TryGetValue(RepositoryName, out var belonging)
25+
? belonging.VersioningSystem! //If the page's docset has a name with a direct product match, use the versioning system of the product
26+
: ProductsConfiguration.Products.Values.SingleOrDefault(p =>
27+
p.Repository is not null && p.Repository.Equals(RepositoryName, StringComparison.OrdinalIgnoreCase)) is { } repositoryMatch
28+
? repositoryMatch.VersioningSystem! // Verify if the page belongs to a repository linked to a product, and if so, use the versioning system of the product
29+
: VersionsConfiguration.VersioningSystems[VersioningSystemId.Stack]; // Fallback to the stack versioning system
30+
31+
return versioning;
32+
}
33+
}
34+
35+
public class NoopVersionInferrer : IVersionInferrerService
36+
{
37+
public VersioningSystem InferVersion(IReadOnlyCollection<LegacyPageMapping>? legacyPages) => new()
38+
{
39+
Id = VersioningSystemId.Stack,
40+
Base = new SemVersion(0, 0, 0),
41+
Current = new SemVersion(0, 0, 0)
42+
};
43+
}

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Elastic.Documentation;
88
using Elastic.Documentation.Configuration;
99
using Elastic.Documentation.Configuration.LegacyUrlMappings;
10+
using Elastic.Documentation.Configuration.Versions;
1011
using Elastic.Documentation.Links;
1112
using Elastic.Documentation.Links.CrossLinks;
1213
using Elastic.Documentation.Serialization;
@@ -72,7 +73,7 @@ public DocumentationGenerator(
7273
Context = docSet.Context;
7374
CrossLinkResolver = docSet.CrossLinkResolver;
7475
HtmlWriter = new HtmlWriter(DocumentationSet, _writeFileSystem, new DescriptionGenerator(), navigationHtmlWriter, legacyUrlMapper,
75-
positionalNavigation);
76+
positionalNavigation, new ProductVersionInferrerService(DocumentationSet.Context.ProductsConfiguration, DocumentationSet.Context.VersionsConfiguration, DocumentationSet.Context.Git.RepositoryName));
7677
_documentationFileExporter =
7778
docSet.Context.AvailableExporters.Contains(Exporter.Html)
7879
? docSet.EnabledExtensions.FirstOrDefault(e => e.FileExporter != null)?.FileExporter

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Elastic.Documentation;
88
using Elastic.Documentation.Configuration.LegacyUrlMappings;
99
using Elastic.Documentation.Configuration.Products;
10-
using Elastic.Documentation.Configuration.TableOfContents;
1110
using Elastic.Documentation.Configuration.Versions;
1211
using Elastic.Documentation.Site.FileProviders;
1312
using Elastic.Documentation.Site.Navigation;
@@ -26,7 +25,8 @@ public class HtmlWriter(
2625
IDescriptionGenerator descriptionGenerator,
2726
INavigationHtmlWriter? navigationHtmlWriter = null,
2827
ILegacyUrlMapper? legacyUrlMapper = null,
29-
IPositionalNavigation? positionalNavigation = null
28+
IPositionalNavigation? positionalNavigation = null,
29+
IVersionInferrerService? versionInferrerService = null
3030
)
3131
: IMarkdownStringRenderer
3232
{
@@ -39,6 +39,8 @@ public class HtmlWriter(
3939
private ILegacyUrlMapper LegacyUrlMapper { get; } = legacyUrlMapper ?? new NoopLegacyUrlMapper();
4040
private IPositionalNavigation PositionalNavigation { get; } = positionalNavigation ?? documentationSet;
4141

42+
private IVersionInferrerService VersionInferrerService { get; } = versionInferrerService ?? new NoopVersionInferrer();
43+
4244
/// <inheritdoc />
4345
public string Render(string markdown, IFileInfo? source)
4446
{
@@ -103,24 +105,7 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
103105
fullNavigationRenderResult
104106
);
105107

106-
// Acquiring the versioning system for the current page:
107-
// 1. If the page has a legacy page mapping, use the versioning system of the legacy page
108-
// 2. If the page's docset has a name with a direct product maatch, use the versioning system of the product
109-
// 3. If the page's docset has a table of contents entry with a direct product match, use the versioning system of the product
110-
// 4. Fallback to the stack versioning system
111-
VersioningSystem pageVersioning = null!;
112-
if (legacyPages is not null && legacyPages.Count > 0)
113-
pageVersioning = legacyPages.ElementAt(0).Product.VersioningSystem!;
114-
else if (DocumentationSet.Context.ProductsConfiguration.Products.TryGetValue(DocumentationSet.Name, out var belonging))
115-
pageVersioning = belonging.VersioningSystem!;
116-
else if (DocumentationSet.Configuration.TableOfContents.FirstOrDefault(t => t is TocReference tRef && !tRef.Source.LocalPath.Equals("/")) is TocReference tocRef)
117-
{
118-
var productFound = DocumentationSet.Context.ProductsConfiguration.Products.TryGetValue(tocRef.Source.LocalPath.Trim('/'), out var tocProduct);
119-
if (productFound && tocProduct is not null)
120-
pageVersioning = tocProduct.VersioningSystem!;
121-
}
122-
else
123-
pageVersioning = DocumentationSet.Context.VersionsConfiguration.VersioningSystems[VersioningSystemId.Stack];
108+
var pageVersioning = VersionInferrerService.InferVersion(legacyPages);
124109

125110
var currentBaseVersion = $"{pageVersioning.Base.Major}.{pageVersioning.Base.Minor}+";
126111

0 commit comments

Comments
 (0)