Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Elastic.Documentation.Tooling/DocumentationTooling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 Actions.Core.Extensions;
using Elastic.Documentation.Tooling.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

namespace Elastic.Documentation.Tooling;

public static class DocumentationTooling
{
public static ServiceProvider CreateServiceProvider(ref string[] args, Action<IServiceCollection>? configure = null)
{
var defaultLogLevel = LogLevel.Information;
ProcessCommandLineArguments(ref args, ref defaultLogLevel);

var services = new ServiceCollection()
.AddGitHubActionsCore();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ConsoleFormatter, CondensedConsoleFormatter>());
_ = services.AddLogging(x => x
.ClearProviders()
.SetMinimumLevel(defaultLogLevel)
.AddConsole(c => c.FormatterName = "condensed")
);


configure?.Invoke(services);

return services.BuildServiceProvider();
}

private static void ProcessCommandLineArguments(ref string[] args, ref LogLevel defaultLogLevel)
{
var newArgs = new List<string>();
for (var i = 0; i < args.Length; i++)
{
if (args[i] == "--log-level")
{
if (args.Length > i + 1)
defaultLogLevel = GetLogLevel(args[i + 1]);

i++;
}
else
newArgs.Add(args[i]);
}

args = [.. newArgs];
}

private static LogLevel GetLogLevel(string? logLevel) => logLevel switch
{
"trace" => LogLevel.Trace,
"debug" => LogLevel.Debug,
"information" => LogLevel.Information,
"info" => LogLevel.Information,
"warning" => LogLevel.Warning,
"error" => LogLevel.Error,
"critical" => LogLevel.Critical,
_ => LogLevel.Information
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework.Abstractions" Version="5.4.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.2"/>
<PackageReference Include="Github.Actions.Core" Version="9.0.0"/>
<PackageReference Include="Crayon" Version="2.0.69"/>
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions Elastic.Documentation.Tooling/Filters/CatchExceptionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 ConsoleAppFramework;
using Microsoft.Extensions.Logging;

namespace Elastic.Documentation.Tooling.Filters;

public sealed class CatchExceptionFilter(ConsoleAppFilter next, ILogger<CatchExceptionFilter> logger)
: ConsoleAppFilter(next)
{
public override async Task InvokeAsync(ConsoleAppContext context, Cancel cancellationToken)
{
try
{
await Next.InvokeAsync(context, cancellationToken);
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
logger.LogInformation("Cancellation requested, exiting.");
return;
}

throw;

}
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,31 @@
// 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.Diagnostics;
using ConsoleAppFramework;
using Microsoft.Extensions.Logging;

namespace Documentation.Builder.Cli;
namespace Elastic.Documentation.Tooling.Filters;

internal sealed class StopwatchFilter(ConsoleAppFilter next) : ConsoleAppFilter(next)
public class StopwatchFilter(ConsoleAppFilter next, ILogger<StopwatchFilter> logger) : ConsoleAppFilter(next)
{
public override async Task InvokeAsync(ConsoleAppContext context, Cancel ctx)
public override async Task InvokeAsync(ConsoleAppContext context, Cancel cancellationToken)
{
var isHelpOrVersion = context.Arguments.Any(a => a is "--help" or "-h" or "--version");
var name = string.IsNullOrWhiteSpace(context.CommandName) ? "generate" : context.CommandName;
var startTime = Stopwatch.GetTimestamp();
if (!isHelpOrVersion)
ConsoleApp.Log($"{name} :: Starting...");
logger.LogInformation("{Name} :: Starting...", name);
try
{
await Next.InvokeAsync(context, ctx);
await Next.InvokeAsync(context, cancellationToken);
}
finally
{
var endTime = Stopwatch.GetElapsedTime(startTime);
if (!isHelpOrVersion)
ConsoleApp.Log($"{name} :: Finished in '{endTime}");
}
}
}

internal sealed class CatchExceptionFilter(ConsoleAppFilter next) : ConsoleAppFilter(next)
{
public override async Task InvokeAsync(ConsoleAppContext context, Cancel cancellationToken)
{
try
{
await Next.InvokeAsync(context, cancellationToken);
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
ConsoleApp.Log("Cancellation requested, exiting.");
return;
}

throw;

logger.LogInformation("{Name} :: Finished in '{EndTime}'", name, endTime);
}
}
}
57 changes: 57 additions & 0 deletions Elastic.Documentation.Tooling/Logging/CondensedConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
using static Crayon.Output;

namespace Elastic.Documentation.Tooling.Logging;

public class CondensedConsoleFormatter() : ConsoleFormatter("condensed")
{
public override void Write<TState>(
in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter
)
{
var now = DateTime.UtcNow;
var message = logEntry.Formatter.Invoke(logEntry.State, logEntry.Exception);

var logLevel = GetLogLevel(logEntry.LogLevel);
var categoryName = logEntry.Category;

var nowString =
Environment.UserInteractive
? ""
: now.ToString("[yyyy-MM-ddTHH:mm:ss.fffZ] ", System.Globalization.CultureInfo.InvariantCulture);

textWriter.WriteLine($"{nowString}{logLevel}::{ShortCategoryName(categoryName)}:: {message}");
}

private static string GetLogLevel(LogLevel logLevel) => logLevel switch
{
LogLevel.Trace => "trace",
LogLevel.Debug => "debug",
LogLevel.Information => Blue().Bold().Text("info "),
LogLevel.Warning => Yellow().Bold().Text("warn "),
LogLevel.Error => Red().Bold().Text("error"),
LogLevel.Critical => Red().Bold().Text("fail "),
LogLevel.None => " ",
_ => "???"
};

private static string ShortCategoryName(string category)
{
var tokens = category.Split('.', StringSplitOptions.RemoveEmptyEntries);
var s = string.Join(".", tokens.Take(tokens.Length - 1).Select(t => t.ToLowerInvariant().First()).ToArray());
if (s.Length > 0)
s += ".";

var maxLength = 22 - s.Length;
var last = tokens.Last();
var start = Math.Max(0, last.Length - maxLength);
s += last[start..];
return Dim().Text(s.PadRight(22));
}
}
5 changes: 5 additions & 0 deletions Elastic.Documentation.Tooling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Elastic.Documentation.Tooling

This is a shared library for our command line tooling.

Ensures we share the same logging infrastructure and command line helpers
7 changes: 7 additions & 0 deletions docs-builder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assembler", "assembler", "{
actions\assembler\action.yml = actions\assembler\action.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Tooling", "Elastic.Documentation.Tooling\Elastic.Documentation.Tooling.csproj", "{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,10 @@ Global
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.Build.0 = Release|Any CPU
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4D198E25-C211-41DC-9E84-B15E89BD7048} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
Expand All @@ -106,5 +112,6 @@ Global
{018F959E-824B-4664-B345-066784478D24} = {67B576EE-02FA-4F9B-94BC-3630BC09ECE5}
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
{CFEE9FAD-9E0C-4C0E-A0C2-B97D594C14B5} = {245023D2-D3CA-47B9-831D-DAB91A2FFDC7}
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private async Task ExtractEmbeddedStaticResources(Cancel ctx)
outputFile.Directory.Create();
await using var stream = outputFile.OpenWrite();
await resourceStream.CopyToAsync(stream, ctx);
_logger.LogInformation("Copied static embedded resource {Path}", path);
_logger.LogDebug("Copied static embedded resource {Path}", path);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/IO/DocumentationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public DocumentationSet(BuildContext context, ILoggerFactory logger, ICrossLinkR
Files = [.. context.ReadFileSystem.Directory
.EnumerateFiles(SourcePath.FullName, "*.*", SearchOption.AllDirectories)
.Select(f => context.ReadFileSystem.FileInfo.New(f))
.Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden) && !f.Attributes.HasFlag(FileAttributes.System))
.Where(f => !f.Directory!.Attributes.HasFlag(FileAttributes.Hidden) && !f.Directory!.Attributes.HasFlag(FileAttributes.System))
// skip hidden folders
.Where(f => !Path.GetRelativePath(SourcePath.FullName, f.FullName).StartsWith('.'))
Comment on lines +54 to +57
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reakaleek this is actually an important fix for docs-content and asciidocalypse since they generate from the root 😸

.Select<IFileInfo, DocumentationFile>(file => file.Extension switch
{
".jpg" => new ImageFile(file, SourcePath, "image/jpeg"),
Expand Down
52 changes: 0 additions & 52 deletions src/docs-assembler/Cli/Filters.cs

This file was deleted.

22 changes: 5 additions & 17 deletions src/docs-assembler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@
// 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 Actions.Core.Extensions;
using Actions.Core.Services;
using ConsoleAppFramework;
using Documentation.Assembler.Cli;
using Elastic.Documentation.Tooling;
using Elastic.Documentation.Tooling.Filters;
using Elastic.Markdown.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

var services = new ServiceCollection();
services.AddGitHubActionsCore();
services.AddLogging(x => x
.ClearProviders()
.SetMinimumLevel(LogLevel.Information)
.AddSimpleConsole(c =>
{
c.SingleLine = true;
c.IncludeScopes = true;
c.UseUtcTimestamp = true;
c.TimestampFormat = Environment.UserInteractive ? ":: " : "[yyyy-MM-ddTHH:mm:ss] ";
})
await using var serviceProvider = DocumentationTooling.CreateServiceProvider(ref args, services => services
.AddSingleton<DiagnosticsChannel>()
.AddSingleton<DiagnosticsCollector>()
);
services.AddSingleton<DiagnosticsChannel>();
services.AddSingleton<DiagnosticsCollector>();

await using var serviceProvider = services.BuildServiceProvider();
ConsoleApp.ServiceProvider = serviceProvider;

var app = ConsoleApp.Create();
Expand Down
7 changes: 3 additions & 4 deletions src/docs-assembler/docs-assembler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.414.5" />
<PackageReference Include="ConsoleAppFramework" Version="5.3.3">
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="ConsoleAppFramework.Abstractions" Version="5.4.1" />
<PackageReference Include="ConsoleAppFramework" Version="5.4.1" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0"/>
<PackageReference Include="Github.Actions.Core" Version="9.0.0"/>
<PackageReference Include="Proc" Version="0.9.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Elastic.Documentation.Tooling\Elastic.Documentation.Tooling.csproj" />
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
</ItemGroup>
</Project>
Loading