Exemplo n.º 1
0
        public override SyntaxNode VisitAttribute(AttributeSyntax node)
        {
            AttributeSyntax attributeSyntax = (AttributeSyntax)base.VisitAttribute(node);

            foreach (var action in _allActions.OfType <AttributeAction>())
            {
                if (action.Key == node.Name.ToString())
                {
                    if (action.AttributeActionFunc != null)
                    {
                        var actionExecution = new GenericActionExecution(action, _filePath)
                        {
                            TimesRun = 1
                        };
                        try
                        {
                            attributeSyntax = action.AttributeActionFunc(_syntaxGenerator, attributeSyntax);
                            LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                            actionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                        allExecutedActions.Add(actionExecution);
                    }
                }
            }

            return(attributeSyntax);
        }
Exemplo n.º 2
0
        public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var symbol = SemanticHelper.GetSemanticSymbol(node, _semanticModel, _preportSemanticModel);
            ExpressionSyntax newNode      = node;
            bool             skipChildren = false; // This is here to skip actions on children node when the main identifier was changed. Just use new expression for the subsequent children actions.

            foreach (var action in _allActions.OfType <ObjectCreationExpressionAction>())
            {
                if (newNode.ToString() == action.Key || symbol?.OriginalDefinition.ToDisplayString() == action.Key)
                {
                    var actionExecution = new GenericActionExecution(action, _filePath)
                    {
                        TimesRun = 1
                    };
                    try
                    {
                        skipChildren = true;
                        newNode      = action.ObjectCreationExpressionGenericActionFunc(_syntaxGenerator, (ObjectCreationExpressionSyntax)newNode);
                        allExecutedActions.Add(actionExecution);
                        LogHelper.LogInformation(string.Format("{0}", action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                }
            }
            if (!skipChildren)
            {
                newNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node);
            }
            return(newNode);
        }
Exemplo n.º 3
0
        public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            NamespaceDeclarationSyntax newNode = (NamespaceDeclarationSyntax)base.VisitNamespaceDeclaration(node);

            foreach (var action in _allActions.OfType <NamespaceAction>())
            {
                if (action.Key == newNode.Name.ToString())
                {
                    var actionExecution = new GenericActionExecution(action, _filePath)
                    {
                        TimesRun = 1
                    };
                    try
                    {
                        newNode = action.NamespaceActionFunc(_syntaxGenerator, newNode);
                        LogHelper.LogInformation(string.Format("{0}", action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allExecutedActions.Add(actionExecution);
                }
            }
            return(newNode);
        }
Exemplo n.º 4
0
        public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
        {
            var symbol  = SemanticHelper.GetSemanticSymbol(node, _semanticModel, _preportSemanticModel);
            var newNode = base.VisitMemberAccessExpression(node);

            if (symbol != null)
            {
                var nodeKey = $"{symbol.ContainingType}.{node.Name}";

                foreach (var action in _allActions.OfType <MemberAccessAction>())
                {
                    if (nodeKey == action.Key)
                    {
                        var actionExecution = new GenericActionExecution(action, _filePath)
                        {
                            TimesRun = 1
                        };
                        try
                        {
                            newNode = action.MemberAccessActionFunc(_syntaxGenerator, newNode);
                            LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                            actionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                        allExecutedActions.Add(actionExecution);
                    }
                }
            }
            return(newNode);
        }
Exemplo n.º 5
0
        public override SyntaxNode VisitCompilationUnit(CompilationUnitSyntax node)
        {
            CompilationUnitSyntax newNode = (CompilationUnitSyntax)base.VisitCompilationUnit(node);

            //Applying using actions
            foreach (var action in _allActions.OfType <UsingAction>())
            {
                var actionExecution = new GenericActionExecution(action, _filePath)
                {
                    TimesRun = 1
                };
                try
                {
                    newNode = action.UsingActionFunc(_syntaxGenerator, newNode);
                    LogHelper.LogInformation(string.Format("{0}", action.Description));
                }
                catch (Exception ex)
                {
                    var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                    actionExecution.InvalidExecutions = 1;
                    LogHelper.LogError(actionExecutionException);
                }
                allExecutedActions.Add(actionExecution);
            }

            return(newNode);
        }
Exemplo n.º 6
0
        public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
        {
            var symbol = SemanticHelper.GetSemanticSymbol(node, _semanticModel, _preportSemanticModel);

            var identifierNameSyntax = (IdentifierNameSyntax)base.VisitIdentifierName(node);

            if (symbol != null)
            {
                var nodeKey = symbol.OriginalDefinition != null?symbol.OriginalDefinition.ToString() : symbol.ToString();

                foreach (var action in _allActions.OfType <IdentifierNameAction>())
                {
                    if (nodeKey == action.Key && identifierNameTypes.Contains(identifierNameSyntax.Parent?.GetType()))
                    {
                        var actionExecution = new GenericActionExecution(action, _filePath)
                        {
                            TimesRun = 1
                        };
                        try
                        {
                            identifierNameSyntax = action.IdentifierNameActionFunc(_syntaxGenerator, identifierNameSyntax);
                            LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                            actionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                        allExecutedActions.Add(actionExecution);
                    }
                }
            }
            return(identifierNameSyntax);
        }
Exemplo n.º 7
0
        private List <GenericActionExecution> AddActionsWithoutExecutions(FileActions currentFileActions, List <GenericActionExecution> allActions)
        {
            var filePath = currentFileActions.FilePath;

            //We are looking for an action that is in our file actions but has no executions:
            var actionsWithNoMatch = currentFileActions.AllActions
                                     .Where(a => !allActions.Any(runInstance => runInstance.Name == a.Name && runInstance.Type == a.Type && runInstance.Key == a.Key && runInstance.Value == a.Value));

            foreach (var action in actionsWithNoMatch)
            {
                var genericActionExecution = new GenericActionExecution(action, filePath)
                {
                    TimesRun = 0
                };
                allActions.Add(genericActionExecution);
            }

            allActions = allActions
                         .GroupBy(a => new { a.Type, a.Name, a.Value })
                         .Select(g => new GenericActionExecution()
            {
                Type              = g.First().Type,
                Name              = g.First().Name,
                Value             = g.First().Value,
                FilePath          = filePath,
                TimesRun          = g.Sum(gt => gt.TimesRun),
                InvalidExecutions = g.Sum(gi => gi.InvalidExecutions)
            })
                         .ToList();

            return(allActions);
        }
Exemplo n.º 8
0
        public override SyntaxNode VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
        {
            var classSymbol = SemanticHelper.GetDeclaredSymbol(node, _semanticModel, _preportSemanticModel);
            InterfaceDeclarationSyntax newNode = (InterfaceDeclarationSyntax)base.VisitInterfaceDeclaration(node);

            foreach (var action in _allActions.OfType <InterfaceDeclarationAction>())
            {
                if (action.Key == node.Identifier.Text.Trim())
                {
                    var actionExecution = new GenericActionExecution(action, _filePath)
                    {
                        TimesRun = 1
                    };
                    try
                    {
                        newNode = action.InterfaceDeclarationActionFunc(_syntaxGenerator, newNode);
                        LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allExecutedActions.Add(actionExecution);
                }
            }
            return(newNode);
        }
Exemplo n.º 9
0
        public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            ObjectCreationExpressionSyntax newNode = node;// (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node);
            bool skipChildren = false;

            foreach (var action in _fileActions.ObjectCreationExpressionActions)
            {
                if (newNode.ToString() == action.Key)
                {
                    var actionExecution = new GenericActionExecution(action, _fileActions.FilePath);
                    actionExecution.TimesRun = 1;
                    try
                    {
                        skipChildren = true;
                        var createdNode = action.ObjectCreationExpressionGenericActionFunc(_syntaxGenerator, newNode);
                        allActions.Add(actionExecution);
                        LogHelper.LogInformation(string.Format("{0}", action.Description));
                        return(createdNode);
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allActions.Add(actionExecution);
                }
            }
            if (!skipChildren)
            {
                newNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node);
            }
            return(newNode);
        }
Exemplo n.º 10
0
        public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
        {
            var symbols = _semanticModel.GetSymbolInfo(node);
            var newNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node);

            foreach (var action in _fileActions.InvocationExpressionActions)
            {
                if (symbols.Symbol.Name == action.Key)
                {
                    var actionExecution = new GenericActionExecution(action, _fileActions.FilePath);
                    actionExecution.TimesRun = 1;
                    try
                    {
                        newNode = action.InvocationExpressionActionFunc(_syntaxGenerator, newNode);
                        LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allActions.Add(actionExecution);
                }
            }
            return(newNode);
        }
Exemplo n.º 11
0
        public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
        {
            var symbolInfo = _semanticModel.GetSymbolInfo(node);

            var identifierNameSyntax = (IdentifierNameSyntax)base.VisitIdentifierName(node);

            if (symbolInfo.Symbol != null)
            {
                foreach (var action in _fileActions.IdentifierNameActions)
                {
                    if (symbolInfo.Symbol.ToString() == action.Key && identifierNameTypes.Contains(identifierNameSyntax.Parent?.GetType()))
                    {
                        var actionExecution = new GenericActionExecution(action, _fileActions.FilePath);
                        actionExecution.TimesRun = 1;
                        try
                        {
                            identifierNameSyntax = action.IdentifierNameActionFunc(_syntaxGenerator, identifierNameSyntax);
                            LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                            actionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                        allActions.Add(actionExecution);
                    }
                }
            }
            return(identifierNameSyntax);
        }
Exemplo n.º 12
0
        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            var classSymbol = _semanticModel.GetDeclaredSymbol(node);
            ClassDeclarationSyntax newNode = (ClassDeclarationSyntax)base.VisitClassDeclaration(node);

            foreach (var action in _fileActions.ClassDeclarationActions)
            {
                if (action.Key == node.Identifier.Text.Trim())
                {
                    var actionExecution = new GenericActionExecution(action, _fileActions.FilePath);
                    actionExecution.TimesRun = 1;
                    try
                    {
                        newNode = action.ClassDeclarationActionFunc(_syntaxGenerator, newNode);
                        LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allActions.Add(actionExecution);
                }
            }
            return(newNode);
        }
Exemplo n.º 13
0
        protected virtual List <GenericActionExecution> ApplyProjectActions(ProjectActions projectActions, ProjectType projectType)
        {
            var projectRunActions = new List <GenericActionExecution>();

            //Project Level Actions
            foreach (var projectLevelAction in projectActions.ProjectLevelActions)
            {
                var projectActionExecution = new GenericActionExecution(projectLevelAction, _projectConfiguration.ProjectPath)
                {
                    TimesRun = 1
                };
                var runResult = string.Empty;
                if (!_projectConfiguration.IsMockRun)
                {
                    if (projectLevelAction.ProjectLevelActionFunc != null)
                    {
                        try
                        {
                            runResult = projectLevelAction.ProjectLevelActionFunc(_projectConfiguration.ProjectPath, projectType);
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(projectLevelAction.Name, projectLevelAction.Key, ex);
                            projectActionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                    }
                    else if (projectLevelAction.ProjectFileActionFunc != null)
                    {
                        try
                        {
                            runResult = projectLevelAction.ProjectFileActionFunc(_projectConfiguration.ProjectPath,
                                                                                 projectType,
                                                                                 _projectConfiguration.TargetVersions,
                                                                                 projectActions.PackageActions.GroupBy(g => g.Name).Select(g => g.FirstOrDefault()).ToDictionary(p => p.Name, p => p.Version),
                                                                                 projectActions.ProjectReferenceActions.ToList(),
                                                                                 _metadataReferences);
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(projectLevelAction.Name, projectLevelAction.Key, ex);
                            projectActionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                    }
                }
                if (!string.IsNullOrEmpty(runResult))
                {
                    projectActionExecution.Description = string.Concat(projectActionExecution.Description, ": ", runResult);
                    projectRunActions.Add(projectActionExecution);
                    LogHelper.LogInformation(projectLevelAction.Description);
                }
            }

            return(projectRunActions);
        }
Exemplo n.º 14
0
        public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
        {
            var        newNode      = base.VisitExpressionStatement(node);
            SyntaxNode modifiedNode = newNode;

            var invocationExpressionNodes = node.DescendantNodes().OfType <InvocationExpressionSyntax>().ToList();

            if (invocationExpressionNodes.Count <= 0)
            {
                return(newNode);
            }
            var invocationExpressionNode = invocationExpressionNodes.First();

            var symbol = SemanticHelper.GetSemanticSymbol(invocationExpressionNode, _semanticModel, _preportSemanticModel);

            if (symbol == null)
            {
                return(newNode);
            }
            var nodeKey = symbol.OriginalDefinition.ToString();

            foreach (var action in _allActions.OfType <ExpressionAction>())
            {
                if (nodeKey == action.Key)
                {
                    var actionExecution = new GenericActionExecution(action, _filePath)
                    {
                        TimesRun = 1
                    };
                    try
                    {
                        modifiedNode = action.ExpressionActionFunc(_syntaxGenerator, newNode);
                        LogHelper.LogInformation(string.Format("{0}: {1}", node.SpanStart, action.Description));
                    }
                    catch (Exception ex)
                    {
                        var actionExecutionException = new ActionExecutionException(action.Name, action.Key, ex);
                        actionExecution.InvalidExecutions = 1;
                        LogHelper.LogError(actionExecutionException);
                    }
                    allExecutedActions.Add(actionExecution);
                }
            }
            return(modifiedNode);
        }
Exemplo n.º 15
0
        public GenericActionExecutionMetric(MetricsContext context, GenericActionExecution action, string projectPath)
        {
            ActionName        = action.Name;
            ActionType        = action.Type;
            ActionValue       = action.Value;
            TimesRun          = action.TimesRun;
            InvalidExecutions = action.InvalidExecutions;
            SolutionPath      = context.SolutionPathHash;
            ProjectGuid       = context.ProjectGuidMap.GetValueOrDefault(projectPath, "N/A");

            if (ActionType == Constants.Project)
            {
                FilePath = ProjectGuid;
            }
            else
            {
                FilePath = string.IsNullOrEmpty(action.FilePath) ? "N/A" : EncryptionHelper.ConvertToSHA256Hex(action.FilePath);
            }
        }
Exemplo n.º 16
0
        public Dictionary <string, List <GenericActionExecution> > Run(ProjectActions projectActions, ProjectType projectType)
        {
            IEnumerable <FileActions> fileActions = projectActions.FileActions;

            ConcurrentDictionary <string, List <GenericActionExecution> > actionsPerProject = new ConcurrentDictionary <string, List <GenericActionExecution> >();

            var parallelOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = Constants.ThreadCount
            };

            Parallel.ForEach(_sourceFileBuildResults, parallelOptions, sourceFileBuildResult => {
                try
                {
                    var currentFileActions = fileActions.Where(f => f.FilePath == sourceFileBuildResult.SourceFileFullPath).FirstOrDefault();

                    var root = sourceFileBuildResult.SyntaxTree.GetRoot();

                    if (currentFileActions != null)
                    {
                        LogHelper.LogInformation("---------------------------------------------------------------------------");
                        LogHelper.LogInformation("Processing file " + sourceFileBuildResult.SourceFilePath);

                        ActionsRewriter oneRewriter = new ActionsRewriter(sourceFileBuildResult.SemanticModel, sourceFileBuildResult.SyntaxGenerator, currentFileActions);
                        root = oneRewriter.Visit(root);


                        var result = root.NormalizeWhitespace().ToFullString();
                        //TODO : Can you send a result
                        if (!_projectConfiguration.IsMockRun)
                        {
                            File.WriteAllText(sourceFileBuildResult.SourceFileFullPath, result);
                        }
                        var processedActions = ValidateActions(oneRewriter.allActions, result);
                        processedActions     = AddActionsWithoutExecutions(currentFileActions, oneRewriter.allActions);

                        if (!actionsPerProject.TryAdd(sourceFileBuildResult.SourceFileFullPath, processedActions))
                        {
                            throw new FilePortingException(sourceFileBuildResult.SourceFilePath, new Exception("File already exists in collection"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    var filePortingException = new FilePortingException(sourceFileBuildResult.SourceFilePath, ex);
                    LogHelper.LogError(filePortingException);
                }
            });

            var projectRunActions = new List <GenericActionExecution>();

            //Project Level Actions
            foreach (var projectLevelAction in projectActions.ProjectLevelActions)
            {
                var projectActionExecution = new GenericActionExecution(projectLevelAction, _projectConfiguration.ProjectPath);
                projectActionExecution.TimesRun = 1;
                var runResult = string.Empty;
                if (!_projectConfiguration.IsMockRun)
                {
                    if (projectLevelAction.ProjectLevelActionFunc != null)
                    {
                        try
                        {
                            runResult = projectLevelAction.ProjectLevelActionFunc(_projectConfiguration.ProjectPath, projectType);
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(projectLevelAction.Name, projectLevelAction.Key, ex);
                            projectActionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                    }
                    else if (projectLevelAction.ProjectFileActionFunc != null)
                    {
                        try
                        {
                            runResult = projectLevelAction.ProjectFileActionFunc(_projectConfiguration.ProjectPath, projectType, _projectConfiguration.TargetVersions, projectActions.PackageActions.Distinct().ToDictionary(p => p.Name, p => p.Version), projectActions.ProjectReferenceActions.ToList());
                        }
                        catch (Exception ex)
                        {
                            var actionExecutionException = new ActionExecutionException(projectLevelAction.Name, projectLevelAction.Key, ex);
                            projectActionExecution.InvalidExecutions = 1;
                            LogHelper.LogError(actionExecutionException);
                        }
                    }
                }
                if (!string.IsNullOrEmpty(runResult))
                {
                    projectActionExecution.Description = string.Concat(projectActionExecution.Description, ": ", runResult);
                    projectRunActions.Add(projectActionExecution);
                    LogHelper.LogInformation(projectLevelAction.Description);
                }
            }

            if (!actionsPerProject.TryAdd(Constants.Project, projectRunActions))
            {
                LogHelper.LogError(new FilePortingException(Constants.Project, new Exception("Error adding project to actions collection")));
            }

            return(actionsPerProject.ToDictionary(a => a.Key, a => a.Value));
        }