Пример #1
0
        private async Task <CsSource> AddMethodMember(CsClass targetClass, CsMethod member, bool logging, bool cdf, bool cdfAspNet, string targetFilePath, NamespaceManager manager)
        {
            CsSource result     = null;
            string   sourceCode = null;

            if (cdfAspNet)
            {
                if (WebApiSupport.IsControllerAction(member))
                {
                    sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateControllerActionMethodSourceCode(member,
                                                                                                                        manager, true, true, CsSecurity.Public, logging, "_logger");
                }
                else
                {
                    sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateStandardMethodSourceCode(member,
                                                                                                                manager, true, true, CsSecurity.Public, logging, "_logger");
                }
            }
            else
            {
                if (cdf)
                {
                    sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateStandardMethodSourceCode(member,
                                                                                                                manager, true, true, CsSecurity.Public, logging, "_logger");
                }
                else
                {
                    if (logging)
                    {
                        sourceCode = CSharpSourceGenerationNetCommon.GenerateStandardMethodSourceCode(member,
                                                                                                      manager, true, true, CsSecurity.Public, true, "_logger");
                    }
                    else
                    {
                        sourceCode = CSharpSourceGenerationCommon.GenerateStandardMethodSourceCode(member,
                                                                                                   manager, true, true);
                    }
                }
            }

            if (string.IsNullOrEmpty(sourceCode))
            {
                throw new CodeFactoryException("Was not able to generate the source code for the member method.");
            }

            result = await targetClass.AddToEndAsync(targetFilePath, CsSourceFormatter.IndentCodeBlock(2, sourceCode));

            if (result == null)
            {
                throw new CodeFactoryException("Could not load the source code after adding the member.");
            }

            return(result);
        }
Пример #2
0
        private async Task <CsSource> AddProperty(CsClass targetClass, CsProperty member, string targetFilePath, NamespaceManager manager)
        {
            CsSource result = null;

            string sourceCode = CSharpSourceGenerationCommon.GenerateStandardPropertySourceCode(member, manager);

            if (string.IsNullOrEmpty(sourceCode))
            {
                throw new CodeFactoryException("Was not able to generate the source code for the member property.");
            }

            result = await targetClass.AddToEndAsync(targetFilePath, CsSourceFormatter.IndentCodeBlock(2, sourceCode));

            if (result == null)
            {
                throw new CodeFactoryException("Could not load the source code after adding the member.");
            }

            return(result);
        }
