Skip to content
Open
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
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<PackageVersion Include="OneOf.SourceGenerator" Version="3.0.271" />
<PackageVersion Include="PdfPig" Version="0.1.11" />
<PackageVersion Include="PlantUml.Net" Version="1.4.80" />
<PackageVersion Include="Spectre.Console" Version="0.52.0" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.52.0" />
<PackageVersion Include="Spectre.Console" Version="0.53.0" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.53.0" />
<PackageVersion Include="Stubble.Core" Version="1.10.8" />
<PackageVersion Include="System.Composition" Version="9.0.10" />
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
Expand Down
14 changes: 14 additions & 0 deletions src/Docfx.App/PdfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using Docfx.Build;
Expand Down Expand Up @@ -73,6 +74,19 @@ public static async Task CreatePdf(string outputFolder, CancellationToken cancel

Program.Main(["install", "chromium", "--only-shell"]);

// Create linked CancellationToken with PosixSignalRegistration handler.
// It's required because default `Ctrl+C` interruption is canceled when using WebApplication inside Spectre.Console command.
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
void onSignal(PosixSignalContext context)
{
context.Cancel = true;
cancellationTokenSource.Cancel();
}
using var sigInt = PosixSignalRegistration.Create(PosixSignal.SIGINT, onSignal);
using var sigQuit = PosixSignalRegistration.Create(PosixSignal.SIGQUIT, onSignal);
using var sigTerm = PosixSignalRegistration.Create(PosixSignal.SIGTERM, onSignal);
cancellationToken = cancellationTokenSource.Token;

var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
builder.WebHost.UseUrls("http://127.0.0.1:0");
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/DotnetApiCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static async Task GenerateManagedReferenceYamlFiles(string configPath, Do
}
}

