Exemplo n.º 1
0
        private void RunCodeChanges(SyntaxNode root, SourceFileBuildResult sourceFileBuildResult, FileActions currentFileActions, ConcurrentDictionary <string, List <GenericActionExecution> > actionsPerProject)
        {
            ActionsRewriter oneRewriter = new ActionsRewriter(sourceFileBuildResult.SemanticModel, sourceFileBuildResult.PrePortSemanticModel, sourceFileBuildResult.SyntaxGenerator, currentFileActions.FilePath, currentFileActions.AllActions);

            root = oneRewriter.Visit(root);
            var result = root.NormalizeWhitespace().ToFullString();

            if (!_projectConfiguration.IsMockRun)
            {
                File.WriteAllText(sourceFileBuildResult.SourceFileFullPath, result);
            }

            var processedActions = ValidateActions(oneRewriter.allExecutedActions, root);

            processedActions = AddActionsWithoutExecutions(currentFileActions, oneRewriter.allExecutedActions);

            if (!actionsPerProject.TryAdd(sourceFileBuildResult.SourceFileFullPath, processedActions))
            {
                throw new FilePortingException(sourceFileBuildResult.SourceFilePath, new Exception("File already exists in collection"));
            }
        }
Exemplo n.º 2
0
        private void GenerateCodeChanges(SyntaxNode root, SourceFileBuildResult sourceFileBuildResult, FileActions currentFileActions, int fileActionsCount, ConcurrentDictionary <string, List <GenericActionExecution> > actionsPerProject)
        {
            if (currentFileActions != null)
            {
                var normalizedRoot = root.NormalizeWhitespace().ToFullString();
                //If true, and we are doing a full analysis, line endings and spaces need to be normalized:
                //TODO change the condition to be a config value in ProjectConfiguration instead of file count
                if (normalizedRoot != root.ToFullString() && fileActionsCount > 1)
                {
                    File.WriteAllText(sourceFileBuildResult.SourceFileFullPath, normalizedRoot);
                }

                currentFileActions.NodeTokens.ForEach(nodetoken =>
                {
                    nodetoken.AllActions.ForEach(nodeAction => {
                        ActionsRewriter oneRewriter = new ActionsRewriter(sourceFileBuildResult.SemanticModel, sourceFileBuildResult.PrePortSemanticModel, sourceFileBuildResult.SyntaxGenerator, currentFileActions.FilePath, nodeAction);

                        var newRoot    = oneRewriter.Visit(root);
                        var allChanges = newRoot.SyntaxTree.GetChanges(root.SyntaxTree);

                        foreach (var textChange in allChanges)
                        {
                            var fileLinePositionSpan = root.SyntaxTree.GetMappedLineSpan(textChange.Span);
                            var newTextChange        = new TextChange()
                            {
                                FileLinePositionSpan = fileLinePositionSpan, NewText = textChange.NewText
                            };
                            if (!nodetoken.TextChanges.Contains(newTextChange))
                            {
                                nodetoken.TextChanges.Add(newTextChange);
                            }
                        }
                    });
                });
            }
        }
Exemplo n.º 3
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));
        }