Skip to content

Commit e09de44

Browse files
authored
Embedd static resources in assembly (#99)
1 parent 1367f9f commit e09de44

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

src/Elastic.Markdown/IO/Paths.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,13 @@ private static DirectoryInfo RootDirectoryInfo()
1515
}
1616

1717
public static readonly DirectoryInfo Root = RootDirectoryInfo();
18+
19+
/// Used in debug to locate static folder so we can change js/css files while the server is running
20+
public static DirectoryInfo? GetSolutionDirectory()
21+
{
22+
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
23+
while (directory != null && directory.GetFiles("*.sln").Length == 0)
24+
directory = directory.Parent;
25+
return directory;
26+
}
1827
}

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
108108

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

112116
if (!string.IsNullOrEmpty(anchor))
113117
link.Url += $"#{anchor}";

src/docs-builder/Http/DocumentationWebHost.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.Extensions.DependencyInjection;
1212
using Microsoft.Extensions.FileProviders;
13+
using Microsoft.Extensions.FileProviders.Physical;
1314
using Microsoft.Extensions.Hosting;
1415
using Microsoft.Extensions.Logging;
16+
using Microsoft.Extensions.Primitives;
1517
using Westwind.AspNetCore.LiveReload;
18+
using IFileInfo = Microsoft.Extensions.FileProviders.IFileInfo;
1619

1720
namespace Documentation.Builder.Http;
1821

1922
public class DocumentationWebHost
2023
{
2124
private readonly WebApplication _webApplication;
2225

23-
private readonly string _staticFilesDirectory =
24-
Path.Combine(Paths.Root.FullName, "docs", "source", "_static");
26+
private readonly string _staticFilesDirectory;
2527

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

45+
_staticFilesDirectory = Path.Combine(context.SourcePath.FullName, "_static");
46+
#if DEBUG
47+
// this attempts to serve files directly from their source rather than the embedded resourses during development.
48+
// this allows us to change js/css files without restarting the webserver
49+
var solutionRoot = Paths.GetSolutionDirectory();
50+
if (solutionRoot != null)
51+
_staticFilesDirectory = Path.Combine(solutionRoot.FullName, "src", "Elastic.Markdown", "_static");
52+
#endif
53+
4354
_webApplication = builder.Build();
4455
SetUpRoutes();
4556
}
@@ -52,7 +63,7 @@ private void SetUpRoutes()
5263
_webApplication.UseLiveReload();
5364
_webApplication.UseStaticFiles(new StaticFileOptions
5465
{
55-
FileProvider = new PhysicalFileProvider(_staticFilesDirectory),
66+
FileProvider = new EmbeddedOrPhysicalFileProvider(_staticFilesDirectory),
5667
RequestPath = "/_static"
5768
});
5869
_webApplication.UseRouting();
@@ -86,3 +97,40 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
8697
}
8798
}
8899
}
100+
101+
102+
public class EmbeddedOrPhysicalFileProvider : IFileProvider
103+
{
104+
private readonly EmbeddedFileProvider _embeddedProvider;
105+
private readonly PhysicalFileProvider _fileProvider;
106+
107+
public EmbeddedOrPhysicalFileProvider(string root)
108+
{
109+
_embeddedProvider = new EmbeddedFileProvider(typeof(BuildContext).Assembly, "Elastic.Markdown._static");
110+
_fileProvider = new PhysicalFileProvider(root);
111+
}
112+
113+
public IDirectoryContents GetDirectoryContents(string subpath)
114+
{
115+
var contents = _fileProvider.GetDirectoryContents(subpath);
116+
if (!contents.Exists)
117+
contents = _embeddedProvider.GetDirectoryContents(subpath);
118+
return contents;
119+
}
120+
121+
public IFileInfo GetFileInfo(string subpath)
122+
{
123+
var fileInfo = _fileProvider.GetFileInfo(subpath.Replace("/_static", ""));
124+
if (!fileInfo.Exists)
125+
fileInfo = _embeddedProvider.GetFileInfo(subpath);
126+
return fileInfo;
127+
}
128+
129+
public IChangeToken Watch(string filter)
130+
{
131+
var changeToken = _fileProvider.Watch(filter);
132+
if (changeToken is NullChangeToken)
133+
changeToken = _embeddedProvider.Watch(filter);
134+
return changeToken;
135+
}
136+
}

src/docs-generator/Cli/Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public async Task<int> Generate(
9595
WriteMarkdownFile(outputFolder, file);
9696
}
9797

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

101101
var docset = Path.Combine(outputFolder.FullName, "docset.yml");

src/docs-generator/Domain/Generators.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
using Slugify;
65
using Soenneker.Utils.AutoBogus;
76

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

1715
static Generators()
1816
{

0 commit comments

Comments
 (0)