Beispiel #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 => !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 = 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,
                    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");
            }
        }
Beispiel #2
0
        private ProjectAnalysisResult AnalyzeProject(
            string project, List <AnalyzerResult> analyzers, Dictionary <string, ProjectActions> analysisActions, string targetFramework = "netcoreapp3.1")
        {
            try
            {
                using var analyzer = analyzers.Find((a) => a.ProjectResult?.ProjectFilePath != null &&
                                                    a.ProjectResult.ProjectFilePath.Equals(project));

                var projectActions = new ProjectActions();
                analysisActions.TryGetValue(project, out projectActions);

                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 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, 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 = InvocationExpressionModelToInvocations.AnalyzeResults(
                    sourceFileToCodeEntityDetails, packageResults, recommendationResults, portingActionResults, targetFramework);



                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(),
                    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>()
                });
            }
        }