Exemple #1
0
        public ProjectBuildResult SyntaxOnlyBuild()
        {
            SetSyntaxCompilation();

            ProjectBuildResult projectBuildResult = new ProjectBuildResult
            {
                BuildErrors      = Errors,
                ProjectPath      = ProjectAnalyzer.ProjectFile.Path,
                ProjectRootPath  = Path.GetDirectoryName(ProjectAnalyzer.ProjectFile.Path),
                Compilation      = Compilation,
                IsSyntaxAnalysis = isSyntaxAnalysis
            };

            projectBuildResult.ProjectGuid = ProjectAnalyzer.ProjectGuid.ToString();
            projectBuildResult.ProjectType = ProjectAnalyzer.ProjectInSolution != null?ProjectAnalyzer.ProjectInSolution.ProjectType.ToString() : string.Empty;

            foreach (var syntaxTree in Compilation.SyntaxTrees)
            {
                var sourceFilePath = Path.GetRelativePath(projectBuildResult.ProjectRootPath, syntaxTree.FilePath);
                var fileResult     = new SourceFileBuildResult
                {
                    SyntaxTree         = syntaxTree,
                    SemanticModel      = Compilation.GetSemanticModel(syntaxTree),
                    SourceFileFullPath = syntaxTree.FilePath,
                    SourceFilePath     = sourceFilePath
                };
                projectBuildResult.SourceFileBuildResults.Add(fileResult);
                projectBuildResult.SourceFiles.Add(sourceFilePath);
            }

            return(projectBuildResult);
        }
Exemple #2
0
        private void GetTargetFrameworks(ProjectBuildResult result, Buildalyzer.IAnalyzerResult analyzerResult)
        {
            result.TargetFramework = analyzerResult.TargetFramework;
            var targetFrameworks = analyzerResult.GetProperty(Constants.TargetFrameworks);

            if (!string.IsNullOrEmpty(targetFrameworks))
            {
                result.TargetFrameworks = targetFrameworks.Split(';').ToList();
            }
        }
Exemple #3
0
 private void GetTargetFrameworks(ProjectBuildResult result, Buildalyzer.IAnalyzerResult analyzerResult)
 {
     if (analyzerResult != null)
     {
         result.TargetFramework = analyzerResult.TargetFramework;
         var targetFrameworks = analyzerResult.GetProperty(Constants.TargetFrameworks);
         if (!string.IsNullOrEmpty(targetFrameworks))
         {
             result.TargetFrameworks = targetFrameworks.Split(';').ToList();
         }
     }
     else
     {
         result.TargetFramework  = ProjectAnalyzer.ProjectFile.TargetFrameworks.FirstOrDefault();
         result.TargetFrameworks = ProjectAnalyzer.ProjectFile.TargetFrameworks.ToList();
     }
 }
Exemple #4
0
        public async Task <ProjectBuildResult> Build()
        {
            await SetCompilation();

            ProjectBuildResult projectBuildResult = new ProjectBuildResult
            {
                BuildErrors        = Errors,
                ProjectPath        = _projectPath,
                ProjectRootPath    = Path.GetDirectoryName(_projectPath),
                Project            = Project,
                Compilation        = Compilation,
                PrePortCompilation = PrePortCompilation,
                IsSyntaxAnalysis   = isSyntaxAnalysis,
                PreportReferences  = PrePortMetaReferences,
                MissingReferences  = MissingMetaReferences
            };

            GetTargetFrameworks(projectBuildResult, AnalyzerResult);
            projectBuildResult.ProjectGuid = ProjectAnalyzer.ProjectGuid.ToString();
            projectBuildResult.ProjectType = ProjectAnalyzer.ProjectInSolution != null?ProjectAnalyzer.ProjectInSolution.ProjectType.ToString() : string.Empty;

            foreach (var syntaxTree in Compilation.SyntaxTrees)
            {
                var sourceFilePath = Path.GetRelativePath(projectBuildResult.ProjectRootPath, syntaxTree.FilePath);
                var preportTree    = PrePortCompilation?.SyntaxTrees?.FirstOrDefault(s => s.FilePath == syntaxTree.FilePath);
                var fileResult     = new SourceFileBuildResult
                {
                    SyntaxTree           = syntaxTree,
                    PrePortSemanticModel = preportTree != null?PrePortCompilation?.GetSemanticModel(preportTree) : null,
                                               SemanticModel      = Compilation.GetSemanticModel(syntaxTree),
                                               SourceFileFullPath = syntaxTree.FilePath,
                                               SyntaxGenerator    = SyntaxGenerator.GetGenerator(Project),
                                               SourceFilePath     = sourceFilePath
                };
                projectBuildResult.SourceFileBuildResults.Add(fileResult);
                projectBuildResult.SourceFiles.Add(sourceFilePath);
            }

            if (_analyzerConfiguration != null && _analyzerConfiguration.MetaDataSettings.ReferenceData)
            {
                projectBuildResult.ExternalReferences = GetExternalReferences(projectBuildResult?.Compilation, projectBuildResult?.Project, projectBuildResult?.Compilation?.References);
            }

            return(projectBuildResult);
        }