internal static async Task Exec(MetadataJsonConfig config, DotnetApiOptions options, string configDirectory, string outputDirectory = null)
internal static async Task Exec(MetadataJsonConfig config, DotnetApiOptions options, string configDirectory, string outputDirectory = null, CancellationToken cancellationToken = default)
{
var stopwatch = Stopwatch.StartNew();

Expand Down
8 changes: 5 additions & 3 deletions src/docfx/Models/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

namespace Docfx;

internal class BuildCommand : Command<BuildCommandOptions>
# pragma warning disable 1998 // CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls

internal class BuildCommand : AsyncCommand<BuildCommandOptions>
{
public override int Execute(CommandContext context, BuildCommandOptions settings)
public override Task<int> ExecuteAsync(CommandContext context, BuildCommandOptions settings, CancellationToken cancellationToken)
{
return CommandHelper.Run(settings, () =>
return CommandHelper.RunAsync(settings, async () =>
{
if (settings.Serve && CommandHelper.IsTcpPortAlreadyUsed(settings.Host, settings.Port))
{
Expand Down
33 changes: 0 additions & 33 deletions src/docfx/Models/CancellableCommandBase.cs

This file was deleted.

60 changes: 40 additions & 20 deletions src/docfx/Models/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,28 @@ public static int Run(Action run)

Logger.Flush();
Logger.UnregisterAllListeners();
// Logger.PrintSummary() method is not called when Run without LogOptions.(DownloadCommand/ServeCommand/TemplateCommand)

return 0;
}

public static int Run(LogOptions options, Action run)
{
var consoleLogListener = new ConsoleLogListener();
Logger.RegisterListener(consoleLogListener);

if (!string.IsNullOrWhiteSpace(options.LogFilePath))
{
Logger.RegisterListener(new ReportLogListener(options.LogFilePath));
}
SetupLogger(options);

if (options.LogLevel.HasValue)
{
Logger.LogLevelThreshold = options.LogLevel.Value;
}
else if (options.Verbose)
{
Logger.LogLevelThreshold = LogLevel.Verbose;
}
run();

Logger.WarningsAsErrors = options.WarningsAsErrors;
CleanupLogger();
return Logger.HasError ? -1 : 0;
}

run();
public static async Task<int> RunAsync(LogOptions options, Func<Task> run)
{
SetupLogger(options);

Logger.Flush();
Logger.UnregisterAllListeners();
Logger.PrintSummary();
await run();

CleanupLogger();
return Logger.HasError ? -1 : 0;
}

Expand Down Expand Up @@ -82,4 +73,33 @@ public static bool IsTcpPortAlreadyUsed(string? host, int? port)
}
}
}

private static void SetupLogger(LogOptions options)
{
var consoleLogListener = new ConsoleLogListener();
Logger.RegisterListener(consoleLogListener);

if (!string.IsNullOrWhiteSpace(options.LogFilePath))
{
Logger.RegisterListener(new ReportLogListener(options.LogFilePath));
}

if (options.LogLevel.HasValue)
{
Logger.LogLevelThreshold = options.LogLevel.Value;
}
else if (options.Verbose)
{
Logger.LogLevelThreshold = LogLevel.Verbose;
}

Logger.WarningsAsErrors = options.WarningsAsErrors;
}

private static void CleanupLogger()
{
Logger.Flush();
Logger.UnregisterAllListeners();
Logger.PrintSummary();
}
}
2 changes: 1 addition & 1 deletion src/docfx/Models/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Docfx;

class DefaultCommand : CancellableCommandBase<DefaultCommand.Options>
class DefaultCommand : Command<DefaultCommand.Options>
{
[Description("Runs metadata, build and pdf commands")]
internal class Options : BuildCommandOptions
Expand Down
2 changes: 1 addition & 1 deletion src/docfx/Models/DownloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Docfx;

internal class DownloadCommand : Command<DownloadCommandOptions>
{
public override int Execute([NotNull] CommandContext context, [NotNull] DownloadCommandOptions options)
public override int Execute([NotNull] CommandContext context, [NotNull] DownloadCommandOptions options, CancellationToken cancellationToken)
{
return CommandHelper.Run(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/docfx/Models/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Docfx;

class InitCommand : Command<InitCommandOptions>
{
public override int Execute([NotNull] CommandContext context, [NotNull] InitCommandOptions options)
public override int Execute([NotNull] CommandContext context, [NotNull] InitCommandOptions options, CancellationToken cancellationToken)
{
WriteLine(
"""
Expand Down
2 changes: 1 addition & 1 deletion src/docfx/Models/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Docfx;

internal class MergeCommand : Command<MergeCommandOptions>
{
public override int Execute([NotNull] CommandContext context, [NotNull] MergeCommandOptions options)
public override int Execute([NotNull] CommandContext context, [NotNull] MergeCommandOptions options, CancellationToken cancellationToken)
{
return CommandHelper.Run(options, () =>
{
Expand Down
8 changes: 4 additions & 4 deletions src/docfx/Models/MetadataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace Docfx;

internal class MetadataCommand : Command<MetadataCommandOptions>
internal class MetadataCommand : AsyncCommand<MetadataCommandOptions>
{
public override int Execute([NotNull] CommandContext context, [NotNull] MetadataCommandOptions options)
public override Task<int> ExecuteAsync([NotNull] CommandContext context, [NotNull] MetadataCommandOptions options, CancellationToken cancellationToken)
{
return CommandHelper.Run(options, () =>
return CommandHelper.RunAsync(options, async () =>
{
var (config, baseDirectory) = Docset.GetConfig(options.Config);
MergeOptionsToConfig(options, config);
DotnetApiCatalog.Exec(config.metadata, new(), baseDirectory, options.OutputFolder).GetAwaiter().GetResult();
await DotnetApiCatalog.Exec(config.metadata, new(), baseDirectory, options.OutputFolder, cancellationToken);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/docfx/Models/PdfCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

namespace Docfx;

internal class PdfCommand : CancellableCommandBase<PdfCommandOptions>
internal class PdfCommand : AsyncCommand<PdfCommandOptions>
{
public override int Execute(CommandContext context, PdfCommandOptions options, CancellationToken cancellationToken)
public override async Task<int> ExecuteAsync(CommandContext context, PdfCommandOptions options, CancellationToken cancellationToken)
{
return CommandHelper.Run(options, () =>
return await CommandHelper.RunAsync(options, async () =>
{
var (config, configDirectory) = Docset.GetConfig(options.ConfigFile);

if (config.build is not null)
PdfBuilder.Run(config.build, configDirectory, options.OutputFolder, cancellationToken).GetAwaiter().GetResult();
await PdfBuilder.Run(config.build, configDirectory, options.OutputFolder, cancellationToken);
});
}
}
2 changes: 1 addition & 1 deletion src/docfx/Models/ServeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class Settings : CommandSettings
public string OpenFile { get; set; }
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings options)
public override int Execute([NotNull] CommandContext context, [NotNull] Settings options, CancellationToken cancellationToken)
{
return CommandHelper.Run(() => RunServe.Exec(options.Folder, options.Host, options.Port, options.OpenBrowser, options.OpenFile));
}
Expand Down
4 changes: 2 additions & 2 deletions src/docfx/Models/TemplateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class TemplateCommand
{
public class ListCommand : Command
{
public override int Execute(CommandContext context)
public override int Execute(CommandContext context, CancellationToken cancellationToken)
{
foreach (var path in Directory.GetDirectories(GetTemplateBaseDirectory()))
Console.WriteLine(Path.GetFileName(path));
Expand All @@ -38,7 +38,7 @@ internal class Options : CommandSettings
public string OutputFolder { get; set; }
}

public override int Execute(CommandContext context, Options options)
public override int Execute(CommandContext context, Options options, CancellationToken cancellationToken)
{
return CommandHelper.Run(() =>
{
Expand Down