Exemplo n.º 1
0
        private List <ProjectResult> AnalyzeActions(List <string> projects, string targetFramework, List <AnalyzerResult> analyzerResults, string pathToSolution)
        {
            _logger.LogInformation("Memory Consumption before AnalyzeActions: ");
            MemoryUtils.LogMemoryConsumption(_logger);

            List <PortCoreConfiguration> configs = new List <PortCoreConfiguration>();

            var anaylyzedProjects = projects.Where(p =>
            {
                var project = analyzerResults.Find((a) => a.ProjectResult?.ProjectFilePath != null &&
                                                   a.ProjectResult.ProjectFilePath.Equals(p));
                return(project != null);
            }).ToList();

            foreach (var proj in anaylyzedProjects)
            {
                PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                {
                    ProjectPath     = proj,
                    UseDefaultRules = true,
                    TargetVersions  = new List <string> {
                        targetFramework
                    },
                    PortCode    = false,
                    PortProject = false
                };

                configs.Add(projectConfiguration);
            }
            var solutionPort   = new SolutionPort(pathToSolution, analyzerResults, configs, _logger);
            var projectResults = solutionPort.Run().ProjectResults.ToList();

            _logger.LogInformation("Memory Consumption after AnalyzeActions: ");
            MemoryUtils.LogMemoryConsumption(_logger);

            return(projectResults);
        }