Exemple #5
0
        public async Task <ProjectBuildResult> IncrementalBuild(string filePath, ProjectBuildResult projectBuildResult)
        {
            await Task.Run(() =>
            {
                var languageVersion = LanguageVersion.Default;
                if (projectBuildResult.Compilation is CSharpCompilation compilation)
                {
                    languageVersion = compilation.LanguageVersion;
                }

                var fileContents = File.ReadAllText(filePath);
                var updatedTree  = CSharpSyntaxTree.ParseText(SourceText.From(fileContents), path: filePath, options: new CSharpParseOptions(languageVersion));

                var syntaxTree        = Compilation.SyntaxTrees.FirstOrDefault(syntaxTree => syntaxTree.FilePath == filePath);
                var preportSyntaxTree = Compilation.SyntaxTrees.FirstOrDefault(syntaxTree => syntaxTree.FilePath == filePath);

                Compilation        = Compilation.RemoveSyntaxTrees(syntaxTree).AddSyntaxTrees(updatedTree);
                PrePortCompilation = PrePortCompilation?.RemoveSyntaxTrees(preportSyntaxTree).AddSyntaxTrees(updatedTree);

                var oldSourceFileBuildResult = projectBuildResult.SourceFileBuildResults.FirstOrDefault(sourceFile => sourceFile.SourceFileFullPath == filePath);
                projectBuildResult.SourceFileBuildResults.Remove(oldSourceFileBuildResult);

                var sourceFilePath = Path.GetRelativePath(projectBuildResult.ProjectRootPath, filePath);
                var preportTree    = PrePortCompilation?.SyntaxTrees?.FirstOrDefault(s => s.FilePath == syntaxTree.FilePath);
                var fileResult     = new SourceFileBuildResult
                {
                    SyntaxTree           = updatedTree,
                    PrePortSemanticModel = preportTree != null ? PrePortCompilation?.GetSemanticModel(preportTree) : null,
                    SemanticModel        = Compilation.GetSemanticModel(updatedTree),
                    SourceFileFullPath   = syntaxTree.FilePath,
                    SyntaxGenerator      = SyntaxGenerator.GetGenerator(Project),
                    SourceFilePath       = sourceFilePath
                };

                projectBuildResult.SourceFileBuildResults.Add(fileResult);
                projectBuildResult.Compilation        = Compilation;
                projectBuildResult.PrePortCompilation = PrePortCompilation;
            });


            return(projectBuildResult);
        }
Exemple #6
0
        private ExternalReferences GetExternalReferences(ProjectBuildResult projectResult)
        {
            ExternalReferences externalReferences = new ExternalReferences();

            if (projectResult != null && projectResult.Compilation != null)
            {
                var project = projectResult.Project;
                var projectReferencesIds = project.ProjectReferences != null?project.ProjectReferences.Select(pr => pr.ProjectId).ToList() : null;

                var projectReferences = projectReferencesIds != null?project.Solution.Projects.Where(p => projectReferencesIds.Contains(p.Id)) : null;

                var projectReferenceNames = projectReferences != null?projectReferences.Select(p => p.Name).ToHashSet <string>() : null;

                externalReferences.ProjectReferences.AddRange(projectReferences.Select(p => new ExternalReference()
                {
                    Identity         = p.Name,
                    AssemblyLocation = p.FilePath,
                    Version          = p.Version.ToString()
                }));

                LoadProjectPackages(externalReferences, Directory.GetParent(project.FilePath).FullName);

                var compilation = projectResult.Compilation;
                var externalReferencesMetaData = compilation.ExternalReferences;

                foreach (var externalReferenceMetaData in externalReferencesMetaData)
                {
                    try
                    {
                        var symbol = compilation.GetAssemblyOrModuleSymbol(externalReferenceMetaData) as IAssemblySymbol;

                        var filePath          = externalReferenceMetaData.Display;
                        var name              = Path.GetFileNameWithoutExtension(externalReferenceMetaData.Display);
                        var externalReference = new ExternalReference()
                        {
                            AssemblyLocation = filePath
                        };

                        if (symbol != null && symbol.Identity != null)
                        {
                            externalReference.Identity = symbol.Identity.Name;
                            externalReference.Version  = symbol.Identity.Version != null?symbol.Identity.Version.ToString() : string.Empty;

                            name = symbol.Identity.Name;
                        }

                        var nugetRef = externalReferences.NugetReferences.FirstOrDefault(n => n.Identity == name);

                        if (nugetRef == null)
                        {
                            nugetRef = externalReferences.NugetReferences.FirstOrDefault(n => filePath.ToLower().Contains(string.Concat(Constants.PackagesDirectoryIdentifier, n.Identity.ToLower(), ".", n.Version)));
                        }

                        if (nugetRef != null)
                        {
                            //Nuget with more than one dll?
                            nugetRef.AssemblyLocation = filePath;

                            //If version isn't resolved, get from external reference
                            if (string.IsNullOrEmpty(nugetRef.Version) || !Regex.IsMatch(nugetRef.Version, @"([0-9])+(\.)([0-9])+(\.)([0-9])+"))
                            {
                                nugetRef.Version = externalReference.Version;
                            }
                        }
                        else if (filePath.Contains(Common.Constants.PackagesDirectoryIdentifier, System.StringComparison.CurrentCultureIgnoreCase))
                        {
                            externalReferences.NugetDependencies.Add(externalReference);
                        }
                        else if (!projectReferenceNames.Any(n => n.StartsWith(name)))
                        {
                            externalReferences.SdkReferences.Add(externalReference);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, "Error while resolving reference {0}", externalReferenceMetaData);
                    }
                }
            }
            return(externalReferences);
        }