예제 #1
0
        /// <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 references = await result.GetHostProjectReferencesAsync();

                if (!references.Any())
                {
                    return;
                }

                bool hasLogging   = references.Any(r => r.Name == NetConstants.MicrosoftExtensionsLoggingLibraryName);
                bool hasCDF       = references.Any(r => r.Name == CommonDeliveryFrameworkConstants.CommonDeliveryFrameworkAssemblyName);
                bool hasCDFAspnet = references.Any(r =>
                                                   r.Name == CommonDeliveryFrameworkConstants.CommonDeliveryFrameworkNetAspNetAssemblyName);

                var sourceCode = result.SourceCode;


                foreach (var sourceCodeClass in sourceCode.Classes)
                {
                    var missingMembers = sourceCodeClass.MissingInterfaceMembers();

                    if (missingMembers == null)
                    {
                        continue;
                    }
                    if (!missingMembers.Any())
                    {
                        continue;
                    }

                    IEnumerable <CsMember> baseMembers = null;

                    var updatedClass = sourceCodeClass;

                    var contractMembers = ContractHelper.MissingContractMembers(sourceCodeClass, missingMembers);

                    baseMembers = contractMembers != null?ContractHelper.MissingBaseMembers(missingMembers, contractMembers) : missingMembers;

                    if (contractMembers != null)
                    {
                        sourceCode = await ContractHelper.GetContractSourceFileAsync(result, sourceCodeClass);

                        if (sourceCode == null)
                        {
                            throw new CodeFactoryException("Cannot load the source code for the contract to support this class.");
                        }

                        updatedClass = sourceCode.GetModel(updatedClass.LookupPath) as CsClass;
                        if (updatedClass == null)
                        {
                            throw new CodeFactoryException("Cannot get class data to add members.");
                        }

                        sourceCode = await UpdateFileAsync(updatedClass, sourceCode, contractMembers,
                                                           hasLogging, hasCDF, hasCDFAspnet, true);

                        if (baseMembers != null)
                        {
                            sourceCode = await ContractHelper.GetContractBaseFileAsync(result, sourceCodeClass);
                        }

                        if (sourceCode == null)
                        {
                            throw new CodeFactoryException("Cannot load the source code for the class.");
                        }

                        updatedClass = sourceCode.GetModel(updatedClass.LookupPath) as CsClass;
                        if (updatedClass == null)
                        {
                            throw new CodeFactoryException("Cannot get class data to add members.");
                        }
                    }

                    if (baseMembers != null)
                    {
                        sourceCode = await UpdateFileAsync(updatedClass, sourceCode, baseMembers,
                                                           hasLogging, hasCDF, hasCDFAspnet, false);
                    }
                }
            }
            catch (Exception unhandledError)
            {
                _logger.Error($"The following unhandled error occured while executing the solution explorer C# document command {commandTitle}. ",
                              unhandledError);
            }
        }