public static T ProcessTemplates <T>(AopCsharpTemplateService templateService, List <AopTemplate> templates, T node, ClassDeclarationSyntax classDeclaration) where T : SyntaxNode { T result = node; if (templates.Count == 0) { return(result); } string startingWhitespace = ""; if (node.HasLeadingTrivia) { startingWhitespace = node.GetLeadingTrivia().ToFullString(); } string closingWhitespace = ""; if (node.HasTrailingTrivia) { closingWhitespace = node.GetTrailingTrivia().ToFullString(); } foreach (AopTemplate template in templates.OrderBy(o => o.AdvicePriority)) { Console.Out.WriteLine($"\tProcessing template {template.TemplateName}"); string sourceCode = templateService.ProcessTemplate(template.TemplateName, new Dictionary <string, object>() { { "ClassNode", result is ClassDeclarationSyntax ? result as ClassDeclarationSyntax : classDeclaration }, { "MethodNode", result is MethodDeclarationSyntax ? result : null }, { "PropertyNode", result is PropertyDeclarationSyntax ? result : null }, { "StatementNode", null }, { "SyntaxNode", result }, { "AppliedTo", template.AppliedTo.ToString() }, { "ExtraTag", template.ExtraTag } }); // if sourceCode is null, it means no changes were done to original code and we keep it as-is if (sourceCode == null) { continue; } SyntaxNode compUnit = SyntaxFactory.ParseCompilationUnit(startingWhitespace + sourceCode.Trim(' ', '\r', '\n') + closingWhitespace); result = compUnit.DescendantNodes().OfType <T>().FirstOrDefault(); if (result == null) { throw (new Exception("Cannot parse generated source code as a " + typeof(T).Name)); } } return(result); }
private static SyntaxNode ProcessTemplates(List <AopTemplate> templates, StatementSyntax node, ClassDeclarationSyntax classDeclaration) { SyntaxNode result = node; string startingWhitespace = ""; if (node.HasLeadingTrivia) { startingWhitespace = node.GetLeadingTrivia().ToFullString(); } string closingWhitespace = ""; if (node.HasTrailingTrivia) { closingWhitespace = node.GetTrailingTrivia().ToFullString(); } var aopCsharpTemplateService = new AopCsharpTemplateService(); foreach (AopTemplate template in templates.OrderBy(o => o.AdvicePriority)) { Console.Out.WriteLine($"\tProcessing template {template.TemplateName}"); string sourceCode = aopCsharpTemplateService.ProcessTemplate(template.TemplateName, new Dictionary <string, object>() { { "ClassNode", classDeclaration }, { "MethodNode", result is MethodDeclarationSyntax ? result : null }, { "PropertyNode", result is PropertyDeclarationSyntax ? result : null }, { "StatementNode", result is StatementSyntax ? result : null }, { "ExtraTag", template.ExtraTag } }); // if sourceCode is null, it means no changes were done to original code and we keep it as-is if (sourceCode == null) { continue; } sourceCode = sourceCode.Trim(' ', '\r', '\n'); if (!Regex.IsMatch(sourceCode, "^\\s*\\{.*\\}\\s*$", RegexOptions.Singleline)) { sourceCode = (new StringBuilder()).AppendLine("{").AppendLine(sourceCode).AppendLine(startingWhitespace + "}").ToString(); } result = SyntaxFactory.ParseStatement(startingWhitespace + sourceCode + closingWhitespace); } return(result); }
public static IEnumerable <SyntaxNode> ProcessTemplate(AopCsharpTemplateService templateService, AopTemplate template, SyntaxNode node, ClassDeclarationSyntax classDeclaration) { string startingWhitespace = ""; if (node.HasLeadingTrivia) { startingWhitespace = node.GetLeadingTrivia().ToFullString(); } string closingWhitespace = ""; if (node.HasTrailingTrivia) { closingWhitespace = node.GetTrailingTrivia().ToFullString(); } Console.Out.WriteLine($"\tProcessing template {template.TemplateName}"); string sourceCode = templateService.ProcessTemplate(template.TemplateName, new Dictionary <string, object>() { { "ClassNode", classDeclaration }, { "MethodNode", node is MethodDeclarationSyntax ? node : null }, { "PropertyNode", node is PropertyDeclarationSyntax ? node : null }, { "SyntaxNode", node }, { "AppliedTo", template.AppliedTo.ToString() }, { "StatementNode", null }, { "ExtraTag", template.ExtraTag } }); // if sourceCode is null, it means no changes were done to original code and we keep it as-is if (sourceCode == null) { return new List <SyntaxNode> { node } } ; SyntaxNode compUnit = SyntaxFactory.ParseCompilationUnit(startingWhitespace + sourceCode.Trim(' ', '\r', '\n') + closingWhitespace); return(compUnit.ChildNodes()); }
public AopClassRewriter(string filePath, AopCsharpTemplateService templateService) { _filePath = filePath; _templateService = templateService; }
public AopRewriter(string filePath, AopCsharpTemplateService templateService, Dictionary <string, List <AopTemplate> > classTemplates) { _filePath = filePath; _templateService = templateService; _classTemplates = classTemplates; }