Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedd static resources in assembly #99

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions docs/source/_ext/rejoin.py

This file was deleted.

8 changes: 0 additions & 8 deletions docs/source/_templates/header.html

This file was deleted.

59 changes: 0 additions & 59 deletions docs/source/_templates/partials/globaltoc-above.html

This file was deleted.

8 changes: 0 additions & 8 deletions docs/source/_templates/versioning.html

This file was deleted.

22 changes: 22 additions & 0 deletions src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Markdown.IO;
Expand Down Expand Up @@ -110,6 +111,27 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
if (item % 1_000 == 0)
_logger.LogInformation($"Handled {handledItems} files");
});

var embeddedStaticFiles = Assembly.GetExecutingAssembly()
.GetManifestResourceNames()
.ToList();
foreach (var a in embeddedStaticFiles)
{
await using var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(a);
if (resourceStream == null)
continue;

var path = a.Replace("Elastic.Markdown.", "").Replace("_static.", "_static/");

var outputFile = OutputFile(path);
if (outputFile.Directory is { Exists: false })
outputFile.Directory.Create();
await using var stream = outputFile.OpenWrite();
await resourceStream.CopyToAsync(stream, ctx);
_logger.LogInformation($"Copied static embedded resource {path}");
}


Context.Collector.Channel.TryComplete();

await GenerateDocumentationState(ctx);
Expand Down
5 changes: 4 additions & 1 deletion src/Elastic.Markdown/Elastic.Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
<PackageReference Include="YamlDotNet" Version="16.1.3" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="_static/*.js" />
<EmbeddedResource Include="_static/*.css" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion src/Elastic.Markdown/IO/DocumentationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public DocumentationSet(BuildContext context)
".md" => CreateMarkDownFile(file, context),
_ => new StaticFile(file, SourcePath)
})

.ToList();

LastWrite = Files.Max(f => f.SourceFile.LastWriteTimeUtc);
Expand Down
9 changes: 9 additions & 0 deletions src/Elastic.Markdown/IO/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ private static DirectoryInfo RootDirectoryInfo()
}

public static readonly DirectoryInfo Root = RootDirectoryInfo();

/// Used in debug to locate static folder so we can change js/css files while the server is running
public static DirectoryInfo? GetSolutionDirectory()
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (directory != null && directory.GetFiles("*.sln").Length == 0)
directory = directory.Parent;
return directory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)

if (url.EndsWith(".md"))
link.Url = Path.ChangeExtension(url, ".html");
// rooted links might need the configured path prefix to properly link
var prefix = processor.GetBuildContext().UrlPathPrefix;
if (url.StartsWith("/") && !string.IsNullOrWhiteSpace(prefix))
link.Url = $"{prefix}/{link.Url}";

if (!string.IsNullOrEmpty(anchor))
link.Url += $"#{anchor}";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Documentation.Builder.Diagnostics;
using Documentation.Builder.Http;
using Elastic.Markdown;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO;
using Microsoft.Extensions.Logging;

Expand Down
54 changes: 51 additions & 3 deletions src/docs-builder/Http/DocumentationWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Westwind.AspNetCore.LiveReload;
using IFileInfo = Microsoft.Extensions.FileProviders.IFileInfo;

namespace Documentation.Builder.Http;

public class DocumentationWebHost
{
private readonly WebApplication _webApplication;

private readonly string _staticFilesDirectory =
Path.Combine(Paths.Root.FullName, "docs", "source", "_static");
private readonly string _staticFilesDirectory;

public DocumentationWebHost(string? path, ILoggerFactory logger, IFileSystem fileSystem)
{
Expand All @@ -40,6 +42,15 @@ public DocumentationWebHost(string? path, ILoggerFactory logger, IFileSystem fil
builder.Services.AddSingleton(logger);
builder.Logging.SetMinimumLevel(LogLevel.Warning);

_staticFilesDirectory = Path.Combine(context.SourcePath.FullName, "_static");
#if DEBUG
// this attempts to serve files directly from their source rather than the embedded resourses during development.
// this allows us to change js/css files without restarting the webserver
var solutionRoot = Paths.GetSolutionDirectory();
if (solutionRoot != null)
_staticFilesDirectory = Path.Combine(solutionRoot.FullName, "src", "Elastic.Markdown", "_static");
#endif

_webApplication = builder.Build();
SetUpRoutes();
}
Expand All @@ -52,7 +63,7 @@ private void SetUpRoutes()
_webApplication.UseLiveReload();
_webApplication.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(_staticFilesDirectory),
FileProvider = new EmbeddedOrPhysicalFileProvider(_staticFilesDirectory),
RequestPath = "/_static"
});
_webApplication.UseRouting();
Expand Down Expand Up @@ -86,3 +97,40 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
}
}
}


public class EmbeddedOrPhysicalFileProvider : IFileProvider
{
private readonly EmbeddedFileProvider _embeddedProvider;
private readonly PhysicalFileProvider _fileProvider;

public EmbeddedOrPhysicalFileProvider(string root)
{
_embeddedProvider = new EmbeddedFileProvider(typeof(BuildContext).Assembly, "Elastic.Markdown._static");
_fileProvider = new PhysicalFileProvider(root);
}

public IDirectoryContents GetDirectoryContents(string subpath)
{
var contents = _fileProvider.GetDirectoryContents(subpath);
if (!contents.Exists)
contents = _embeddedProvider.GetDirectoryContents(subpath);
return contents;
}

public IFileInfo GetFileInfo(string subpath)
{
var fileInfo = _fileProvider.GetFileInfo(subpath.Replace("/_static", ""));
if (!fileInfo.Exists)
fileInfo = _embeddedProvider.GetFileInfo(subpath);
return fileInfo;
}

public IChangeToken Watch(string filter)
{
var changeToken = _fileProvider.Watch(filter);
if (changeToken is NullChangeToken)
changeToken = _embeddedProvider.Watch(filter);
return changeToken;
}
}
2 changes: 2 additions & 0 deletions src/docs-builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Actions.Core.Extensions;
using ConsoleAppFramework;
using Documentation.Builder;
using Documentation.Builder.Cli;
using Elastic.Markdown.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -28,6 +29,7 @@
services.AddSingleton<DiagnosticsChannel>();
services.AddSingleton<DiagnosticsCollector>();


await using var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
ConsoleApp.ServiceProvider = serviceProvider;
Expand Down
1 change: 1 addition & 0 deletions src/docs-builder/docs-builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
<ItemGroup>
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/docs-generator/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task<int> Generate(
WriteMarkdownFile(outputFolder, file);
}

var name = $"random-docset-{seedContent}-{seedFileSystem}";
var name = $"random-docset-{Determinism.Random.SeedFileSystem}-{Determinism.Random.SeedFileSystem}";
WriteIndexMarkdownFile(name, outputFolder);

var docset = Path.Combine(outputFolder.FullName, "docset.yml");
Expand Down
2 changes: 0 additions & 2 deletions src/docs-generator/Domain/Generators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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 Slugify;
using Soenneker.Utils.AutoBogus;

namespace Documentation.Generator.Domain;
Expand All @@ -12,7 +11,6 @@ public static class Generators
public static AutoFaker<FolderName> FolderName { get; } = new();
public static AutoFaker<Section> Section { get; } = new();
public static AutoFaker<MarkdownFile> File { get; } = new();
public static SlugHelper Slug { get; } = new();

static Generators()
{
Expand Down
Loading