Example #1
0
        public GeneratedCodeInfo GenerateCodeInfo(TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useAdvancedCode, string codeToInsert, bool useCodeFormatting)
        {
            GeneratedCodeInfo codeInfo = this.CreateWrapper(template, eventData, methodName, useCodeFormatting);

            codeInfo = this.CreateMainNew(codeInfo, template, eventData, methodName, useAdvancedCode, codeToInsert);
            codeInfo = this.CreatePartialClasses(codeInfo);
            codeInfo = this.CreateTestsNew(codeInfo, template, eventData, methodName, useAdvancedCode);

            return(codeInfo);
        }
Example #2
0
        public CodeInfo CreateCodeItemInfo(MethodInfo methodInformation, string fileName, CodeType codeType, CodeElementType codeElementType, bool useVSFormatting)
        {
            string serverMethodFolderPath = projectManager.ServerMethodFolderPath;
            string selectedFolderPath     = projectManager.SelectedFolderPath;
            string methodName             = projectManager.MethodName;

            string codeItemPath = selectedFolderPath.Substring(serverMethodFolderPath.IndexOf(serverMethodFolderPath) + serverMethodFolderPath.Length);

            codeItemPath = Path.Combine(codeItemPath, fileName);
            string codeItemAttributePath = codeItemPath.Substring(codeItemPath.IndexOf(methodName) + methodName.Length + 1);

            codeItemAttributePath = codeItemAttributePath.Replace("\\", "/");

            var templateLoader = new TemplateLoader(this.dialogFactory);

            templateLoader.Load(projectManager.MethodConfigPath);

            TemplateInfo template = null;

            template = templateLoader.Templates.Where(t => t.TemplateLanguage == methodInformation.MethodLanguage && t.TemplateName == methodInformation.TemplateName).FirstOrDefault();
            if (template == null)
            {
                template = templateLoader.Templates.Where(t => t.TemplateLanguage == methodInformation.MethodLanguage && t.IsSupported).FirstOrDefault();
            }
            if (template == null)
            {
                throw new Exception("Template not found.");
            }

            EventSpecificDataType eventData = CommonData.EventSpecificDataTypeList.First(x => x.EventSpecificData == methodInformation.EventData);
            GeneratedCodeInfo     codeInfo  = this.CreateWrapper(template, eventData, methodName, useVSFormatting);

            string methodCode = File.ReadAllText(projectManager.MethodPath, new UTF8Encoding(true));
            var    tree       = CSharpSyntaxTree.ParseText(methodCode);
            var    root       = tree.GetRoot();

            var referenceUsings            = string.Empty;
            var mainUsingDirectiveSyntaxes = root.DescendantNodes().OfType <UsingDirectiveSyntax>();

            referenceUsings = string.Join("\r\n", mainUsingDirectiveSyntaxes);
            if (!string.IsNullOrEmpty(referenceUsings))
            {
                referenceUsings += "\r\n";
            }

            string codeItemTemplate = this.codeItemProvider.GetCodeElementTypeTemplate(codeType, codeElementType);
            string code             = string.Format(codeItemTemplate, referenceUsings, codeInfo.MethodCodeParentClassName, codeItemAttributePath, codeInfo.Namespace, fileName);
            var    codeItemInfo     = new CodeInfo()
            {
                Path = codeItemPath,
                Code = useVSFormatting ? FormattingCode(code) : code
            };

            return(codeItemInfo);
        }
Example #3
0
        public GeneratedCodeInfo CreateTestsNew(GeneratedCodeInfo generatedCodeInfo, TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useAdvancedCode)
        {
            var resultCodeInfo = new GeneratedCodeInfo(generatedCodeInfo);
            DefaultCodeTemplate defaultTemplate = LoadDefaultCodeTemplate(template, eventData);

            string code = useAdvancedCode ? defaultTemplate.AdvancedUnitTestsCode : defaultTemplate.SimpleUnitTestsCode;

            code = code.Replace("$(pkgname)", resultCodeInfo.Namespace);
            code = code.Replace("$(clsname)", resultCodeInfo.ClassName);

            resultCodeInfo.TestsCodeInfo.Code = resultCodeInfo.IsUseVSFormatting ? FormattingCode(code) : code;
            resultCodeInfo.TestsCodeInfo.Path = Path.Combine(methodName, methodName + "Tests.cs");

            return(resultCodeInfo);
        }
