Exemple #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);
        }
Exemple #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);
        }
Exemple #3
0
        private DefaultCodeTemplate LoadDefaultCodeTemplate(TemplateInfo template, EventSpecificDataType eventData)
        {
            var defaultTemplate = defaultCodeProvider.GetDefaultCodeTemplate(projectManager.DefaultCodeTemplatesPath,
                                                                             template.TemplateName, eventData.EventSpecificData.ToString());

            if (defaultTemplate == null)
            {
                throw new FileNotFoundException($"Default code template file with templateName=\"{template.TemplateName}\" eventData=\"{eventData.EventSpecificData}\" not found.");
            }

            return(defaultTemplate);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
0
 public GeneratedCodeInfo GenerateCodeInfo(TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useAdvancedCode, string codeToInsert, bool useCodeFormating)
 {
     throw new NotImplementedException();
 }
Exemple #8
0
 public GeneratedCodeInfo CreateTestsNew(GeneratedCodeInfo generatedCodeInfo, TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useAdvancedCode)
 {
     throw new NotImplementedException();
 }
Exemple #9
0
 public GeneratedCodeInfo CreateWrapper(TemplateInfo template, EventSpecificDataType eventData, string methodName, bool useCodeFormating)
 {
     throw new NotImplementedException();
 }
Exemple #10
0
 public void LoadCodeToProject(dynamic methodItem, EventSpecificDataType eventData, string packageName)
 {
     throw new NotImplementedException();
 }
Exemple #11
0
 public void LoadCodeToProject(string methodLanguage, string methodCode, string methodLocation, string methodName, string innovatorMethodConfigId, string innovatorMethodId, EventSpecificDataType eventData, string packageName, string executionAllowedToId, string executionAllowedToKeyedName)
 {
     throw new NotImplementedException();
 }
Exemple #12
0
        public string GenerateSourceCodeByTemplate(TemplateInfo template, string methodName, string methodCode, string packageName, EventSpecificDataType eventData)
        {
            const string fncname        = "FNCMethod";
            var          eventDataClass = eventData.EventDataClass;
            var          interfaceName  = eventData.InterfaceName;
            var          clsname        = "CLS_" + methodName + "_version";
            var          pkgname        = "PKG_" + methodName + "_version";

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

            var methodCodeWithRegion = string.Format("\r\n\r\n#Region \"MethodCode\"\r\n{0}#End Region \r\n\r\n", methodCode);
            var resultCode           = template.TemplateCode;

            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);
            resultCode = resultCode.Replace("$(MethodCode)", methodCodeWithRegion);

            if (eventData.EventSpecificData != EventSpecificData.None)
            {
                resultCode = resultCode.Insert(0, "#define EventDataIsAvailable");
            }

            return(resultCode);
        }
Exemple #13
0
        //		public string GetSourceCodeDataAccessLayerTests(string methodName, string classPrefix)
        //		{
        //			string pkgname = "PKG_" + methodName + "_version";
        //			string sourceCode = string.Format(@"Imports Aras.IOM
        //Imports NUnit.Framework

        //Namespace {0}

        //	<TestFixture> _
        //	Public Class DataAccessLayerTests
        //		<Test> _
        //		Public Sub Ctor_InnovatorIsNull_ShouldThrowArgumentNullException()
        //			' Arrange
        //			Dim innovator As Innovator = Nothing

        //			' Assert
        //			Assert.Throws(Of ArgumentNullException)(New TestDelegate(Function()
        //			' Act
        //			Dim dataAccessLayer = New {1}DataAccessLayer(innovator)

        //End Function), ""innovator"")
        //		End Sub
        //	End Class
        //End Namespace", pkgname, classPrefix);

        //			return sourceCode;
        //		}

        //		public string GetSourceCodeBusinessLogicTests(string methodName, string classPrefix)
        //		{
        //			string pkgname = "PKG_" + methodName + "_version";
        //			string sourceCode = string.Format(@"Imports Aras.IOM
        //Imports NSubstitute
        //Imports NUnit.Framework

        //Namespace {0}
        //	<TestFixture>
        //	Public Class BusinessLogicTests
        //		<Test>
        //		Public Sub Ctor_DataAccessLayerIsNull_ShouldThrowArgumentNullException()
        //			' Arrange
        //			Dim dataAccessLayer As {1}IDataAccessLayer = Nothing

        //			' Assert
        //			Assert.Throws(Of ArgumentNullException)(New TestDelegate(Function()
        //			' Act
        //			Dim businessLogic = New {1}BusinessLogic(dataAccessLayer)

        //End Function), ""dataAccessLayer"")
        //		End Sub

        //		<Test>
        //		Public Sub Run_ShouldReturnSameItem()
        //			' Arrange
        //			Dim serverConnection As IServerConnection = Substitute.[For](Of IServerConnection)()
        //			Dim innovator As New Innovator(serverConnection)

        //			Dim dataAccessLayer As {1}IDataAccessLayer = New {1}DataAccessLayer(innovator)
        //			Dim businessLogic As New {1}BusinessLogic(dataAccessLayer)

        //			Dim expected As Item = innovator.newItem()

        //			' Act
        //			Dim actual As Item = businessLogic.Run(expected)

        //			' Assert
        //			Assert.AreEqual(expected, actual)
        //		End Sub
        //	End Class
        //End Namespace
        //", pkgname, classPrefix);
        //			return sourceCode;
        //		}

        public string GenerateSourceCodeByTemplate(TemplateInfo template, string methodName, string packageName, EventSpecificDataType eventData)
        {
            string methodCode = @"End Function
Friend Interface IDataAccessLayer

End Interface

Friend Class DataAccessLayer
	Implements IDataAccessLayer
	Private ReadOnly innovator As Innovator

	Friend Sub New(innovator As Innovator)
		If innovator Is Nothing Then
			Throw New ArgumentNullException(""innovator"")
		End If

		Me.innovator = innovator
	End Sub
End Class

Friend Class BusinessLogic
	Private ReadOnly dataAccessLayer As IDataAccessLayer

	Friend Sub New(dataAccessLayer As IDataAccessLayer)
		If dataAccessLayer Is Nothing Then
			Throw New ArgumentNullException(""dataAccessLayer"")
		End If

		Me.dataAccessLayer = dataAccessLayer
	End Sub

	Friend Function Run(contextItem As Item) As Item
		Return contextItem
	End Function
End Class
		"        ;

            string sourceCode = GenerateSourceCodeByTemplate(template, methodName, methodCode, packageName, eventData);

            return(sourceCode);
        }