Skip to content

Commit 2556a3e

Browse files
committed
Add ability to set feature flags from assembler config
1 parent c7be45e commit 2556a3e

File tree

9 files changed

+72
-35
lines changed

9 files changed

+72
-35
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ public record PublishEnvironment
2525

2626
[YamlMember(Alias = "google_tag_manager")]
2727
public GoogleTagManager GoogleTagManager { get; set; } = new();
28+
29+
[YamlMember(Alias = "feature_flags")]
30+
public Dictionary<string, bool> FeatureFlags { get; set; } = [];
2831
}

src/Elastic.Documentation.Configuration/Builder/FeatureFlags.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,28 @@
44

55
namespace Elastic.Documentation.Configuration.Builder;
66

7-
public class FeatureFlags(Dictionary<string, bool> featureFlags)
7+
public class FeatureFlags(Dictionary<string, bool> initFeatureFlags)
88
{
9-
public bool IsPrimaryNavEnabled => IsEnabled("primary-nav");
10-
public bool IsVersionDropdownEnabled => IsEnabled("version-dropdown");
9+
private readonly Dictionary<string, bool> _featureFlags = new(initFeatureFlags);
10+
11+
public void Set(string key, bool value)
12+
{
13+
var normalizedKey = key.ToLowerInvariant().Replace('_', '-');
14+
_featureFlags[normalizedKey] = value;
15+
}
16+
17+
public bool PrimaryNavEnabled
18+
{
19+
get => IsEnabled("primary-nav");
20+
set => _featureFlags["primary-nav"] = value;
21+
}
22+
23+
public bool VersionDropdownEnabled
24+
{
25+
get => IsEnabled("version-dropdown");
26+
set => _featureFlags["version-dropdown"] = value;
27+
}
28+
1129
private bool IsEnabled(string key)
1230
{
1331
var envKey = $"FEATURE_{key.ToUpperInvariant().Replace('-', '_')}";
@@ -18,6 +36,7 @@ private bool IsEnabled(string key)
1836
// if the env var is set but not a bool, we treat it as enabled
1937
return true;
2038
}
21-
return featureFlags.TryGetValue(key, out var value) && value;
39+
40+
return _featureFlags.TryGetValue(key, out var value) && value;
2241
}
2342
}

src/Elastic.Documentation.Site/Layout/_Footer.cshtml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33

44
<footer class="py-12 px-6 w-full mx-auto bg-ink-dark">
55
<div class="max-w-(--max-layout-width) w-full mx-auto">
6-
<div class="text-grey-20 text-[12px]">
7-
<a href="https://www.elastic.co/">
8-
<img class="block" alt="Elastic logo" src="@Model.Static("logo-elastic-horizontal-white.svg")" width="120"/>
9-
</a>
10-
</div>
11-
<div class="text-grey-20 text-[12px] bg-ink-dark grid grid-cols-1 md:grid-cols-2 gap-2 items-center">
12-
<div>
13-
<ul class="mt-4 flex gap-3">
14-
<li>
15-
<a class="underline hover:text-white" href="https://www.elastic.co/legal/trademarks">Trademarks</a>
16-
</li>
17-
<li>
18-
<a class="underline hover:text-white" href="https://www.elastic.co/legal/terms-of-use">Terms of Use</a>
19-
</li>
20-
<li>
21-
<a class="underline hover:text-white" href="https://www.elastic.co/legal/privacy-statement">Privacy</a>
22-
</li>
23-
<li>
24-
<a class="underline hover:text-white" href="https://www.elastic.co/sitemap">Sitemap</a>
25-
</li>
26-
</ul>
6+
<div class="text-grey-20 text-[12px]">
7+
<a href="https://www.elastic.co/">
8+
<img class="block" alt="Elastic logo" src="@Model.Static("logo-elastic-horizontal-white.svg")" width="120"/>
9+
</a>
10+
</div>
11+
<div class="text-grey-20 text-[12px] bg-ink-dark grid grid-cols-1 md:grid-cols-2 gap-2 items-center">
12+
<div>
13+
<ul class="mt-4 flex gap-3">
14+
<li>
15+
<a class="underline hover:text-white" href="https://www.elastic.co/legal/trademarks">Trademarks</a>
16+
</li>
17+
<li>
18+
<a class="underline hover:text-white" href="https://www.elastic.co/legal/terms-of-use">Terms of Use</a>
19+
</li>
20+
<li>
21+
<a class="underline hover:text-white" href="https://www.elastic.co/legal/privacy-statement">Privacy</a>
22+
</li>
23+
<li>
24+
<a class="underline hover:text-white" href="https://www.elastic.co/sitemap">Sitemap</a>
25+
</li>
26+
</ul>
27+
<p class="mt-4">
28+
© @(DateTime.Today.Year) Elasticsearch B.V. All Rights Reserved.
29+
</p>
30+
</div>
2731
<p class="mt-4">
28-
© @(DateTime.Today.Year) Elasticsearch B.V. All Rights Reserved.
32+
Elasticsearch is a trademark of Elasticsearch B.V., registered in the U.S. and in other countries. Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant logo are trademarks of the Apache Software Foundation in the United States and/or other countries.
2933
</p>
3034
</div>
31-
<p class="mt-4">
32-
Elasticsearch is a trademark of Elasticsearch B.V., registered in the U.S. and in other countries. Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant logo are trademarks of the Apache Software Foundation in the United States and/or other countries.
33-
</p>
34-
</div>
3535
</div>
3636
</footer>
3737