Exemplo n.º 2
0
        public TestSolutionAnalysis AnalyzeSolution(
            string solutionPath,
            string version,
            Dictionary <string, List <string> > metaReferences = null,
            bool skipCopy    = false,
            bool portCode    = true,
            bool portProject = true)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Utils.GetProjectPaths(solutionPath);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        Dictionary <string, Tuple <string, string> > packages = new Dictionary <string, Tuple <string, string> >
                        {
                            { "Newtonsoft.Json", new Tuple <string, string>("9.0.0", "*") }
                        };
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            SolutionPath    = solutionPath,
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages,
                            PortCode          = portCode,
                            PortProject       = portProject
                        };

                        if (metaReferences != null)
                        {
                            projectConfiguration.MetaReferences = metaReferences.ContainsKey(projectFile) ? metaReferences[projectFile] : null;
                        }

                        solutionPortConfiguration.Add(projectConfiguration);
                        result.ProjectResults.Add(new ProjectResult()
                        {
                            CsProjectPath    = projectFile,
                            ProjectDirectory = Directory.GetParent(projectFile).FullName
                        });
                    }

                    // SolutionPort should remove this extra config because it does not have a matching analyzer result.
                    // Otherwise will hit KeyNotFoundException
                    solutionPortConfiguration.Add(new PortCoreConfiguration
                    {
                        SolutionPath    = solutionPath,
                        ProjectPath     = "fakeproject.csproj",
                        UseDefaultRules = true,
                    });

                    SolutionPort solutionPort = new SolutionPort(solutionPath, solutionPortConfiguration);
                    CopyTestRules();
                    var analysisRunResult = solutionPort.AnalysisRun();

                    StringBuilder str = new StringBuilder();
                    foreach (var projectResult in analysisRunResult.ProjectResults)
                    {
                        Assert.IsTrue(projectResult.ProjectActions.ToSummaryString()?.Length > 0);
                        StringBuilder projectResults = new StringBuilder();
                        projectResults.AppendLine(projectResult.ProjectFile);
                        projectResults.AppendLine(projectResult.ProjectActions.ToString());
                        result.ProjectResults.Where(p => p.CsProjectPath == projectResult.ProjectFile).FirstOrDefault().ProjectAnalysisResult = projectResults.ToString();

                        str.Append(projectResults);
                    }
                    result.SolutionAnalysisResult = str.ToString();


                    var runResult = solutionPort.Run();

                    foreach (var projectFile in result.ProjectResults)
                    {
                        projectFile.CsProjectContent = File.ReadAllText(projectFile.CsProjectPath);
                    }

                    result.SolutionRunResult = runResult;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        public TestSolutionAnalysis AnalyzeSolution(
            string solutionPath,
            string version,
            Dictionary <string, List <string> > metaReferences = null,
            bool skipCopy    = false,
            bool portCode    = true,
            bool portProject = true)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();
            var solutionDir             = Directory.GetParent(solutionPath).FullName;

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Directory.EnumerateFiles(solutionDir, "*.csproj", SearchOption.AllDirectories);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        Dictionary <string, Tuple <string, string> > packages = new Dictionary <string, Tuple <string, string> >
                        {
                            { "Newtonsoft.Json", new Tuple <string, string>("9.0.0", "*") }
                        };
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages,
                            PortCode          = portCode,
                            PortProject       = portProject
                        };

                        if (metaReferences != null)
                        {
                            projectConfiguration.MetaReferences = metaReferences.ContainsKey(projectFile) ? metaReferences[projectFile] : null;
                        }

                        solutionPortConfiguration.Add(projectConfiguration);
                    }

                    // SolutionPort should remove this extra config because it does not have a matching analyzer result.
                    // Otherwise will hit KeyNotFoundException
                    solutionPortConfiguration.Add(new PortCoreConfiguration
                    {
                        ProjectPath     = "fakeproject.csproj",
                        UseDefaultRules = true,
                    });

                    SolutionPort solutionPort = new SolutionPort(solutionPath, solutionPortConfiguration);
                    CopyTestRules();
                    var analysisRunResult = solutionPort.AnalysisRun();

                    foreach (var projectResult in analysisRunResult.ProjectResults)
                    {
                        Assert.IsTrue(projectResult.ProjectActions.ToSummaryString()?.Length > 0);
                    }
                    var runResult = solutionPort.Run();
                    result = GenerateSolutionResult(solutionDir, analysisRunResult, runResult);
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        public TestSolutionAnalysis AnalyzeSolution(string solutionName, string tempDir, string downloadLocation, string version, bool skipCopy = false)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();

            var sourceDir   = Directory.GetParent(Directory.EnumerateFiles(downloadLocation, solutionName, SearchOption.AllDirectories).FirstOrDefault());
            var solutionDir = Path.Combine(tempDir, version);

            if (!skipCopy)
            {
                CopyDirectory(sourceDir, new DirectoryInfo(solutionDir));
            }
            else
            {
                solutionDir = tempDir;
            }
            string solutionPath = Directory.EnumerateFiles(solutionDir, solutionName, SearchOption.AllDirectories).FirstOrDefault();

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Directory.EnumerateFiles(solutionDir, "*.csproj", SearchOption.AllDirectories);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        ProjectResult project = new ProjectResult();

                        Dictionary <string, string> packages = new Dictionary <string, string>();
                        packages.Add("Newtonsoft.Json", "*");
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages
                        };

                        //project.CsProjectContent = File.ReadAllText(projectFile);
                        project.ProjectDirectory = Directory.GetParent(projectFile).FullName;
                        project.CsProjectPath    = projectFile;

                        result.ProjectResults.Add(project);

                        solutionPortConfiguration.Add(projectConfiguration);
                    }

                    SolutionPort solutionPort      = new SolutionPort(solutionPath, solutionPortConfiguration);
                    var          analysisRunResult = solutionPort.AnalysisRun();

                    foreach (var projectAction in analysisRunResult.Values)
                    {
                        Assert.IsTrue(projectAction.ToSummaryString()?.Length > 0);
                    }

                    StringBuilder str = new StringBuilder();
                    foreach (var k in analysisRunResult.Keys)
                    {
                        StringBuilder projectResults = new StringBuilder();
                        projectResults.AppendLine(k);
                        projectResults.AppendLine(analysisRunResult[k].ToString());
                        result.ProjectResults.Where(p => p.CsProjectPath == k).FirstOrDefault().ProjectAnalysisResult = projectResults.ToString();

                        str.Append(projectResults);
                    }

                    result.SolutionAnalysisResult = str.ToString();

                    var runResult = solutionPort.Run();
                    result.SolutionRunResult = runResult;

                    foreach (var project in result.ProjectResults)
                    {
                        project.CsProjectContent = File.ReadAllText(project.CsProjectPath);
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Ports a list of projects
        /// </summary>
        /// <param name="projectPaths">List of projects paths</param>
        /// <param name="solutionPath">Path to solution file</param>
        /// <param name="targetFramework">Target framework to be used when porting</param>
        /// <param name="upgradeVersions">List of key/value pairs where key is package and value is version number</param>
        /// <returns>A PortingProjectFileResult object, representing the result of the porting operation</returns>
        public List <PortingResult> ApplyProjectChanges(
            List <string> projectPaths, string solutionPath, string targetFramework,
            Dictionary <string, string> upgradeVersions)
        {
            _logger.LogInformation("Applying porting changes to {0}", projectPaths);

            var results = new List <PortingResult>();
            List <PortCoreConfiguration> configs = new List <PortCoreConfiguration>();

            projectPaths.ForEach((proj) =>
            {
                configs.Add(new PortCoreConfiguration()
                {
                    ProjectPath       = proj,
                    UseDefaultRules   = true,
                    PackageReferences = upgradeVersions,
                    TargetVersions    = new List <string> {
                        targetFramework
                    }
                });
            });

            var projectFilesNotFound = projectPaths.Where((path) => !File.Exists(path)).ToList();

            projectFilesNotFound.ForEach((path) => results.Add(new PortingResult
            {
                Message     = "File not found.",
                ProjectFile = path,
                ProjectName = Path.GetFileNameWithoutExtension(path),
                Success     = false
            }));

            try
            {
                SolutionPort solutionPort = new SolutionPort(solutionPath, configs, _logger);
                solutionPort.Run();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to port projects{projectPaths}");
                configs.ForEach(config =>
                {
                    if (!projectFilesNotFound.Contains(config.ProjectPath))
                    {
                        results.Add(new PortingResult
                        {
                            Message     = $"porting project with error {ex.Message}",
                            Success     = false,
                            ProjectFile = config.ProjectPath,
                            ProjectName = Path.GetFileNameWithoutExtension(config.ProjectPath)
                        });
                    }
                });
                return(results);
            }

            //TODO Return result from solution run
            projectPaths.Where(p => !projectFilesNotFound.Contains(p)).ToList().ForEach((path) => results.Add(new PortingResult
            {
                ProjectFile = path,
                ProjectName = Path.GetFileNameWithoutExtension(path),
                Success     = true
            }));

            _logger.LogInformation("Completed porting changes to {0}", projectPaths);

            return(results);
        }
        /// <summary>
        /// Ports a list of projects
        /// </summary>
        /// <param name="projectPaths">List of projects paths</param>
        /// <param name="solutionPath">Path to solution file</param>
        /// <param name="targetFramework">Target framework to be used when porting</param>
        /// <param name="upgradeVersions">List of key/value pairs where key is package and value is version number</param>
        /// <returns>A PortingProjectFileResult object, representing the result of the porting operation</returns>
        ///
        public List <PortingResult> ApplyProjectChanges(
            List <ProjectDetails> projects, string solutionPath, string targetFramework,
            bool includeCodeFix,
            Dictionary <string, Tuple <string, string> > upgradeVersions)
        {
            var results = new List <PortingResult>();

            var projectFilesNotFound = projects.Where((p) => !File.Exists(p.ProjectFilePath)).ToList();

            projectFilesNotFound.ForEach((p) => results.Add(new PortingResult
            {
                Message     = "File not found.",
                ProjectFile = p.ProjectFilePath,
                ProjectName = Path.GetFileNameWithoutExtension(p.ProjectFilePath),
                Success     = false
            }));
            projects = projects.Where((p) => File.Exists(p.ProjectFilePath)).ToList();

            var(projectsWithAccess, noAccessPortingResults) = VerifyFileAccess(projects);
            results.AddRange(noAccessPortingResults);
            projects = projectsWithAccess;

            _logger.LogInformation("Applying porting changes to {0}", projects.Select(p => p.ProjectFilePath).ToList());

            List <PortCoreConfiguration> configs = new List <PortCoreConfiguration>();

            projects.Where(p => !p.IsBuildFailed).ToList().ForEach((proj) =>
            {
                var upgradePackages = upgradeVersions
                                      .Where(p => proj.PackageReferences
                                             .Exists(package => package.PackageId == p.Key))
                                      .ToDictionary(t => t.Key, t => t.Value);

                configs.Add(new PortCoreConfiguration()
                {
                    ProjectPath       = proj.ProjectFilePath,
                    UseDefaultRules   = true,
                    PackageReferences = upgradePackages,
                    TargetVersions    = new List <string> {
                        targetFramework
                    },
                    PortCode = includeCodeFix
                });
            });

            try
            {
                SolutionPort solutionPort = new SolutionPort(solutionPath, configs, _logger);
                solutionPort.Run();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to port projects {projects.Select(p => p.ProjectFilePath).ToList()} with error: {ex}");
                configs.ForEach(config =>
                {
                    if (!projectFilesNotFound.Exists(p => p.ProjectFilePath == config.ProjectPath))
                    {
                        results.Add(new PortingResult
                        {
                            Message     = $"porting project with error {ex.Message}",
                            Success     = false,
                            ProjectFile = config.ProjectPath,
                            ProjectName = Path.GetFileNameWithoutExtension(config.ProjectPath)
                        });
                    }
                });
                return(results);
            }

            //TODO Return result from solution run
            projects.Where(p => !projectFilesNotFound.Exists(proj => proj.ProjectFilePath == p.ProjectFilePath) && !p.IsBuildFailed)
            .ToList().ForEach((p) => results.Add(new PortingResult
            {
                ProjectFile = p.ProjectFilePath,
                ProjectName = Path.GetFileNameWithoutExtension(p.ProjectFilePath),
                Success     = true
            }));

            _logger.LogInformation("Completed porting changes to {0}", projects.Select(p => p.ProjectFilePath).ToList());

            return(results);
        }