Example #4
0
 public GeneratedCodeInfo(GeneratedCodeInfo codeInfo)
 {
     this.ClassName  = codeInfo.ClassName;
     this.Namespace  = codeInfo.Namespace;
     this.MethodName = codeInfo.MethodName;
     this.MethodCodeParentClassName = codeInfo.MethodCodeParentClassName;
     this.IsUseVSFormatting         = codeInfo.IsUseVSFormatting;
     WrapperCodeInfo = new CodeInfo()
     {
         Code = codeInfo.WrapperCodeInfo.Code, Path = codeInfo.WrapperCodeInfo.Path
     };
     MethodCodeInfo = new CodeInfo()
     {
         Code = codeInfo.MethodCodeInfo.Code, Path = codeInfo.MethodCodeInfo.Path
     };
     PartialCodeInfoList = new List <CodeInfo>(codeInfo.PartialCodeInfoList);            // check references in list
     TestsCodeInfo       = new CodeInfo()
     {
         Code = codeInfo.TestsCodeInfo.Code, Path = codeInfo.TestsCodeInfo.Path
     };
 }
Example #5
0
 public GeneratedCodeInfo(GeneratedCodeInfo codeInfo)
 {
     this.ClassName  = codeInfo.ClassName;
     this.Namespace  = codeInfo.Namespace;
     this.MethodName = codeInfo.MethodName;
     this.MethodCodeParentClassName = codeInfo.MethodCodeParentClassName;
     this.IsUseVSFormatting         = codeInfo.IsUseVSFormatting;
     this.WrapperCodeInfo           = new CodeInfo()
     {
         Code = codeInfo.WrapperCodeInfo.Code, Path = codeInfo.WrapperCodeInfo.Path
     };
     this.MethodCodeInfo = new CodeInfo()
     {
         Code = codeInfo.MethodCodeInfo.Code, Path = codeInfo.MethodCodeInfo.Path
     };
     this.PartialCodeInfoList   = new List <CodeInfo>(codeInfo.PartialCodeInfoList);
     this.ExternalItemsInfoList = new List <CodeInfo>(codeInfo.ExternalItemsInfoList);
     this.TestsCodeInfo         = new CodeInfo()
     {
         Code = codeInfo.TestsCodeInfo.Code, Path = codeInfo.TestsCodeInfo.Path
     };
 }
Example #6
0
        public GeneratedCodeInfo CreateMainNew(GeneratedCodeInfo generatedCodeInfo,
                                               TemplateInfo template,
                                               EventSpecificDataType eventData,
                                               string methodName,
                                               bool useAdvancedCode,
                                               string codeToInsert)
        {
            DefaultCodeTemplate defaultTemplate = LoadDefaultCodeTemplate(template, eventData);
            StringBuilder       code            = new StringBuilder(useAdvancedCode ? defaultTemplate.AdvancedSourceCode : defaultTemplate.SimpleSourceCode);

            code = code.Replace("$(pkgname)", generatedCodeInfo.Namespace);
            code = code.Replace("$(clsname)", generatedCodeInfo.ClassName);

            if (!string.IsNullOrEmpty(codeToInsert))
            {
                string codeString  = code.ToString();
                var    defaultCode = GetSourceCodeBetweenRegion(codeString);
                if (string.IsNullOrWhiteSpace(defaultCode))
                {
                    var insertPattern = "#region MethodCode\r\n";
                    var insertIndex   = codeString.IndexOf(insertPattern);
                    code = code.Insert(insertIndex + insertPattern.Length, codeToInsert);
                }
                else
                {
                    code = code.Replace(defaultCode, codeToInsert);
                }
            }
            if (eventData.EventSpecificData != EventSpecificData.None)
            {
                code = code.Insert(0, "#define EventDataIsAvailable\r\n");
            }

            generatedCodeInfo.MethodCodeInfo.Code = generatedCodeInfo.IsUseVSFormatting ? this.FormattingCode(code.ToString()) : code.ToString();
            generatedCodeInfo.MethodCodeInfo.Path = Path.Combine(methodName, methodName + ".cs");

            return(generatedCodeInfo);
        }
