diff --git a/src/Standard/NDF/CodeFactory.Automation.Standard.NDF/RegisterTransientServices.cs b/src/Standard/NDF/CodeFactory.Automation.Standard.NDF/RegisterTransientServices.cs new file mode 100644 index 0000000..36ec48b --- /dev/null +++ b/src/Standard/NDF/CodeFactory.Automation.Standard.NDF/RegisterTransientServices.cs @@ -0,0 +1,97 @@ +using CodeFactory.WinVs; +using CodeFactory.WinVs.Commands; +using CodeFactory.WinVs.Commands.SolutionExplorer; +using CodeFactory.WinVs.Logging; +using CodeFactory.WinVs.Models.CSharp; +using CodeFactory.WinVs.Models.CSharp.Builder; +using CodeFactory.WinVs.Models.ProjectSystem; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CodeFactory.Automation.Standard.NDF.Logic; + +namespace CodeFactory.Automation.Standard.NDF +{ + /// + /// Code factory command for automation of a project when selected from solution explorer. + /// + public class RegisterTransientServices : ProjectCommandBase + { + private static readonly string commandTitle = "Register Transient Services"; + private static readonly string commandDescription = "Registers Transient classes with a NDF dependency injection loader for a target project."; + +#pragma warning disable CS1998 + + /// + public RegisterTransientServices(ILogger logger, IVsActions vsActions) : base(logger, vsActions, commandTitle, commandDescription) + { + //Intentionally blank + } +#pragma warning disable CS1998 + + #region External Configuration + + /// + /// The fully qualified name of the command to be used with configuration. + /// + public static string Type = typeof(RegisterTransientServices).FullName; + + /// + /// Loads the external configuration definition for this command. + /// + /// Will return the command configuration or null if this command does not support external configurations. + public override ConfigCommand LoadExternalConfigDefinition() + { + return null; + } + #endregion + + #region Overrides of VsCommandBase + + /// + /// Validation logic that will determine if this command should be enabled for execution. + /// + /// The target model data that will be used to determine if this command should be enabled. + /// Boolean flag that will tell code factory to enable this command or disable it. + public override async Task EnableCommandAsync(VsProject result) + { + //Result that determines if the command is enabled and visible in the context menu for execution. + bool isEnabled = false; + + try + { + isEnabled = await result.CanRegisterTransientClassesAsync(); + } + catch (Exception unhandledError) + { + _logger.Error($"The following unhandled error occurred while checking if the solution explorer project command {commandTitle} is enabled. ", + unhandledError); + isEnabled = false; + } + + return isEnabled; + } + + /// + /// Code factory framework calls this method when the command has been executed. + /// + /// The code factory model that has generated and provided to the command to process. + public override async Task ExecuteCommandAsync(VsProject result) + { + try + { + await VisualStudioActions.RegisterTransientClassesAsync(result); + } + catch (Exception unhandledError) + { + _logger.Error($"The following unhandled error occurred while executing the solution explorer project command {commandTitle}. ", + unhandledError); + + } + } + + #endregion + } +}