Skip to content

Commit

Permalink
manually include livereload extension to satisfy AOT trimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Dec 26, 2024
1 parent dc30694 commit 60b39cb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
12 changes: 5 additions & 7 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ This repository is host to:
* *`docs-builder`* command line tool to generate single doc-sets (13mb native code, no dependencies)
* *`docs-assembler`* command line tool to assemble all the doc sets. (IN PROGRESS)
* `elastic/docs-builder@main` Github Action to build and validate a repositories documentation
* *`docs-generator`* command line tool to deterministically generate a docset and incremental updates to generated content

## Command line interface

```
```bash
$ docs-builder --help
Usage: [command] [options...] [-h|--help] [--version]

Expand Down Expand Up @@ -130,16 +131,13 @@ https://github.com/elastic/{your-repository}/settings/pages
## Run without docker

If you have dotnet 8 installed you can use its CLI to publish a self-contained `docs-builder` native code
binary. (On my M2 Pro mac the binary is currently 13mb)
binary. (On my M2 Pro mac the binary is currently 16mb)

```bash
$ dotnet publish "src/docs-builder/docs-builder.csproj" -c Release -o .artifacts/publish \
--self-contained true /p:PublishTrimmed=true /p:PublishSingleFile=false /p:PublishAot=true -a arm64
$ dotnet publish "src/docs-builder/docs-builder.csproj"
```

**Note**: `-a` should be the machines CPU architecture

The resulting binary `./.artifacts/publish/docs-builder` will run on machines without .NET installed
The resulting binary `./.artifacts/publish/docs-builder/release/docs-builder` will run on machines without .NET installed

# Performance

Expand Down
6 changes: 2 additions & 4 deletions src/docs-builder/ConsoleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
// ReSharper disable once CheckNamespace
namespace ConsoleAppFramework;

[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode",
Justification = "Everything referenced in the loaded assembly is manually preserved, so it's safe")]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresUnreferencedCode",
Justification = "Everything referenced in the loaded assembly is manually preserved, so it's safe")]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "Manually verified")]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresDynamicCode", Justification = "Manually verified")]
internal static partial class ConsoleApp;

2 changes: 1 addition & 1 deletion src/docs-builder/Http/DocumentationWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DocumentationWebHost(string? path, ILoggerFactory logger, IFileSystem fil
{
Collector = new ConsoleDiagnosticsCollector(logger)
};
builder.Services.AddLiveReload(s =>
builder.Services.AddAotLiveReload(s =>
{
s.FolderToMonitor = context.SourcePath.FullName;
s.ClientFileExtensions = ".md,.yml";
Expand Down
57 changes: 57 additions & 0 deletions src/docs-builder/Http/LiveReload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Westwind.AspNetCore.LiveReload;

[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "Manually verified")]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresDynamicCode", Justification = "Manually verified")]
public static class LiveReloadMiddlewareExtensions
{

public static IServiceCollection AddAotLiveReload(this IServiceCollection services,
Action<LiveReloadConfiguration> configAction)
{

var provider = services.BuildServiceProvider();
var configuration = provider.GetService<IConfiguration>();

var config = new LiveReloadConfiguration();
configuration!.Bind("LiveReload", config);

LiveReloadConfiguration.Current = config;

if (config.LiveReloadEnabled)
{
var env = provider.GetService<IWebHostEnvironment>();
if (string.IsNullOrEmpty(config.FolderToMonitor))
{
config.FolderToMonitor = env!.ContentRootPath;
}
else if (config.FolderToMonitor.StartsWith("~"))
{
if (config.FolderToMonitor.Length > 1)
{
var folder = config.FolderToMonitor.Substring(1);
if (folder.StartsWith('/') || folder.StartsWith("\\"))
folder = folder.Substring(1);
config.FolderToMonitor = Path.Combine(env!.ContentRootPath, folder);
config.FolderToMonitor = Path.GetFullPath(config.FolderToMonitor);
}
else
config.FolderToMonitor = env!.ContentRootPath;
}

configAction.Invoke(config);

LiveReloadConfiguration.Current = config;
}

return services;
}
}
5 changes: 3 additions & 2 deletions src/docs-builder/docs-builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
<AssemblyName>docs-builder</AssemblyName>
<RootNamespace>Documentation.Builder</RootNamespace>
<InvariantGlobalization>true</InvariantGlobalization>

<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<PublishAot>true</PublishAot>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>

<IsPublishable>true</IsPublishable>
<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
Expand All @@ -25,7 +27,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0"/>
<PackageReference Include="Github.Actions.Core" Version="8.1.1"/>
<PackageReference Include="Westwind.AspNetCore.LiveReload" Version="0.5.0" />
<PackageReference Include="Chorn.EmbeddedResourceAccessGenerator" Version="1.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 60b39cb

Please sign in to comment.