src/Elastic.Documentation.Site/Layout/_Header.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@inherits RazorSlice<Elastic.Documentation.Site.GlobalLayoutViewModel>
2-
@if (Model.Features.IsPrimaryNavEnabled)
2+
@if (Model.Features.PrimaryNavEnabled)
33
{
44
<div id="elastic-nav"></div>
55
<script src="https://www.elastic.co/elastic-nav.js"></script>

src/Elastic.Documentation.Site/Navigation/IsolatedBuildNavigationHtmlWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class IsolatedBuildNavigationHtmlWriter(BuildContext context, INodeNaviga
1414

1515
public async Task<string> RenderNavigation(INodeNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, Uri navigationSource, Cancel ctx = default)
1616
{
17-
var navigation = context.Configuration.Features.IsPrimaryNavEnabled
17+
var navigation = context.Configuration.Features.PrimaryNavEnabled
1818
? currentRootNavigation
1919
: siteRoot;
2020

@@ -33,7 +33,7 @@ private NavigationViewModel CreateNavigationModel(INodeNavigationItem<INavigatio
3333
Title = navigation.NavigationTitle,
3434
TitleUrl = navigation.Url,
3535
Tree = navigation,
36-
IsPrimaryNavEnabled = context.Configuration.Features.IsPrimaryNavEnabled,
36+
IsPrimaryNavEnabled = context.Configuration.Features.PrimaryNavEnabled,
3737
IsGlobalAssemblyBuild = false,
3838
TopLevelItems = siteRoot.NavigationItems.OfType<INodeNavigationItem<INavigationModel, INavigationItem>>().ToList()
3939
};

src/Elastic.Markdown/Slices/Layout/_Breadcrumbs.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@inherits RazorSlice<MarkdownLayoutViewModel>
22
@{
33
var targets =
4-
Model.Features.IsPrimaryNavEnabled
4+
Model.Features.PrimaryNavEnabled
55
? Model.Parents.Reverse()
66
: Model.Parents.Reverse().Skip(1);
77

src/Elastic.Markdown/Slices/Layout/_TableOfContents.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@inherits RazorSlice<MarkdownLayoutViewModel>
22
<aside class="sidebar hidden lg:block max-w-65 md:hidden">
33
<nav id="toc-nav" class="sidebar-nav h-full">
4-
@if (Model.Features.IsVersionDropdownEnabled)
4+
@if (Model.Features.VersionDropdownEnabled)
55
{
66
<div class="mt-6">
77
<version-dropdown all-versions-url="@Model.AllVersionsUrl" current-version='@(Model.CurrentVersion)' items='@(new HtmlString(Model.VersionDropdownSerializedModel))' />

src/tooling/docs-assembler/Building/AssemblerBuilder.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class AssemblerBuilder(
3030
ILegacyUrlMapper? legacyUrlMapper
3131
)
3232
{
33+
private readonly ILogger<AssemblerBuilder> _logger = logger.CreateLogger<AssemblerBuilder>();
34+
3335
private GlobalNavigationHtmlWriter HtmlWriter { get; } = writer;
3436

3537
private ILegacyUrlMapper? LegacyUrlMapper { get; } = legacyUrlMapper;
@@ -118,6 +120,7 @@ string Resolve(string relativeMarkdownPath)
118120

119121
private async Task<GenerationResult> BuildAsync(AssemblerDocumentationSet set, bool noop, IMarkdownExporter[]? markdownExporters, Cancel ctx)
120122
{
123+
SetFeatureFlags(set);
121124
var generator = new DocumentationGenerator(
122125
set.DocumentationSet,
123126
logger, HtmlWriter,
@@ -130,4 +133,14 @@ private async Task<GenerationResult> BuildAsync(AssemblerDocumentationSet set, b
130133
return await generator.GenerateAll(ctx);
131134
}
132135

136+
private void SetFeatureFlags(AssemblerDocumentationSet set)
137+
{
138+
// Enable primary nav by default
139+
set.DocumentationSet.Configuration.Features.PrimaryNavEnabled = true;
140+
foreach (var configurationFeatureFlag in set.AssembleContext.Environment.FeatureFlags)
141+
{
142+
_logger.LogInformation("Setting feature flag: {ConfigurationFeatureFlagKey}={ConfigurationFeatureFlagValue}", configurationFeatureFlag.Key, configurationFeatureFlag.Value);
143+
set.DocumentationSet.Configuration.Features.Set(configurationFeatureFlag.Key, configurationFeatureFlag.Value);
144+
}
145+
}
133146
}

src/tooling/docs-assembler/assembler.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ environments:
2323
content_source: next
2424
google_tag_manager:
2525
enabled: false
26+
feature_flags:
27+
VERSION_DROPDOWN: true
2628
dev:
2729
uri: http://localhost:4000
2830
content_source: next

0 commit comments

Comments
 (0)