Example #7
0
        public GeneratedCodeInfo CreateExternalItems(GeneratedCodeInfo methodInfo)
        {
            var resultGeneratedCode = new GeneratedCodeInfo(methodInfo);

            resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace("//[ExternalPath", "[ExternalPath");

            var tree = CSharpSyntaxTree.ParseText(resultGeneratedCode.MethodCodeInfo.Code);
            var root = tree.GetRoot();

            var members = root.DescendantNodes()
                          .OfType <AttributeSyntax>()
                          .Where(a => a.Name.ToString() == "ExternalPath")
                          .Select(a => new { AttributeInfo = a, Parent = a.Parent.Parent })
                          .Where(m =>
                                 m.Parent is EnumDeclarationSyntax ||
                                 m.Parent is ClassDeclarationSyntax ||
                                 m.Parent is StructDeclarationSyntax ||
                                 m.Parent is InterfaceDeclarationSyntax)
                          .ToList();

            foreach (var member in members)
            {
                var path = member.AttributeInfo.ArgumentList.Arguments.FirstOrDefault()?.ToString();
                path = path.Replace("/", "\\").Replace("\"", string.Empty);
                path = Path.Combine(resultGeneratedCode.MethodName, path);
                if (path != null)
                {
                    var    externalString   = member.Parent.ToFullString();
                    string stringForReplace = string.Empty;
                    string shouldBeReplaced = externalString;

                    var existingExternalInfo = resultGeneratedCode.ExternalItemsInfoList.FirstOrDefault(ei => ei.Path == path);
                    if (existingExternalInfo != null)
                    {
                        existingExternalInfo.Code += "\r\n" + externalString;
                    }
                    else
                    {
                        resultGeneratedCode.ExternalItemsInfoList.Add(new CodeInfo()
                        {
                            Code = externalString, Path = path
                        });
                    }

                    resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace(shouldBeReplaced, stringForReplace);
                }
            }

            var externalUsings = string.Empty;

            if (resultGeneratedCode.ExternalItemsInfoList.Count != 0)
            {
                var mainUsingDirectiveSyntaxes = root.DescendantNodes().OfType <UsingDirectiveSyntax>();
                externalUsings = string.Join("\r\n", mainUsingDirectiveSyntaxes);
                if (!string.IsNullOrEmpty(externalUsings))
                {
                    externalUsings += "\r\n";
                }
            }

            string externalClassTemplate = "{0}using Common;\r\nusing Common.Attributes;\r\nnamespace {2} \r\n{{\r\n{1}\r\n}}";

            foreach (var externalCodeInfo in resultGeneratedCode.ExternalItemsInfoList)
            {
                string code = string.Format(externalClassTemplate, externalUsings, externalCodeInfo.Code, resultGeneratedCode.Namespace);
                externalCodeInfo.Code = resultGeneratedCode.IsUseVSFormatting ? FormattingCode(code) : code;
            }

            return(resultGeneratedCode);
        }