Пример #3
0
        private async Task <CsSource> AddEvent(CsClass targetClass, CsEvent member, bool isContractMember,
                                               string targetFilePath, NamespaceManager manager)
        {
            CsSource result          = null;
            CsClass  currentClass    = targetClass;
            string   eventSourceCode = null;

            string eventFieldName = null;

            if (isContractMember)
            {
                string fieldPrefix = "_";
                string fieldSuffix = "Handler";

                eventFieldName = $"{fieldPrefix}{member.Name.ConvertToCamelCase()}{fieldSuffix}";

                if (!currentClass.Fields.Any(f => f.Name == eventFieldName))
                {
                    string fieldSource = $"private {member.EventType.CSharpFormatTypeName(manager)} {eventFieldName};";

                    result = await currentClass.AddToBeginningAsync(targetFilePath,
                                                                    CsSourceFormatter.IndentCodeBlock(2, fieldSource));

                    currentClass = result.GetModel(currentClass.LookupPath) as CsClass;
                    if (currentClass == null)
                    {
                        throw new CodeFactoryException("Could not load class data cannot update members.");
                    }
                }

                eventSourceCode = CSharpSourceGenerationCommon.GenerateAddRemoveEvent(member, manager,
                                                                                      CsSecurity.Public, fieldPrefix, fieldSuffix);
            }
            else
            {
                eventSourceCode = CSharpSourceGenerationCommon.GenerateStandardEventSourceCode(member, manager);
            }

            if (string.IsNullOrEmpty(eventSourceCode))
            {
                throw new CodeFactoryException("Could not generate the event source code.");
            }

            result = await currentClass.AddToEndAsync(targetFilePath, CsSourceFormatter.IndentCodeBlock(2, eventSourceCode));

            currentClass = result.GetModel(currentClass.LookupPath) as CsClass;
            if (currentClass == null)
            {
                throw new CodeFactoryException("Could not load class data cannot update members.");
            }

            string raiseEventSourceCode = null;

            if (isContractMember)
            {
                if (string.IsNullOrEmpty(eventFieldName))
                {
                    throw new CodeFactoryException("Event field name was not provided cannot create event handler");
                }

                raiseEventSourceCode = CSharpSourceGenerationCommon.GenerateStandardRaiseEventMethod(member, manager, member.Name,
                                                                                                     CsSecurity.Protected, eventFieldName);
            }
            else
            {
                raiseEventSourceCode = CSharpSourceGenerationCommon.GenerateStandardRaiseEventMethod(member, manager, member.Name,
                                                                                                     CsSecurity.Protected);
            }

            result = await currentClass.AddToEndAsync(targetFilePath,
                                                      CsSourceFormatter.IndentCodeBlock(2, raiseEventSourceCode));

            return(result);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="targetClass"></param>
        /// <param name="sourceCode"></param>
        /// <param name="members"></param>
        /// <param name="logging"></param>
        /// <param name="cdf"></param>
        /// <param name="cdfAspnet"></param>
        /// <param name="isContract"></param>
        /// <returns></returns>
        private async Task <CsSource> UpdateFileAsync(CsClass targetClass, CsSource sourceCode, IEnumerable <CsMember> members, bool logging, bool cdf, bool cdfAspnet, bool isContract)
        {
            if (targetClass == null)
            {
                throw new CodeFactoryException("Cannot access class data cannot add members");
            }
            if (sourceCode == null)
            {
                throw new CodeFactoryException("Cannot access the classes source code cannot add members.");
            }
            if (members == null)
            {
                return(sourceCode);
            }
            if (!members.Any())
            {
                return(sourceCode);
            }

            CsSource updatedSourceCode = await sourceCode.AddMissingNamespaces(members, targetClass.Namespace);

            CsClass updatedClass = targetClass;



            if (logging)
            {
                updatedSourceCode = await updatedSourceCode.AddUsingStatementAsync(NetConstants.MicrosoftLoggerNamespace);

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

                if (!updatedClass.Fields.Any(f => f.Name == NetConstants.DefaultClassLoggerName))
                {
                    var formatter = new SourceFormatter();
                    formatter.AppendCodeLine(0, "///<summary>");
                    formatter.AppendCodeLine(0, "///Logger used to manage logging for this class.");
                    formatter.AppendCodeLine(0, "///</summary>");
                    formatter.AppendCodeLine(0, $"private ILogger {NetConstants.DefaultClassLoggerName};");
                    updatedSourceCode =
                        await updatedClass.AddToBeginningAsync(
                            CsSourceFormatter.IndentCodeBlock(2, formatter.ReturnSource()));
                }
            }

            if (cdf)
            {
                updatedSourceCode = await updatedSourceCode.AddUsingStatementAsync(CommonDeliveryFrameworkConstants
                                                                                   .CommonDeliveryFrameworkNamespace);
            }

            if (cdfAspnet)
            {
                updatedSourceCode = await updatedSourceCode.AddUsingStatementAsync(CommonDeliveryFrameworkConstants
                                                                                   .CommonDeliveryFrameworkNetAspNetNamespace);
            }

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

            updatedSourceCode = await UpdateMembersAsync(updatedClass, members, logging, cdf, cdfAspnet,
                                                         updatedSourceCode.SourceDocument, isContract, sourceCode.LoadNamespaceManager(updatedClass.Namespace));

            return(updatedSourceCode);
        }