Exemplo n.º 1
0
        private ProjectAnalysisResult AnalyzeProject(
            string project, List <AnalyzerResult> analyzers)
        {
            try
            {
                var invocationsMethodSignatures = new HashSet <string>();

                using var analyzer = analyzers.Find((a) => a.ProjectResult?.ProjectFilePath != null &&
                                                    a.ProjectResult.ProjectFilePath.Equals(project));

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

                var sourceFileToInvocations = analyzer.ProjectResult.SourceFileResults.Select((sourceFile) =>
                {
                    var invocationsInSourceFile = sourceFile.AllInvocationExpressions();
                    _logger.LogInformation("API: SourceFile {0} has {1} invocations pre-filter", sourceFile.FileFullPath, invocationsInSourceFile.Count());
                    return(KeyValuePair.Create(sourceFile.FileFullPath, invocationsInSourceFile));
                }).ToDictionary(p => p.Key, p => p.Value);

                var sourceFileToCodeEntityDetails = InvocationExpressionModelToInvocations.Convert(sourceFileToInvocations, 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 ProjectReferences = analyzer.ProjectResult.ExternalReferences.ProjectReferences.Count == 0 ?
                                        new List <string>() :
                                        analyzer.ProjectResult.ExternalReferences.ProjectReferences.Select(p => p.AssemblyLocation).ToList();

                var nugetPackages = analyzer.ProjectResult.ExternalReferences.NugetReferences
                                    .Select(r => InvocationExpressionModelToInvocations.ReferenceToPackageVersionPair(r))
                                    .ToHashSet();

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

                var sdkPackages = namespaces.Select(n => new PackageVersionPair {
                    PackageId = n, Version = "0.0.0", PackageSourceType = PackageSourceType.SDK
                });

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

                var packageResults        = _handler.GetNugetPackages(allPackages, null);
                var recommendationResults = _recommendationHandler.GetApiRecommendation(namespaces.ToList());

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

                var SourceFileAnalysisResults = InvocationExpressionModelToInvocations.AnalyzeResults(
                    sourceFileToCodeEntityDetails, packageResults, recommendationResults);

                return(new ProjectAnalysisResult
                {
                    ProjectName = analyzer.ProjectResult.ProjectName,
                    ProjectFilePath = analyzer.ProjectResult.ProjectFilePath,
                    TargetFrameworks = targetframeworks,
                    PackageReferences = nugetPackages.ToList(),
                    ProjectReferences = analyzer.ProjectResult.ExternalReferences.ProjectReferences.Select(p => new ProjectReference {
                        ReferencePath = p.AssemblyLocation
                    }).ToList(),
                    PackageAnalysisResults = packageAnalysisResults,
                    IsBuildFailed = analyzer.ProjectResult.IsBuildFailed(),
                    Errors = analyzer.ProjectResult.BuildErrors,
                    ProjectGuid = analyzer.ProjectResult.ProjectGuid,
                    ProjectType = analyzer.ProjectResult.ProjectType,
                    SourceFileAnalysisResults = SourceFileAnalysisResults
                });
            }
            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>()
                });
            }
        }