Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system-command-line] replaced host and telemetry logger parameters to NewCommand with delegates #4254

Merged
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
76 changes: 20 additions & 56 deletions src/Microsoft.TemplateEngine.Cli/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,25 @@ namespace Microsoft.TemplateEngine.Cli.Commands
{
internal abstract class BaseCommand : Command
{
private readonly ITemplateEngineHost _host;
private readonly Func<ParseResult, ITemplateEngineHost> _hostBuilder;
private readonly Func<ParseResult, ITelemetryLogger> _telemetryLoggerBuilder;

protected BaseCommand(
ITemplateEngineHost host,
ITelemetryLogger logger,
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> 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<string?> DebugCustomSettingsLocationOption { get; } = new("--debug:custom-hive")
{
Description = SymbolStrings.Option_Debug_CustomSettings,
IsHidden = true
};

internal Option<bool> DebugVirtualizeSettingsOption { get; } = new("--debug:ephemeral-hive")
{
Description = SymbolStrings.Option_Debug_VirtualSettings,
IsHidden = true
};

internal Option<bool> DebugAttachOption { get; } = new("--debug:attach")
{
Description = SymbolStrings.Option_Debug_Attach,
IsHidden = true
};

internal Option<bool> DebugReinitOption { get; } = new("--debug:reinit")
{
Description = SymbolStrings.Option_Debug_Reinit,
IsHidden = true
};

internal Option<bool> DebugRebuildCacheOption { get; } = new(new[] { "--debug:rebuild-cache", "--debug:rebuildcache" })
{
Description = SymbolStrings.Option_Debug_RebuildCache,
IsHidden = true
};

internal Option<bool> 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; }

Expand All @@ -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)
bekir-ozturk marked this conversation as resolved.
Show resolved Hide resolved
{
return _telemetryLoggerBuilder(parseResult);
}

private static string? ParseOutputOption(ParseResult commandParseResult)
{
Option<string> outputOption = SharedOptionsFactory.CreateOutputOption();
Expand All @@ -117,12 +80,12 @@ protected IEngineEnvironmentSettings CreateEnvironmentSettings(GlobalArgs args,
internal abstract class BaseCommand<TArgs> : BaseCommand, ICommandHandler where TArgs : GlobalArgs
{
internal BaseCommand(
ITemplateEngineHost host,
ITelemetryLogger logger,
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder,
NewCommandCallbacks callbacks,
string name,
string description)
: base(host, logger, callbacks, name, description)
: base(hostBuilder, telemetryLoggerBuilder, callbacks, name, description)
{
this.Handler = this;
}
Expand All @@ -135,6 +98,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
{
TArgs args = ParseContext(context.ParseResult);
IEngineEnvironmentSettings environmentSettings = CreateEnvironmentSettings(args, context.ParseResult);
ITelemetryLogger telemetryLogger = CreateTelemetryLogger(context.ParseResult);

CancellationToken cancellationToken = context.GetCancellationToken();

Expand All @@ -143,7 +107,7 @@ public async Task<int> 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)
Expand Down Expand Up @@ -193,7 +157,7 @@ protected internal virtual IEnumerable<CompletionItem> GetCompletions(Completion
return base.GetCompletions(context);
}

protected abstract Task<NewCommandStatus> ExecuteAsync(TArgs args, IEngineEnvironmentSettings environmentSettings, InvocationContext context);
protected abstract Task<NewCommandStatus> ExecuteAsync(TArgs args, IEngineEnvironmentSettings environmentSettings, ITelemetryLogger telemetryLogger, InvocationContext context);

protected abstract TArgs ParseContext(ParseResult parseResult);

Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.TemplateEngine.Cli/Commands/GlobalArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
33 changes: 24 additions & 9 deletions src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.Legacy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ internal partial class NewCommand : BaseCommand<NewCommandArgs>
IsHidden = true
};

internal IEnumerable<Option> LegacyOptions
{
get
{
yield return InteractiveOption;
yield return AddSourceOption;
yield return ColumnsAllOption;
yield return ColumnsOption;
foreach (Option filter in LegacyFilters.Values)
{
yield return filter;
}
}
}

internal Option<bool> InteractiveOption { get; } = SharedOptionsFactory.CreateInteractiveOption().AsHidden();

internal Option<IReadOnlyList<string>> AddSourceOption { get; } = SharedOptionsFactory.CreateAddSourceOption().AsHidden().DisableAllowMultipleArgumentsPerToken();
Expand Down Expand Up @@ -126,7 +141,7 @@ internal void AddNoLegacyUsageValidators(Command command, params Symbol[] except
return null;
}

private void BuildLegacySymbols(ITemplateEngineHost host, ITelemetryLogger telemetryLogger, NewCommandCallbacks callbacks)
private void BuildLegacySymbols(Func<ParseResult, ITemplateEngineHost> hostBuilder, Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder, NewCommandCallbacks callbacks)
{
this.AddArgument(ShortNameArgument);
this.AddArgument(RemainingArguments);
Expand All @@ -147,14 +162,14 @@ private void BuildLegacySymbols(ITemplateEngineHost host, ITelemetryLogger telem

this.TreatUnmatchedTokensAsErrors = true;

this.Add(new LegacyInstallCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacyUninstallCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacyUpdateCheckCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacyUpdateApplyCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacySearchCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacyListCommand(this, host, telemetryLogger, callbacks));
this.Add(new LegacyAliasAddCommand(host, telemetryLogger, callbacks));
this.Add(new LegacyAliasShowCommand(host, telemetryLogger, callbacks));
this.Add(new LegacyInstallCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyUninstallCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyUpdateCheckCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyUpdateApplyCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacySearchCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyListCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyAliasAddCommand(hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new LegacyAliasShowCommand(hostBuilder, telemetryLoggerBuilder, callbacks));
}
}
}
Expand Down
76 changes: 62 additions & 14 deletions src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable enable

using System.CommandLine;
using System.CommandLine.Completions;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
Expand All @@ -14,25 +15,68 @@ internal partial class NewCommand : BaseCommand<NewCommandArgs>, ICustomHelp
{
internal NewCommand(
string commandName,
ITemplateEngineHost host,
ITelemetryLogger telemetryLogger,
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder,
NewCommandCallbacks callbacks)
: base(host, telemetryLogger, callbacks, commandName, SymbolStrings.Command_New_Description)
: base(hostBuilder, telemetryLoggerBuilder, callbacks, commandName, SymbolStrings.Command_New_Description)
{
this.TreatUnmatchedTokensAsErrors = true;

//it is important that legacy commands are built before non-legacy, as non legacy commands are building validators that rely on legacy stuff
BuildLegacySymbols(host, telemetryLogger, callbacks);

this.Add(new InstantiateCommand(host, telemetryLogger, callbacks));
this.Add(new InstallCommand(this, host, telemetryLogger, callbacks));
this.Add(new UninstallCommand(this, host, telemetryLogger, callbacks));
this.Add(new UpdateCommand(this, host, telemetryLogger, callbacks));
this.Add(new SearchCommand(this, host, telemetryLogger, callbacks));
this.Add(new ListCommand(this, host, telemetryLogger, callbacks));
this.Add(new AliasCommand(host, telemetryLogger, callbacks));
BuildLegacySymbols(hostBuilder, telemetryLoggerBuilder, callbacks);

this.Add(new InstantiateCommand(hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new InstallCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new UninstallCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new UpdateCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new SearchCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new ListCommand(this, hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new AliasCommand(hostBuilder, telemetryLoggerBuilder, callbacks));

this.AddGlobalOption(DebugCustomSettingsLocationOption);
this.AddGlobalOption(DebugVirtualizeSettingsOption);
this.AddGlobalOption(DebugAttachOption);
this.AddGlobalOption(DebugReinitOption);
this.AddGlobalOption(DebugRebuildCacheOption);
this.AddGlobalOption(DebugShowConfigOption);
}

internal static Option<string?> DebugCustomSettingsLocationOption { get; } = new("--debug:custom-hive")
{
Description = SymbolStrings.Option_Debug_CustomSettings,
IsHidden = true
};

internal static Option<bool> DebugVirtualizeSettingsOption { get; } = new(new[] { "--debug:ephemeral-hive", "--debug:virtual-hive" })
{
Description = SymbolStrings.Option_Debug_VirtualSettings,
IsHidden = true
};

internal static Option<bool> DebugAttachOption { get; } = new("--debug:attach")
{
Description = SymbolStrings.Option_Debug_Attach,
IsHidden = true
};

internal static Option<bool> DebugReinitOption { get; } = new("--debug:reinit")
{
Description = SymbolStrings.Option_Debug_Reinit,
IsHidden = true
};

internal static Option<bool> DebugRebuildCacheOption { get; } = new(new[] { "--debug:rebuild-cache", "--debug:rebuildcache" })
{
Description = SymbolStrings.Option_Debug_RebuildCache,
IsHidden = true
};

internal static Option<bool> DebugShowConfigOption { get; } = new(new[] { "--debug:show-config", "--debug:showconfig" })
{
Description = SymbolStrings.Option_Debug_ShowConfig,
IsHidden = true
};

protected internal override IEnumerable<CompletionItem> GetCompletions(CompletionContext context, IEngineEnvironmentSettings environmentSettings)
{
if (context is not TextCompletionContext textCompletionContext)
Expand All @@ -53,11 +97,15 @@ protected internal override IEnumerable<CompletionItem> GetCompletions(Completio

}

protected override Task<NewCommandStatus> ExecuteAsync(NewCommandArgs args, IEngineEnvironmentSettings environmentSettings, InvocationContext context)
protected override Task<NewCommandStatus> ExecuteAsync(
NewCommandArgs args,
IEngineEnvironmentSettings environmentSettings,
ITelemetryLogger telemetryLogger,
InvocationContext context)
{
InstantiateCommand command = InstantiateCommand.FromNewCommand(this);
ParseResult reparseResult = ParserFactory.CreateParser(command).Parse(args.Tokens);
return command.ExecuteAsync(reparseResult, environmentSettings, context);
return command.ExecuteAsync(reparseResult, environmentSettings, telemetryLogger, context);
}

protected override NewCommandArgs ParseContext(ParseResult parseResult) => new(this, parseResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

#nullable enable

using System.CommandLine.Parsing;
using Microsoft.TemplateEngine.Abstractions;

namespace Microsoft.TemplateEngine.Cli.Commands
{
internal class AliasAddCommand : BaseAliasAddCommand
{
internal AliasAddCommand(ITemplateEngineHost host, ITelemetryLogger logger, NewCommandCallbacks callbacks) : base(host, logger, callbacks, "add")
internal AliasAddCommand(
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder,
NewCommandCallbacks callbacks)
: base(hostBuilder, telemetryLoggerBuilder, callbacks, "add")
{
IsHidden = true;
}
Expand Down
16 changes: 10 additions & 6 deletions src/Microsoft.TemplateEngine.Cli/Commands/alias/AliasCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ namespace Microsoft.TemplateEngine.Cli.Commands
internal class AliasCommand : BaseCommand<AliasCommandArgs>
{
internal AliasCommand(
ITemplateEngineHost host,
ITelemetryLogger telemetryLogger,
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder,
NewCommandCallbacks callbacks)
: base(host, telemetryLogger, callbacks, "alias", SymbolStrings.Command_Alias_Description)
: base(hostBuilder, telemetryLoggerBuilder, callbacks, "alias", SymbolStrings.Command_Alias_Description)
{
IsHidden = true;
this.Add(new AliasAddCommand(host, telemetryLogger, callbacks));
this.Add(new AliasShowCommand(host, telemetryLogger, callbacks));
this.Add(new AliasAddCommand(hostBuilder, telemetryLoggerBuilder, callbacks));
this.Add(new AliasShowCommand(hostBuilder, telemetryLoggerBuilder, callbacks));
}

protected override Task<NewCommandStatus> ExecuteAsync(AliasCommandArgs args, IEngineEnvironmentSettings environmentSettings, InvocationContext context) => throw new NotImplementedException();
protected override Task<NewCommandStatus> ExecuteAsync(
AliasCommandArgs args,
IEngineEnvironmentSettings environmentSettings,
ITelemetryLogger telemetryLogger,
InvocationContext context) => throw new NotImplementedException();

protected override AliasCommandArgs ParseContext(ParseResult parseResult) => new(this, parseResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

#nullable enable

using System.CommandLine.Parsing;
using Microsoft.TemplateEngine.Abstractions;

namespace Microsoft.TemplateEngine.Cli.Commands
{
internal class AliasShowCommand : BaseAliasShowCommand
{
internal AliasShowCommand(ITemplateEngineHost host, ITelemetryLogger logger, NewCommandCallbacks callbacks) : base(host, logger, callbacks, "show")
internal AliasShowCommand(
Func<ParseResult, ITemplateEngineHost> hostBuilder,
Func<ParseResult, ITelemetryLogger> telemetryLoggerBuilder,
NewCommandCallbacks callbacks)
: base(hostBuilder, telemetryLoggerBuilder, callbacks, "show")
{
IsHidden = true;
}
Expand Down
Loading