Skip to content

Commit

Permalink
Advertise docs-builder updates when running from command line (#276)
Browse files Browse the repository at this point in the history
* Advertise docs-builder updates when running from the command line

* Advertise at the end of command invocation

* remove up to date message

* include installation documentation in the output
  • Loading branch information
Mpdreamz authored Jan 21, 2025
1 parent 3166716 commit 5b7a700
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
95 changes: 95 additions & 0 deletions src/docs-builder/Cli/CheckForUpdatesFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// 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.Reflection;
using ConsoleAppFramework;
using Elastic.Markdown.Helpers;

namespace Documentation.Builder.Cli;

internal class CheckForUpdatesFilter : ConsoleAppFilter
{
private readonly FileInfo _stateFile;

public CheckForUpdatesFilter(ConsoleAppFilter next) : base(next)
{
// ~/Library/Application\ Support/ on osx
// XDG_DATA_HOME or home/.local/share on linux
// %LOCAL_APPLICATION_DATA% windows
var localPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var elasticPath = Path.Combine(localPath, "elastic");
_stateFile = new FileInfo(Path.Combine(elasticPath, "docs-build-check.state"));
}

public override async Task InvokeAsync(ConsoleAppContext context, Cancel ctx)
{
await Next.InvokeAsync(context, ctx);
var latestVersionUrl = await GetLatestVersion(ctx);
if (latestVersionUrl is null)
ConsoleApp.LogError("Unable to determine latest version");
else
CompareWithAssemblyVersion(latestVersionUrl);
}

private void CompareWithAssemblyVersion(Uri latestVersionUrl)
{
var versionPath = latestVersionUrl.AbsolutePath.Split('/').Last();
if (!SemVersion.TryParse(versionPath, out var latestVersion))
{
ConsoleApp.LogError($"Unable to parse latest version from {latestVersionUrl}");
return;
}

var assemblyVersion = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion;
if (SemVersion.TryParse(assemblyVersion ?? "", out var currentSemVersion))
{
if (latestVersion <= currentSemVersion)
return;
ConsoleApp.Log("");
ConsoleApp.Log($"A new version of docs-builder is available: {latestVersion} currently on version {currentSemVersion}");
ConsoleApp.Log("");
ConsoleApp.Log($" {latestVersionUrl}");
ConsoleApp.Log("");
ConsoleApp.Log("Read more about updating here:");
ConsoleApp.Log(" https://elastic.github.io/docs-builder/contribute/locally.html#step-one ");
ConsoleApp.Log("");
return;
}

ConsoleApp.LogError($"Unable to parse current version from docs-builder binary");
}

private async ValueTask<Uri?> GetLatestVersion(CancellationToken ctx)
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")))
return null;

// only check for new versions once per hour
if (_stateFile.Exists && _stateFile.LastWriteTimeUtc >= DateTime.UtcNow.Subtract(TimeSpan.FromHours(1)))
{
var url = await File.ReadAllTextAsync(_stateFile.FullName, ctx);
if (Uri.TryCreate(url, UriKind.Absolute, out var uri))
return uri;
}

try
{
var httpClient = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
var response = await httpClient.GetAsync("https://github.com/elastic/docs-builder/releases/latest", ctx);
var redirectUrl = response.Headers.Location;
if (redirectUrl is not null && _stateFile.Directory is not null)
{
// ensure the 'elastic' folder exists.
if (!Directory.Exists(_stateFile.Directory.FullName))
Directory.CreateDirectory(_stateFile.Directory.FullName);
await File.WriteAllTextAsync(_stateFile.FullName, redirectUrl.ToString(), ctx);
}
return redirectUrl;
}
// ReSharper disable once RedundantEmptyFinallyBlock
// ignore on purpose
finally { }
}
}
3 changes: 3 additions & 0 deletions src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class Commands(ILoggerFactory logger, ICoreService githubActionsService
/// <param name="port">Port to serve the documentation.</param>
/// <param name="ctx"></param>
[Command("serve")]
[ConsoleAppFilter<CheckForUpdatesFilter>]
public async Task Serve(string? path = null, int port = 3000, Cancel ctx = default)
{
var host = new DocumentationWebHost(path, port, logger, new FileSystem());
Expand All @@ -45,6 +46,7 @@ public async Task Serve(string? path = null, int port = 3000, Cancel ctx = defau
[Command("generate")]
[ConsoleAppFilter<StopwatchFilter>]
[ConsoleAppFilter<CatchExceptionFilter>]
[ConsoleAppFilter<CheckForUpdatesFilter>]
public async Task<int> Generate(
string? path = null,
string? output = null,
Expand Down Expand Up @@ -89,6 +91,7 @@ public async Task<int> Generate(
[Command("")]
[ConsoleAppFilter<StopwatchFilter>]
[ConsoleAppFilter<CatchExceptionFilter>]
[ConsoleAppFilter<CheckForUpdatesFilter>]
public async Task<int> GenerateDefault(
string? path = null,
string? output = null,
Expand Down
2 changes: 1 addition & 1 deletion src/docs-builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
services.AddSingleton<DiagnosticsChannel>();
services.AddSingleton<DiagnosticsCollector>();


await using var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
ConsoleApp.ServiceProvider = serviceProvider;

var isHelp = args.Contains("-h") || args.Contains("--help");
if (!isHelp)
ConsoleApp.Log = msg => logger.LogInformation(msg);
Expand Down

0 comments on commit 5b7a700

Please sign in to comment.