Skip to content

Commit 131a5bf

Browse files
Mpdreamzreakaleek
andauthored
Add ./build.sh watch to integrate all continuous builds. (#390)
All continuous builds are now servicable through `dotnet watch` > dotnet watch --project src/docs-builder --no-hot-reload -- serve Will build web assets without recompiling c# or rebooting the server. C# or cshtml changes will rebuild automatically. Markdown files will continue to be server through liverload without a full restart. Co-authored-by: Jan Calanog <[email protected]>
1 parent 9e26ce5 commit 131a5bf

File tree

7 files changed

+81
-19
lines changed

7 files changed

+81
-19
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,22 @@ existing surveyed tools
144144
- [.NET 9.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)
145145
- [Node.js 22.13.1 (LTS)](https://nodejs.org/en/blog/release/v22.13.1)
146146

147-
## Live Reload Markdown files in the `docs` folder:
148-
```shell
149-
dotnet run --project src/docs-builder -- serve
150-
```
151147

152-
## Automatically rebuild changes to `src/Elastic.Markdown/Assets` JS and CSS files:
148+
## Continuously build all assets during development.
149+
153150
```shell
154-
cd src/Elastic.Markdown
155-
npm ci
156-
npm run watch
151+
./build.sh watch
157152
```
158153

154+
This will monitor code, cshtml template files & static files and reload the application
155+
if any changes.
156+
157+
Web assets are reloaded through `parcel watch` and don't require a recompilation.
158+
159+
Markdown files are refreshed automatically through livereload
160+
161+
Code or layout changes will relaunch the server automatically
162+
159163
# Release Process
160164

161165
This section outlines the process for releasing a new version of this project.

build/CommandLine.fs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ type Build =
1717
| [<CliPrefix(CliPrefix.None);SubCommand>] Test
1818

1919
| [<CliPrefix(CliPrefix.None);SubCommand>] Format
20-
20+
| [<CliPrefix(CliPrefix.None);SubCommand>] Watch
21+
2122
| [<CliPrefix(CliPrefix.None);Hidden;SubCommand>] Lint
2223
| [<CliPrefix(CliPrefix.None);Hidden;SubCommand>] PristineCheck
2324
| [<CliPrefix(CliPrefix.None);Hidden;SubCommand>] ValidateLicenses
@@ -46,7 +47,9 @@ with
4647
| Release -> "runs build, tests, and create and validates the packages shy of publishing them"
4748
| Publish -> "Publishes artifacts"
4849
| Format -> "runs dotnet format"
49-
50+
51+
| Watch -> "runs dotnet watch to continuous build code/templates and web assets on the fly"
52+
5053
// steps
5154
| Lint
5255
| PristineCheck

build/Targets.fs

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ let private version _ =
3535

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

38+
let private watch _ = exec { run "dotnet" "watch" "--project" "src/docs-builder" "--no-hot-reload" "--" "serve" }
39+
3840
let private lint _ =
3941
match exec {
4042
exit_code_of "dotnet" "format" "--verify-no-changes"
@@ -167,6 +169,7 @@ let Setup (parsed:ParseResults<Build>) =
167169
release
168170

169171
| Format -> Build.Step format
172+
| Watch -> Build.Step watch
170173

171174
// steps
172175
| Lint -> Build.Step lint

src/Elastic.Markdown/Assets/styles.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
@import "legacy/togglebutton.css";
66
@import "legacy/sphinx-design.min.css";
77
@import "legacy/custom.css";
8-
@import "legacy/atom-one-light.css";
8+
@import "legacy/atom-one-light.css";

src/Elastic.Markdown/Elastic.Markdown.csproj

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Documentation: https://learn.microsoft.com/en-us/visualstudio/msbuild/incremental-builds?WT.mc_id=DT-MVP-5003978
2323
-->
2424
<Target Name="NpmInstall" Inputs="package.json" Outputs="node_modules/.install-stamp">
25-
<Exec Command="npm ci" />
25+
<Exec Command="npm ci" WorkingDirectory="$(MSBuildThisFileDirectory)" ConsoleToMsBuild="true" />
2626

2727
<!-- Write the stamp file, so incremental builds work -->
2828
<Touch Files="node_modules/.install-stamp" AlwaysCreate="true" />
@@ -33,15 +33,17 @@
3333
MSBuild runs NpmInstall before this task because of the DependsOnTargets attribute.
3434
-->
3535
<Target Name="NpmRunBuild" DependsOnTargets="NpmInstall" BeforeTargets="BeforeBuild">
36-
<Exec Command="npm run build" />
36+
<Exec Command="npm run build" WorkingDirectory="$(MSBuildThisFileDirectory)" ConsoleToMsBuild="true">
37+
<Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
38+
</Exec>
3739
</Target>
3840

3941
<Target Name="EmbedGeneratedAssets" AfterTargets="NpmRunBuild">
4042
<ItemGroup>
41-
<EmbeddedResource Include="_static/*.js.map" />
42-
<EmbeddedResource Include="_static/*.js" />
43-
<EmbeddedResource Include="_static/*.css" />
44-
<EmbeddedResource Include="_static/*.css.map" />
43+
<EmbeddedResource Include="_static/*.js" Watch="false" />
44+
<EmbeddedResource Include="_static/*.js.map" Watch="false" />
45+
<EmbeddedResource Include="_static/*.css" Watch="false" />
46+
<EmbeddedResource Include="_static/*.css.map" Watch="false" />
4547
<EmbeddedResource Include="_static/*.svg" />
4648
</ItemGroup>
4749
</Target>
@@ -58,4 +60,5 @@
5860
<PackageReference Include="YamlDotNet" Version="16.1.3" />
5961
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
6062
</ItemGroup>
63+
6164
</Project>

src/docs-builder/Http/DocumentationWebHost.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44
using System.Diagnostics.CodeAnalysis;
55
using System.IO.Abstractions;
6+
using System.Reflection;
67
using Documentation.Builder.Diagnostics;
78
using Documentation.Builder.Diagnostics.Console;
89
using Documentation.Builder.Diagnostics.LiveMode;
@@ -28,9 +29,11 @@ public class DocumentationWebHost
2829
private readonly WebApplication _webApplication;
2930

3031
private readonly BuildContext _context;
32+
private readonly ILogger<DocumentationWebHost> _logger;
3133

3234
public DocumentationWebHost(string? path, int port, ILoggerFactory logger, IFileSystem fileSystem)
3335
{
36+
_logger = logger.CreateLogger<DocumentationWebHost>();
3437
var builder = WebApplication.CreateSlimBuilder();
3538

3639
builder.Logging.ClearProviders();
@@ -52,15 +55,17 @@ public DocumentationWebHost(string? path, int port, ILoggerFactory logger, IFile
5255
});
5356
builder.Services.AddSingleton<ReloadableGeneratorState>(_ => new ReloadableGeneratorState(_context.SourcePath, null, _context, logger));
5457
builder.Services.AddHostedService<ReloadGeneratorService>();
55-
56-
//builder.Services.AddSingleton(logger);
58+
if (IsDotNetWatchBuild())
59+
builder.Services.AddHostedService<ParcelWatchService>();
5760

5861
builder.WebHost.UseUrls($"http://localhost:{port}");
5962

6063
_webApplication = builder.Build();
6164
SetUpRoutes();
6265
}
6366

67+
private bool IsDotNetWatchBuild() =>
68+
Environment.GetEnvironmentVariable("DOTNET_WATCH") is not null;
6469

6570
public async Task RunAsync(Cancel ctx)
6671
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Diagnostics;
6+
using Elastic.Markdown.IO;
7+
using Microsoft.Extensions.Hosting;
8+
9+
namespace Documentation.Builder.Http;
10+
11+
public class ParcelWatchService : IHostedService
12+
{
13+
private Process? _process;
14+
15+
public Task StartAsync(CancellationToken cancellationToken)
16+
{
17+
_process = Process.Start(new ProcessStartInfo
18+
{
19+
FileName = "npm",
20+
Arguments = "run watch",
21+
RedirectStandardOutput = true,
22+
RedirectStandardError = true,
23+
UseShellExecute = false,
24+
CreateNoWindow = true,
25+
WorkingDirectory = Path.Combine(Paths.Root.FullName, "src", "Elastic.Markdown")
26+
})!;
27+
28+
_process.EnableRaisingEvents = true;
29+
_process.OutputDataReceived += (_, e) => Console.WriteLine($"[npm run watch]: {e.Data}");
30+
_process.ErrorDataReceived += (_, e) => Console.WriteLine($"[npm run watch]: {e.Data}");
31+
32+
_process.BeginOutputReadLine();
33+
_process.BeginErrorReadLine();
34+
35+
return Task.CompletedTask;
36+
}
37+
38+
public Task StopAsync(CancellationToken cancellationToken)
39+
{
40+
_process?.Kill(entireProcessTree: true);
41+
_process?.Kill();
42+
return Task.CompletedTask;
43+
}
44+
}

0 commit comments

Comments
 (0)