/// <summary> /// Validation logic that will determine if this command should be enabled for execution. /// </summary> /// <param name="result">The target model data that will be used to determine if this command should be enabled.</param> /// <returns>Boolean flag that will tell code factory to enable this command or disable it.</returns> public override async Task <bool> EnableCommandAsync(VsCSharpSource result) { //Result that determines if the the command is enabled and visible in the context menu for execution. bool isEnabled = false; try { if (!result.IsLoaded) { return(false); } if (!result.SourceCode.Classes.Any()) { return(false); } isEnabled = result.SourceCode.Classes.Any(c => ContractHelper.GetSubscriptions(c) != null); } catch (Exception unhandledError) { _logger.Error($"The following unhandled error occured while checking if the solution explorer C# document command {commandTitle} is enabled. ", unhandledError); isEnabled = false; } return(isEnabled); }
/// <summary> /// Code factory framework calls this method when the command has been executed. /// </summary> /// <param name="result">The code factory model that has generated and provided to the command to process.</param> public override async Task ExecuteCommandAsync(VsCSharpSource result) { try { var sourceCode = result.SourceCode; var classes = sourceCode.Classes; foreach (var csClass in classes) { if (!(sourceCode.GetModel(csClass.LookupPath) is CsClass currentClass)) { throw new CodeFactoryException("Cannot access class data cannot update subscriptions"); } var subscriptions = ContractHelper.GetSubscriptions(currentClass); if (subscriptions == null) { continue; } if (!subscriptions.Any()) { continue; } foreach (var subscription in subscriptions) { sourceCode = await UpdateSubscriptionAsync(subscription, currentClass, result); } } } catch (Exception unhandledError) { _logger.Error($"The following unhandled error occured while executing the solution explorer C# document command {commandTitle}. ", unhandledError); } }