Skip to content

Commit

Permalink
Embedd static resources in assembly (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Dec 10, 2024
1 parent 1367f9f commit e09de44
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
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
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: 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

0 comments on commit e09de44

Please sign in to comment.