Example #8
0
        public GeneratedCodeInfo CreatePartialClasses(GeneratedCodeInfo methodInfo)
        {
            var resultGeneratedCode = new GeneratedCodeInfo(methodInfo);

            resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace("//[PartialPath", "[PartialPath");

            var tree = CSharpSyntaxTree.ParseText(resultGeneratedCode.MethodCodeInfo.Code);
            var root = tree.GetRoot();

            var members = root.DescendantNodes()
                          .OfType <AttributeSyntax>()
                          .Where(a => a.Name.ToString() == "PartialPath")
                          .Select(a => new { AttributeInfo = a, Parent = a.Parent.Parent })
                          .Where(m =>
                                 m.Parent is FieldDeclarationSyntax ||
                                 m.Parent is EnumDeclarationSyntax ||
                                 m.Parent is ConstructorDeclarationSyntax ||
                                 m.Parent is DestructorDeclarationSyntax ||
                                 m.Parent is PropertyDeclarationSyntax ||
                                 m.Parent is MethodDeclarationSyntax ||
                                 m.Parent is OperatorDeclarationSyntax ||
                                 m.Parent is IndexerDeclarationSyntax ||
                                 m.Parent is ClassDeclarationSyntax ||
                                 m.Parent is StructDeclarationSyntax ||
                                 m.Parent is InterfaceDeclarationSyntax)
                          .ToList();

            foreach (var member in members)
            {
                var path = member.AttributeInfo.ArgumentList.Arguments.FirstOrDefault()?.ToString();
                path = path.Replace("/", "\\").Replace("\"", string.Empty);
                path = Path.Combine(resultGeneratedCode.MethodName, path);
                if (path != null)
                {
                    var    partialString    = member.Parent.ToFullString();
                    string stringForReplace = string.Empty;
                    string shouldBeReplaced = partialString;

                    if (partialString.Contains(endMethodCodeRegion))
                    {
                        int indexofEndRegion = partialString.IndexOf(endMethodCodeRegion);
                        stringForReplace = partialString.Substring(indexofEndRegion, partialString.Length - indexofEndRegion);
                        partialString    = partialString.Replace(stringForReplace, "}");
                        stringForReplace = '\t' + stringForReplace;
                    }

                    var existingPartialInfo = resultGeneratedCode.PartialCodeInfoList.FirstOrDefault(pi => pi.Path == path);
                    if (existingPartialInfo != null)
                    {
                        existingPartialInfo.Code += "\r\n" + partialString;
                    }
                    else
                    {
                        resultGeneratedCode.PartialCodeInfoList.Add(new CodeInfo()
                        {
                            Code = partialString, Path = path
                        });
                    }

                    resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace(shouldBeReplaced, stringForReplace);
                    if (!string.IsNullOrEmpty(stringForReplace))
                    {
                        string pattern      = string.Concat(@"\r\n( |\t)*}( |\t)*\r\n( |\t)*", endMethodCodeRegion);
                        string insertRegion = string.Concat(Environment.NewLine, endMethodCodeRegion);
                        string replacedCode = Regex.Replace(resultGeneratedCode.MethodCodeInfo.Code, pattern, insertRegion);
                        resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.IsUseVSFormatting ? FormattingCode(replacedCode) : replacedCode;
                    }
                }
            }

            var partialUsings = string.Empty;

            if (resultGeneratedCode.PartialCodeInfoList.Count != 0)
            {
                var mainUsingDirectiveSyntaxes = root.DescendantNodes().OfType <UsingDirectiveSyntax>();
                partialUsings = string.Join("\r\n", mainUsingDirectiveSyntaxes);
                if (!string.IsNullOrEmpty(partialUsings))
                {
                    partialUsings += "\r\n";
                }
            }

            string partialClassTemplate = "{0}using Common;\r\nusing Common.Attributes;\r\nnamespace {3} \r\n{{public partial class {1} \r\n{{\r\n{2}\r\n}}\r\n}}";

            foreach (var partialCodeInfo in resultGeneratedCode.PartialCodeInfoList)
            {
                string code = string.Format(partialClassTemplate, partialUsings, resultGeneratedCode.MethodCodeParentClassName, partialCodeInfo.Code, resultGeneratedCode.Namespace);
                partialCodeInfo.Code = resultGeneratedCode.IsUseVSFormatting ? FormattingCode(code) : code;
            }

            return(resultGeneratedCode);
        }
