Skip to content

Commit

Permalink
fix: add --serve to default command (#8684)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Apr 25, 2023
1 parent 1284075 commit 30be6fe
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 73 deletions.
9 changes: 0 additions & 9 deletions src/Microsoft.DocAsCode.App/Config/BuildJsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,12 @@ internal class BuildJsonConfig
[JsonProperty("postProcessors")]
public ListWithStringFallback PostProcessors { get; set; } = new ListWithStringFallback();

[JsonProperty("serve")]
public bool? Serve { get; set; }

[JsonProperty("debug")]
public bool? EnableDebugMode { get; set; }

[JsonProperty("debugOutput")]
public string OutputFolderForDebugFiles { get; set; }

[JsonProperty("host")]
public string Host { get; set; }

[JsonProperty("port")]
public string Port { get; set; }

[JsonProperty("exportRawModel")]
public bool? ExportRawModel { get; set; }

Expand Down
5 changes: 2 additions & 3 deletions src/Microsoft.DocAsCode.App/Config/PdfJsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class PdfJsonConfig : BuildJsonConfig
public string Name { get; set; }

[JsonProperty("host")]
public new string Host { get; set; }
public string Host { get; set; }

[JsonProperty("locale")]
public string Locale { get; set; }
Expand Down Expand Up @@ -77,5 +77,4 @@ internal class PdfJsonConfig : BuildJsonConfig
/// </summary>
[JsonProperty("noStdin")]
public bool NoInputStreamArgs { get; set; }

}
}
7 changes: 2 additions & 5 deletions src/Microsoft.DocAsCode.App/RunBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.DocAsCode;

internal static class RunBuild
{
public static void Exec(BuildJsonConfig config, BuildOptions options, string configDirectory, string outputDirectory = null)
public static string Exec(BuildJsonConfig config, BuildOptions options, string configDirectory, string outputDirectory = null)
{
if (config.Templates == null || config.Templates.Count == 0)
{
Expand Down Expand Up @@ -53,10 +53,7 @@ public static void Exec(BuildJsonConfig config, BuildOptions options, string con
templateManager.ProcessTheme(outputFolder, true);
// TODO: SEARCH DATA

if (config?.Serve ?? false)
{
RunServe.Exec(outputFolder, config.Host, config.Port);
}
EnvironmentContext.Clean();
return outputFolder;
}
}
6 changes: 0 additions & 6 deletions src/Microsoft.DocAsCode.App/RunPdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public static void Exec(PdfJsonConfig config, BuildOptions buildOptions, string
var wkhtmltopdfFilePath = config.Wkhtmltopdf?.FilePath is null ? null : Path.Combine(baseDirectory, config.Wkhtmltopdf.FilePath);
ConvertWrapper.PrerequisiteCheck(wkhtmltopdfFilePath);

if (config.Serve == true)
{
Logger.LogWarning("--serve is not supported in pdf command, ignored");
config.Serve = false;
}

if (config.Templates == null || config.Templates.Count == 0)
{
config.Templates = new ListWithStringFallback(new List<string> { "pdf.default" });
Expand Down
7 changes: 3 additions & 4 deletions src/Microsoft.DocAsCode.App/RunServe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ namespace Microsoft.DocAsCode;

internal static class RunServe
{
public static void Exec(string folder, string host, string port)
public static void Exec(string folder, string host, int? port)
{
if (string.IsNullOrEmpty(folder))
folder = Directory.GetCurrentDirectory();

folder = Path.GetFullPath(folder);
host = string.IsNullOrWhiteSpace(host) ? "localhost" : host;
port = string.IsNullOrWhiteSpace(port) ? "8080" : port;
var url = $"http://{host}:{port}";

var url = $"http://{host ?? "localhost"}:{port ?? 8080}";

if (!Directory.Exists(folder))
{
Expand Down
17 changes: 4 additions & 13 deletions src/docfx/Models/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public override int Execute(CommandContext context, BuildCommandOptions settings
return CommandHelper.Run(settings, () =>
{
var config = ParseOptions(settings, out var baseDirectory, out var outputFolder);
RunBuild.Exec(config, new(), baseDirectory, outputFolder);
var serveDirectory = RunBuild.Exec(config, new(), baseDirectory, outputFolder);

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

Expand Down Expand Up @@ -59,18 +62,6 @@ internal static void MergeOptionsToConfig(BuildCommandOptions options, BuildJson
.Distinct());
}

if (options.Serve)
{
config.Serve = options.Serve;
}
if (options.Host != null)
{
config.Host = options.Host;
}
if (options.Port.HasValue)
{
config.Port = options.Port.Value.ToString();
}
config.EnableDebugMode |= options.EnableDebugMode;
config.ExportRawModel |= options.ExportRawModel;
config.ExportViewModel |= options.ExportViewModel;
Expand Down
17 changes: 16 additions & 1 deletion src/docfx/Models/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ internal class Options : LogOptions
[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; }
Expand All @@ -39,13 +51,16 @@ public override int Execute(CommandContext context, Options options)
{
var (config, baseDirectory) = CommandHelper.GetConfig<Config>(options.Config);
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)
RunBuild.Exec(config.Build, new(), baseDirectory, outputFolder);
serveDirectory = RunBuild.Exec(config.Build, new(), baseDirectory, outputFolder);
if (config.Pdf is not null)
RunPdf.Exec(config.Pdf, new(), baseDirectory, outputFolder);
if (options.Serve && serveDirectory is not null)
RunServe.Exec(serveDirectory, options.Host, options.Port);
});
}

Expand Down
29 changes: 20 additions & 9 deletions src/docfx/Models/ServeCommand.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console.Cli;

namespace Microsoft.DocAsCode;

internal class ServeCommand : Command<ServeCommandOptions>
internal class ServeCommand : Command<ServeCommand.Settings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] ServeCommandOptions options)
[Description("Host a local static website")]
internal class Settings : CommandSettings
{
return CommandHelper.Run(() =>
{
RunServe.Exec(
options.Folder,
options.Host,
options.Port.HasValue ? options.Port.Value.ToString() : null);
});
[Description("Path to the directory to serve")]
[CommandArgument(0, "[directory]")]
public string Folder { 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; }
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings options)
{
return CommandHelper.Run(() => RunServe.Exec(options.Folder, options.Host, options.Port));
}
}
23 changes: 0 additions & 23 deletions src/docfx/Models/ServeCommandOptions.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/docfx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal static int Main(string[] args)
app.Configure(config =>
{
config.SetApplicationName("docfx");
config.UseStrictParsing();

config.AddCommand<InitCommand>("init");
config.AddCommand<BuildCommand>("build");
Expand Down

0 comments on commit 30be6fe

Please sign in to comment.