Skip to content

Commit d37a8a3

Browse files
authored
Support hot reload for cshtml files (#1657)
1 parent 33ba431 commit d37a8a3

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

Directory.Packages.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<!-- Enable central package management, https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management -->
44
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5+
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
56
</PropertyGroup>
67
<ItemGroup>
78
<GlobalPackageReference Include="MinVer" Version="6.0.0" PrivateAssets="All" />
@@ -42,7 +43,7 @@
4243
<PackageVersion Include="Markdig" Version="0.41.1" />
4344
<PackageVersion Include="NetEscapades.EnumGenerators" Version="1.0.0-beta12" PrivateAssets="all" ExcludeAssets="runtime" />
4445
<PackageVersion Include="Proc" Version="0.9.1" />
45-
<PackageVersion Include="RazorSlices" Version="0.9.2" />
46+
<PackageVersion Include="RazorSlices" Version="0.9.4" />
4647
<PackageVersion Include="Samboy063.Tomlet" Version="6.0.0" />
4748
<PackageVersion Include="Slugify.Core" Version="4.0.1" />
4849
<PackageVersion Include="SoftCircuits.IniFileParser" Version="2.7.0" />
@@ -70,4 +71,4 @@
7071
</PackageVersion>
7172
<PackageVersion Include="xunit.v3" Version="2.0.2" />
7273
</ItemGroup>
73-
</Project>
74+
</Project>

build/Targets.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let private version _ =
3434

3535
let private format _ = exec { run "dotnet" "format" "--verbosity" "quiet" }
3636

37-
let private watch _ = exec { run "dotnet" "watch" "--project" "src/tooling/docs-builder" "--no-hot-reload" "--" "serve" }
37+
let private watch _ = exec { run "dotnet" "watch" "--project" "src/tooling/docs-builder" "--configuration" "debug" "--" "serve" }
3838

3939
let private lint _ =
4040
match exec {

src/Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99

1010
<!-- TODO ENABLE to document our code properly <GenerateDocumentationFile>true</GenerateDocumentationFile> -->
1111
<PublishRepositoryUrl>true</PublishRepositoryUrl>
12+
<StartupHookSupport Condition="'$(Configuration)' == 'Debug'">true</StartupHookSupport>
1213
</PropertyGroup>
13-
</Project>
14+
</Project>

src/Elastic.Documentation.Site/Elastic.Documentation.Site.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
1111
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
1212
<IsPublishable>true</IsPublishable>
13-
<StartupHookSupport Condition="'$(Configuration)' == 'Debug'">true</StartupHookSupport>
1413
</PropertyGroup>
1514

1615
<ItemGroup>

src/tooling/docs-builder/Http/ReloadGeneratorService.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@
66
using Microsoft.Extensions.Logging;
77
using Westwind.AspNetCore.LiveReload;
88

9+
[assembly: System.Reflection.Metadata.MetadataUpdateHandler(typeof(Documentation.Builder.Http.HotReloadManager))]
10+
911
namespace Documentation.Builder.Http;
1012

13+
public static class HotReloadManager
14+
{
15+
public static void ClearCache(Type[]? _) => LiveReloadMiddleware.RefreshWebSocketRequest();
16+
17+
public static void UpdateApplication(Type[]? _) => Task.Run(async () =>
18+
{
19+
await Task.Delay(1000);
20+
var __ = LiveReloadMiddleware.RefreshWebSocketRequest();
21+
Console.WriteLine("UpdateApplication");
22+
});
23+
24+
}
25+
26+
1127
public sealed class ReloadGeneratorService(ReloadableGeneratorState reloadableGenerator, ILogger<ReloadGeneratorService> logger) : IHostedService, IDisposable
1228
{
1329
private FileSystemWatcher? _watcher;
@@ -22,6 +38,9 @@ public async Task StartAsync(Cancel cancellationToken)
2238
await ReloadableGenerator.ReloadAsync(cancellationToken);
2339

2440
var directory = ReloadableGenerator.Generator.DocumentationSet.SourceDirectory.FullName;
41+
#if DEBUG
42+
directory = ReloadableGenerator.Generator.Context.DocumentationCheckoutDirectory?.FullName ?? throw new InvalidOperationException("No checkout directory");
43+
#endif
2544
Logger.LogInformation("Start file watch on: {Directory}", directory);
2645
var watcher = new FileSystemWatcher(directory)
2746
{
@@ -40,6 +59,9 @@ public async Task StartAsync(Cancel cancellationToken)
4059
watcher.Renamed += OnRenamed;
4160
watcher.Error += OnError;
4261

62+
#if DEBUG
63+
watcher.Filters.Add("*.cshtml");
64+
#endif
4365
watcher.Filters.Add("*.md");
4466
watcher.Filters.Add("docset.yml");
4567
watcher.IncludeSubdirectories = true;
@@ -73,6 +95,10 @@ private void OnChanged(object sender, FileSystemEventArgs e)
7395
Reload();
7496
if (e.FullPath.EndsWith(".md"))
7597
Reload();
98+
#if DEBUG
99+
if (e.FullPath.EndsWith(".cshtml"))
100+
_ = LiveReloadMiddleware.RefreshWebSocketRequest();
101+
#endif
76102

77103
}
78104

@@ -97,6 +123,10 @@ private void OnRenamed(object sender, RenamedEventArgs e)
97123
Logger.LogInformation(" New: {NewFullPath}", e.FullPath);
98124
if (e.FullPath.EndsWith(".md"))
99125
Reload();
126+
#if DEBUG
127+
if (e.FullPath.EndsWith(".cshtml"))
128+
_ = LiveReloadMiddleware.RefreshWebSocketRequest();
129+
#endif
100130
}
101131

102132
private void OnError(object sender, ErrorEventArgs e) =>

0 commit comments

Comments
 (0)