Example #9
0
        public GeneratedCodeInfo CreateWrapper(TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useVSFormatting)
        {
            if (string.IsNullOrEmpty(methodName))
            {
                throw new ArgumentException("Method name can not be empty");
            }

            DefaultCodeTemplate defaultTemplate = LoadDefaultCodeTemplate(template, eventData);
            string wrapperCode = defaultTemplate.WrapperSourceCode;

            const string fncname                 = "FNCMethod";
            var          eventDataClass          = eventData.EventDataClass;
            var          interfaceName           = eventData.InterfaceName;
            string       methodNameWithOutSpases = Regex.Replace(methodName, "[^a-zA-Z0-9]+", string.Empty, RegexOptions.Compiled);
            var          clsname                 = "ArasCLS" + methodNameWithOutSpases;
            var          pkgname                 = "ArasPKG" + methodNameWithOutSpases;

            if (!wrapperCode.EndsWith("\r\n"))
            {
                wrapperCode += "\r\n";
            }

            var resultCode = template.TemplateCode;

            wrapperCode = wrapperCode.Insert(0, "[WrapperMethod]\r\n");
            resultCode  = resultCode.Replace("$(MethodCode)", wrapperCode);
            resultCode  = resultCode.Replace("$(pkgname)", pkgname);
            resultCode  = resultCode.Replace("$(clsname)", clsname);
            resultCode  = resultCode.Replace("$(interfacename)", interfaceName);
            resultCode  = resultCode.Replace("$(fncname)", fncname);
            resultCode  = resultCode.Replace("$(EventDataClass)", eventDataClass);

            var        tree = CSharpSyntaxTree.ParseText(resultCode);
            SyntaxNode root = tree.GetRoot();

            var member = root.DescendantNodes()
                         .OfType <AttributeSyntax>()
                         .Where(a => a.Name.ToString() == "WrapperMethod")
                         .FirstOrDefault();
            var parentClassName = GetParentClassName(member);

            var clss = root.DescendantNodes()
                       .OfType <ClassDeclarationSyntax>()
                       .FirstOrDefault(a => a.Identifier.Text.ToString() == parentClassName);

            if (clss != null)
            {
                var clsWithModifier = clss.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
                clsWithModifier = clsWithModifier.NormalizeWhitespace();
                root            = root.ReplaceNode(clss, clsWithModifier);
            }

            resultCode = root.ToString().Replace("[WrapperMethod]", string.Empty);

            GeneratedCodeInfo resultInfo = new GeneratedCodeInfo();

            resultInfo.WrapperCodeInfo.Code      = useVSFormatting ? FormattingCode(resultCode) : resultCode;
            resultInfo.WrapperCodeInfo.Path      = Path.Combine(methodName, methodName + "Wrapper.cs");
            resultInfo.MethodName                = methodName;
            resultInfo.ClassName                 = clsname;
            resultInfo.Namespace                 = pkgname;
            resultInfo.MethodCodeParentClassName = parentClassName;
            resultInfo.IsUseVSFormatting         = useVSFormatting;

            return(resultInfo);
        }
Example #10
0
 public GeneratedCodeInfo CreatePartialClasses(GeneratedCodeInfo methodInfo)
 {
     throw new NotImplementedException();
 }
Example #11
0
 public GeneratedCodeInfo CreateTestsNew(GeneratedCodeInfo generatedCodeInfo, TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useAdvancedCode)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public GeneratedCodeInfo UpdateSourceCodeWithPartialClasses(GeneratedCodeInfo methodInfo)
 {
     throw new NotImplementedException();
 }
