diff --git a/src/Microsoft.TemplateEngine.Cli/Commands/BaseCommand.cs b/src/Microsoft.TemplateEngine.Cli/Commands/BaseCommand.cs index 60639de334..25256b757a 100644 --- a/src/Microsoft.TemplateEngine.Cli/Commands/BaseCommand.cs +++ b/src/Microsoft.TemplateEngine.Cli/Commands/BaseCommand.cs @@ -19,67 +19,25 @@ namespace Microsoft.TemplateEngine.Cli.Commands { internal abstract class BaseCommand : Command { - private readonly ITemplateEngineHost _host; + private readonly Func _hostBuilder; + private readonly Func _telemetryLoggerBuilder; protected BaseCommand( - ITemplateEngineHost host, - ITelemetryLogger logger, + Func hostBuilder, + Func telemetryLoggerBuilder, NewCommandCallbacks callbacks, string name, string description) : base(name, description) { - _host = host; - TelemetryLogger = logger; + _hostBuilder = hostBuilder; + _telemetryLoggerBuilder = telemetryLoggerBuilder; Callbacks = callbacks; - this.AddOption(DebugCustomSettingsLocationOption); - this.AddOption(DebugVirtualizeSettingsOption); - this.AddOption(DebugAttachOption); - this.AddOption(DebugReinitOption); - this.AddOption(DebugRebuildCacheOption); - this.AddOption(DebugShowConfigOption); + } protected BaseCommand(BaseCommand baseCommand, string name, string description) - : this(baseCommand._host, baseCommand.TelemetryLogger, baseCommand.Callbacks, name, description) { } - - internal Option DebugCustomSettingsLocationOption { get; } = new("--debug:custom-hive") - { - Description = SymbolStrings.Option_Debug_CustomSettings, - IsHidden = true - }; - - internal Option DebugVirtualizeSettingsOption { get; } = new("--debug:ephemeral-hive") - { - Description = SymbolStrings.Option_Debug_VirtualSettings, - IsHidden = true - }; - - internal Option DebugAttachOption { get; } = new("--debug:attach") - { - Description = SymbolStrings.Option_Debug_Attach, - IsHidden = true - }; - - internal Option DebugReinitOption { get; } = new("--debug:reinit") - { - Description = SymbolStrings.Option_Debug_Reinit, - IsHidden = true - }; - - internal Option DebugRebuildCacheOption { get; } = new(new[] { "--debug:rebuild-cache", "--debug:rebuildcache" }) - { - Description = SymbolStrings.Option_Debug_RebuildCache, - IsHidden = true - }; - - internal Option DebugShowConfigOption { get; } = new(new[] { "--debug:show-config", "--debug:showconfig" }) - { - Description = SymbolStrings.Option_Debug_ShowConfig, - IsHidden = true - }; - - internal ITelemetryLogger TelemetryLogger { get; } + : this(baseCommand._hostBuilder, baseCommand._telemetryLoggerBuilder, baseCommand.Callbacks, name, description) { } internal NewCommandCallbacks Callbacks { get; } @@ -90,13 +48,18 @@ protected IEngineEnvironmentSettings CreateEnvironmentSettings(GlobalArgs args, //for template instantiaton it has to be reparsed string? outputPath = ParseOutputOption(parseResult); IEngineEnvironmentSettings environmentSettings = new EngineEnvironmentSettings( - new CliTemplateEngineHost(_host, outputPath), + new CliTemplateEngineHost(_hostBuilder(parseResult), outputPath), settingsLocation: args.DebugCustomSettingsLocation, virtualizeSettings: args.DebugVirtualizeSettings, environment: new CliEnvironment()); return environmentSettings; } + protected ITelemetryLogger CreateTelemetryLogger(ParseResult parseResult) + { + return _telemetryLoggerBuilder(parseResult); + } + private static string? ParseOutputOption(ParseResult commandParseResult) { Option outputOption = SharedOptionsFactory.CreateOutputOption(); @@ -117,12 +80,12 @@ protected IEngineEnvironmentSettings CreateEnvironmentSettings(GlobalArgs args, internal abstract class BaseCommand : BaseCommand, ICommandHandler where TArgs : GlobalArgs { internal BaseCommand( - ITemplateEngineHost host, - ITelemetryLogger logger, + Func hostBuilder, + Func telemetryLoggerBuilder, NewCommandCallbacks callbacks, string name, string description) - : base(host, logger, callbacks, name, description) + : base(hostBuilder, telemetryLoggerBuilder, callbacks, name, description) { this.Handler = this; } @@ -135,6 +98,7 @@ public async Task InvokeAsync(InvocationContext context) { TArgs args = ParseContext(context.ParseResult); IEngineEnvironmentSettings environmentSettings = CreateEnvironmentSettings(args, context.ParseResult); + ITelemetryLogger telemetryLogger = CreateTelemetryLogger(context.ParseResult); CancellationToken cancellationToken = context.GetCancellationToken(); @@ -143,7 +107,7 @@ public async Task InvokeAsync(InvocationContext context) using (Timing.Over(environmentSettings.Host.Logger, "Execute")) { await HandleGlobalOptionsAsync(args, environmentSettings, cancellationToken).ConfigureAwait(false); - return (int)await ExecuteAsync(args, environmentSettings, context).ConfigureAwait(false); + return (int)await ExecuteAsync(args, environmentSettings, telemetryLogger, context).ConfigureAwait(false); } } catch (Exception ex) @@ -193,7 +157,7 @@ protected internal virtual IEnumerable GetCompletions(Completion return base.GetCompletions(context); } - protected abstract Task ExecuteAsync(TArgs args, IEngineEnvironmentSettings environmentSettings, InvocationContext context); + protected abstract Task ExecuteAsync(TArgs args, IEngineEnvironmentSettings environmentSettings, ITelemetryLogger telemetryLogger, InvocationContext context); protected abstract TArgs ParseContext(ParseResult parseResult); diff --git a/src/Microsoft.TemplateEngine.Cli/Commands/GlobalArgs.cs b/src/Microsoft.TemplateEngine.Cli/Commands/GlobalArgs.cs index 18662d1417..436873c6eb 100644 --- a/src/Microsoft.TemplateEngine.Cli/Commands/GlobalArgs.cs +++ b/src/Microsoft.TemplateEngine.Cli/Commands/GlobalArgs.cs @@ -11,12 +11,12 @@ internal class GlobalArgs { public GlobalArgs(BaseCommand command, ParseResult parseResult) { - DebugCustomSettingsLocation = parseResult.GetValueForOption(command.DebugCustomSettingsLocationOption); - DebugVirtualizeSettings = parseResult.GetValueForOption(command.DebugVirtualizeSettingsOption); - DebugAttach = parseResult.GetValueForOption(command.DebugAttachOption); - DebugReinit = parseResult.GetValueForOption(command.DebugReinitOption); - DebugRebuildCache = parseResult.GetValueForOption(command.DebugRebuildCacheOption); - DebugShowConfig = parseResult.GetValueForOption(command.DebugShowConfigOption); + DebugCustomSettingsLocation = parseResult.GetValueForOption(NewCommand.DebugCustomSettingsLocationOption); + DebugVirtualizeSettings = parseResult.GetValueForOption(NewCommand.DebugVirtualizeSettingsOption); + DebugAttach = parseResult.GetValueForOption(NewCommand.DebugAttachOption); + DebugReinit = parseResult.GetValueForOption(NewCommand.DebugReinitOption); + DebugRebuildCache = parseResult.GetValueForOption(NewCommand.DebugRebuildCacheOption); + DebugShowConfig = parseResult.GetValueForOption(NewCommand.DebugShowConfigOption); CommandName = parseResult.GetNewCommandName(); ParseResult = parseResult; } diff --git a/src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.Legacy.cs b/src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.Legacy.cs index 3eff50ef1f..722994f010 100644 --- a/src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.Legacy.cs +++ b/src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.Legacy.cs @@ -35,6 +35,21 @@ internal partial class NewCommand : BaseCommand IsHidden = true }; + internal IEnumerable