Skip to content

Commit

Permalink
fix: forward build command args to default command (#8689)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Apr 26, 2023
1 parent 30be6fe commit 95d113d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
13 changes: 3 additions & 10 deletions src/docfx/Models/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ public override int Execute(CommandContext context, BuildCommandOptions settings
{
return CommandHelper.Run(settings, () =>
{
var config = ParseOptions(settings, out var baseDirectory, out var outputFolder);
var serveDirectory = RunBuild.Exec(config, new(), baseDirectory, outputFolder);
var (config, baseDirectory) = CommandHelper.GetConfig<BuildConfig>(settings.ConfigFile);
MergeOptionsToConfig(settings, config.Item, baseDirectory);
var serveDirectory = RunBuild.Exec(config.Item, new(), baseDirectory, settings.OutputFolder);

if (settings.Serve)
RunServe.Exec(serveDirectory, settings.Host, settings.Port);
});
}

private static BuildJsonConfig ParseOptions(BuildCommandOptions options, out string baseDirectory, out string outputFolder)
{
(var config, baseDirectory) = CommandHelper.GetConfig<BuildConfig>(options.ConfigFile);
outputFolder = options.OutputFolder;
MergeOptionsToConfig(options, config.Item, baseDirectory);
return config.Item;
}

internal static void MergeOptionsToConfig(BuildCommandOptions options, BuildJsonConfig config, string configDirectory)
{
// base directory for content from command line is current directory
Expand Down
35 changes: 13 additions & 22 deletions src/docfx/Models/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,11 @@ namespace Microsoft.DocAsCode;
class DefaultCommand : Command<DefaultCommand.Options>
{
[Description("Runs metadata, build and pdf commands")]
internal class Options : LogOptions
internal class Options : BuildCommandOptions
{
[Description("Prints version information")]
[CommandOption("-v|--version")]
public bool Version { get;set; }

[Description("Specify the output base directory")]
[CommandOption("-o|--output")]
public string OutputFolder { get; set; }

[Description("Host the generated documentation to a website")]
[CommandOption("-s|--serve")]
public bool Serve { get; set; }

[Description("Specify the hostname of the hosted website [localhost]")]
[CommandOption("-n|--hostname")]
public string Host { get; set; }

[Description("Specify the port of the hosted website [8080]")]
[CommandOption("-p|--port")]
public int? Port { get; set; }

[Description("Path to docfx.json")]
[CommandArgument(0, "[config]")]
public string Config { get; set; }
}

public override int Execute(CommandContext context, Options options)
Expand All @@ -49,18 +29,29 @@ public override int Execute(CommandContext context, Options options)

return CommandHelper.Run(options, () =>
{
var (config, baseDirectory) = CommandHelper.GetConfig<Config>(options.Config);
var (config, baseDirectory) = CommandHelper.GetConfig<Config>(options.ConfigFile);
var outputFolder = options.OutputFolder;
string serveDirectory = null;

if (config.Metadata is not null)
DotnetApiCatalog.Exec(config.Metadata, new(), baseDirectory, outputFolder).GetAwaiter().GetResult();

if (config.Build is not null)
{
BuildCommand.MergeOptionsToConfig(options, config.Build, baseDirectory);
serveDirectory = RunBuild.Exec(config.Build, new(), baseDirectory, outputFolder);
}

if (config.Pdf is not null)
{
BuildCommand.MergeOptionsToConfig(options, config.Pdf, baseDirectory);
RunPdf.Exec(config.Pdf, new(), baseDirectory, outputFolder);
}

if (options.Serve && serveDirectory is not null)
{
RunServe.Exec(serveDirectory, options.Host, options.Port);
}
});
}

Expand Down
13 changes: 3 additions & 10 deletions src/docfx/Models/PdfCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ public override int Execute([NotNull] CommandContext context, [NotNull] PdfComma
{
return CommandHelper.Run(options, () =>
{
var Config = ParseOptions(options, out var BaseDirectory, out var OutputFolder);
RunPdf.Exec(Config, new(), BaseDirectory, OutputFolder);
var (config, baseDirectory) = CommandHelper.GetConfig<PdfConfig>(options.ConfigFile);
MergeOptionsToConfig(options, config.Item, baseDirectory);
RunPdf.Exec(config.Item, new(), baseDirectory, options.OutputFolder);
});
}

private static PdfJsonConfig ParseOptions(PdfCommandOptions options, out string baseDirectory, out string outputFolder)
{
(var config, baseDirectory) = CommandHelper.GetConfig<PdfConfig>(options.ConfigFile);
outputFolder = options.OutputFolder;
MergeOptionsToConfig(options, config.Item, baseDirectory);
return config.Item;
}

private static void MergeOptionsToConfig(PdfCommandOptions options, PdfJsonConfig config, string configDirectory)
{
BuildCommand.MergeOptionsToConfig(options, config, configDirectory);
Expand Down
4 changes: 2 additions & 2 deletions src/docfx/Models/ServeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ internal class Settings : CommandSettings
[CommandArgument(0, "[directory]")]
public string Folder { get; set; }

[Description("Specify the hostname of the hosted website [localhost]")]
[Description("Specify the hostname of the hosted website")]
[CommandOption("-n|--hostname")]
public string Host { get; set; }

[Description("Specify the port of the hosted website [8080]")]
[Description("Specify the port of the hosted website")]
[CommandOption("-p|--port")]
public int? Port { get; set; }
}
Expand Down
37 changes: 37 additions & 0 deletions test/docfx.Tests/CommandLineTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Xunit;

namespace Microsoft.DocAsCode.Tests;

public static class CommandLineTest
{
[Fact]
public static void PrintsVersion()
{
Assert.Equal(0, Program.Main(new[] { "-v" }));
Assert.Equal(0, Program.Main(new[] { "--version" }));
}

[Fact]
public static void PrintsHelp()
{
Assert.Equal(0, Program.Main(new[] { "-h" }));
Assert.Equal(0, Program.Main(new[] { "--help" }));
Assert.Equal(0, Program.Main(new[] { "build", "--help" }));
Assert.Equal(0, Program.Main(new[] { "serve", "--help" }));
Assert.Equal(0, Program.Main(new[] { "metadata", "--help" }));
Assert.Equal(0, Program.Main(new[] { "pdf", "--help" }));
Assert.Equal(0, Program.Main(new[] { "init", "--help" }));
Assert.Equal(0, Program.Main(new[] { "download", "--help" }));
Assert.Equal(0, Program.Main(new[] { "merge", "--help" }));
Assert.Equal(0, Program.Main(new[] { "template", "--help" }));
}

[Fact]
public static void FailForUnknownArgs()
{
Assert.Equal(-1, Program.Main(new[] { "--unknown" }));
}
}

0 comments on commit 95d113d

Please sign in to comment.