Example #13
0
        public GeneratedCodeInfo CreatePartialClasses(GeneratedCodeInfo methodInfo)
        {
            var resultGeneratedCode = new GeneratedCodeInfo(methodInfo);

            resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace("//[PartialPath", "[PartialPath");

            var tree = CSharpSyntaxTree.ParseText(resultGeneratedCode.MethodCodeInfo.Code);
            var root = tree.GetRoot();

            var members = root.DescendantNodes()
                          .OfType <AttributeSyntax>()
                          .Where(a => a.Name.ToString() == "PartialPath")
                          .Select(a => new { AttributeInfo = a, Parent = a.Parent.Parent })
                          .Where(m =>
                                 m.Parent is FieldDeclarationSyntax ||
                                 m.Parent is EnumDeclarationSyntax ||
                                 m.Parent is ConstructorDeclarationSyntax ||
                                 m.Parent is DestructorDeclarationSyntax ||
                                 m.Parent is PropertyDeclarationSyntax ||
                                 m.Parent is MethodDeclarationSyntax ||
                                 m.Parent is OperatorDeclarationSyntax ||
                                 m.Parent is IndexerDeclarationSyntax ||
                                 m.Parent is ClassDeclarationSyntax ||
                                 m.Parent is StructDeclarationSyntax ||
                                 m.Parent is InterfaceDeclarationSyntax)
                          .ToList();

            foreach (var member in members)
            {
                var path = member.AttributeInfo.ArgumentList.Arguments.FirstOrDefault()?.ToString();
                path = path.Replace("/", "\\").Replace("\"", string.Empty);
                path = Path.Combine(resultGeneratedCode.MethodName, path);
                if (path != null)
                {
                    var    partialString    = member.Parent.ToString();
                    string stringForReplace = string.Empty;
                    string shouldBeReplaced = partialString;

                    if (partialString.Contains("#endregion MethodCode"))
                    {
                        int indexofEndRegion = partialString.IndexOf("#endregion MethodCode");
                        stringForReplace = partialString.Substring(indexofEndRegion, partialString.Length - indexofEndRegion);
                        partialString    = partialString.Replace(stringForReplace, "}");
                        stringForReplace = '\t' + stringForReplace;
                    }

                    var existingPartialInfo = resultGeneratedCode.PartialCodeInfoList.FirstOrDefault(pi => pi.Path == path);
                    if (existingPartialInfo != null)
                    {
                        existingPartialInfo.Code += "\r\n" + partialString;
                    }
                    else
                    {
                        resultGeneratedCode.PartialCodeInfoList.Add(new CodeInfo()
                        {
                            Code = partialString, Path = path
                        });
                    }

                    resultGeneratedCode.MethodCodeInfo.Code = resultGeneratedCode.MethodCodeInfo.Code.Replace(shouldBeReplaced, stringForReplace);
                    if (!string.IsNullOrEmpty(stringForReplace))
                    {
                        char currentCharacter = ' ';
                        int  endRegionIndex   = resultGeneratedCode.MethodCodeInfo.Code.IndexOf("#endregion MethodCode");
                        int  endMethodIndex   = endRegionIndex;
                        do
                        {
                            currentCharacter = resultGeneratedCode.MethodCodeInfo.Code[endMethodIndex];
                            endMethodIndex--;
                        }while (endMethodIndex > 0 && currentCharacter != '}');

                        currentCharacter = default(char);
                        while (currentCharacter != '\r')
                        {
                            currentCharacter = resultGeneratedCode.MethodCodeInfo.Code[endRegionIndex];
                            endRegionIndex--;
                        }
                        var endRegionEndString   = resultGeneratedCode.MethodCodeInfo.Code.Substring(endRegionIndex + 1);
                        var beginRegionEndString = resultGeneratedCode.MethodCodeInfo.Code.Remove(endMethodIndex + 1).TrimEnd(new char[] { ' ', '\r', '\n' });
                        resultGeneratedCode.MethodCodeInfo.Code = beginRegionEndString + endRegionEndString;
                    }
                }
            }

            var partialUsings = string.Empty;

            if (resultGeneratedCode.PartialCodeInfoList.Count != 0)
            {
                var mainUsingDirectiveSyntaxes = root.DescendantNodes().OfType <UsingDirectiveSyntax>();
                partialUsings = string.Join("\r\n", mainUsingDirectiveSyntaxes);
                if (!string.IsNullOrEmpty(partialUsings))
                {
                    partialUsings += "\r\n";
                }
            }

            string partialClassTemplate = "{0}using Common;\r\nnamespace {3} \r\n{{public partial class {1} \r\n{{\r\n{2}\r\n}}\r\n}}";

            foreach (var partialCodeInfo in resultGeneratedCode.PartialCodeInfoList)
            {
                string code = string.Format(partialClassTemplate, partialUsings, resultGeneratedCode.MethodCodeParentClassName, partialCodeInfo.Code, resultGeneratedCode.Namespace);
                partialCodeInfo.Code = resultGeneratedCode.IsUseVSFormatting ? FormattingCode(code) : code;
            }

            return(resultGeneratedCode);
        }