Esempio n. 1
0
        private ProjectAnalysisResult AnalyzeProject(
            string project, string solutionFileName, List <AnalyzerResult> analyzers, List <ProjectResult> analysisActions, bool isIncremental = false, string targetFramework = DEFAULT_TARGET)
        {
            try
            {
                var analyzer = analyzers.Find((a) => a.ProjectResult?.ProjectFilePath != null &&
                                              a.ProjectResult.ProjectFilePath.Equals(project));

                var projectFeatureType = analysisActions.Find((a) => a.ProjectFile != null &&
                                                              a.ProjectFile.Equals(project))?.FeatureType.ToString();

                var projectActions = analysisActions.FirstOrDefault(p => p.ProjectFile == project)?.ProjectActions ?? new ProjectActions();

                if (analyzer == null || analyzer.ProjectResult == null)
                {
                    _logger.LogError("Unable to build {0}.", project);
                    return(null);
                }

                var sourceFileToCodeTokens = analyzer.ProjectResult.SourceFileResults.Select((sourceFile) =>
                {
                    return(SourceFileToCodeTokens(sourceFile));
                }).ToDictionary(p => p.Key, p => p.Value);

                var sourceFileToCodeEntityDetails = CodeEntityModelToCodeEntities.Convert(sourceFileToCodeTokens, analyzer);

                var namespaces = sourceFileToCodeEntityDetails.Aggregate(new HashSet <string>(), (agg, cur) =>
                {
                    agg.UnionWith(cur.Value.Select(i => i.Namespace).Where(i => i != null));
                    return(agg);
                });

                var targetframeworks = analyzer.ProjectResult.TargetFrameworks.Count == 0 ?
                                       new List <string> {
                    analyzer.ProjectResult.TargetFramework
                } : analyzer.ProjectResult.TargetFrameworks;

                var nugetPackages = analyzer.ProjectResult.ExternalReferences.NugetReferences
                                    .Select(r => CodeEntityModelToCodeEntities.ReferenceToPackageVersionPair(r))
                                    .ToHashSet();
                var nugetPackageNameLookup = nugetPackages.Select(package => package.PackageId).ToHashSet();

                var subDependencies = analyzer.ProjectResult.ExternalReferences.NugetDependencies
                                      .Select(r => CodeEntityModelToCodeEntities.ReferenceToPackageVersionPair(r))
                                      .ToHashSet();

                var sdkPackages = namespaces.Select(n => new PackageVersionPair
                {
                    PackageId         = n,
                    Version           = "0.0.0",
                    PackageSourceType = PackageSourceType.SDK
                })
                                  .Where(pair =>
                                         !string.IsNullOrEmpty(pair.PackageId) &&
                                         !nugetPackageNameLookup.Contains(pair.PackageId));

                var allPackages = nugetPackages
                                  .Union(subDependencies)
                                  .Union(sdkPackages)
                                  .ToList();

                Dictionary <PackageVersionPair, Task <PackageDetails> > packageResults;

                if (isIncremental)
                {
                    packageResults = _handler.GetNugetPackages(allPackages, solutionFileName, isIncremental: true, incrementalRefresh: true);
                }
                else
                {
                    packageResults = _handler.GetNugetPackages(allPackages, null, isIncremental: false, incrementalRefresh: false);
                }

                var recommendationResults = _recommendationHandler.GetApiRecommendation(namespaces.ToList());

                var packageAnalysisResults = nugetPackages.Select(package =>
                {
                    var result = PackageCompatibility.IsCompatibleAsync(packageResults.GetValueOrDefault(package, null), package, _logger, targetFramework);
                    var packageAnalysisResult = PackageCompatibility.GetPackageAnalysisResult(result, package, targetFramework);
                    return(new Tuple <PackageVersionPair, Task <PackageAnalysisResult> >(package, packageAnalysisResult));
                }).ToDictionary(t => t.Item1, t => t.Item2);

                var portingActionResults = ProjectActionsToRecommendedActions.Convert(projectActions);

                var SourceFileAnalysisResults = CodeEntityModelToCodeEntities.AnalyzeResults(
                    sourceFileToCodeEntityDetails, packageResults, recommendationResults, portingActionResults, targetFramework);
                var compatibilityResults = AnalysisUtils.GenerateCompatibilityResults(SourceFileAnalysisResults, analyzer.ProjectResult.ProjectFilePath, analyzer.ProjectBuildResult?.PrePortCompilation != null);

                return(new ProjectAnalysisResult
                {
                    ProjectName = analyzer.ProjectResult.ProjectName,
                    ProjectFilePath = analyzer.ProjectResult.ProjectFilePath,
                    TargetFrameworks = targetframeworks,
                    PackageReferences = nugetPackages.ToList(),
                    ProjectReferences = analyzer.ProjectResult.ExternalReferences.ProjectReferences.ConvertAll(p => new ProjectReference {
                        ReferencePath = p.AssemblyLocation
                    }),
                    PackageAnalysisResults = packageAnalysisResults,
                    IsBuildFailed = analyzer.ProjectResult.IsBuildFailed() || analyzer.ProjectBuildResult.IsSyntaxAnalysis,
                    Errors = analyzer.ProjectResult.BuildErrors,
                    ProjectGuid = analyzer.ProjectResult.ProjectGuid,
                    ProjectType = analyzer.ProjectResult.ProjectType,
                    FeatureType = projectFeatureType,
                    SourceFileAnalysisResults = SourceFileAnalysisResults,
                    MetaReferences = analyzer.ProjectBuildResult.Project.MetadataReferences.Select(m => m.Display).ToList(),
                    PreportMetaReferences = analyzer.ProjectBuildResult.PreportReferences,
                    ProjectRules = projectActions.ProjectRules,
                    VisualBasicProjectRules = projectActions.VbProjectRules,
                    ExternalReferences = analyzer.ProjectResult.ExternalReferences,
                    ProjectCompatibilityResult = compatibilityResults
                });
            }
            catch (Exception ex)
            {
                _logger.LogError("Error while analyzing {0}, {1}", project, ex);
                return(new ProjectAnalysisResult
                {
                    ProjectName = Path.GetFileNameWithoutExtension(project),
                    ProjectFilePath = project,
                    TargetFrameworks = new List <string>(),
                    PackageReferences = new List <PackageVersionPair>(),
                    ProjectReferences = new List <ProjectReference>(),
                    PackageAnalysisResults = new Dictionary <PackageVersionPair, Task <PackageAnalysisResult> >(),
                    IsBuildFailed = true,
                    Errors = new List <string> {
                        string.Format("Error while analyzing {0}, {1}", project, ex)
                    },
                    ProjectGuid = null,
                    ProjectType = null,
                    SourceFileAnalysisResults = new List <SourceFileAnalysisResult>()
                });
            }
            finally
            {
                CommonUtils.RunGarbageCollection(_logger, "PortingAssistantAnalysisHandler.AnalyzeProject");
            }
        }