示例#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);
        }
示例#2
0
        public async Task <List <SourceFileBuildResult> > Build()
        {
            var results = new List <SourceFileBuildResult>();
            var trees   = new List <SyntaxTree>();

            await Task.Run(() =>
            {
                foreach (var file in _fileInfo)
                {
                    var fileContent = file.Value;
                    var syntaxTree  = CSharpSyntaxTree.ParseText(fileContent, path: file.Key);
                    trees.Add(syntaxTree);
                }
                if (trees.Count != 0)
                {
                    var projectName = Path.GetFileNameWithoutExtension(_projectPath);

                    if (_frameworkMetaReferences?.Any() == true)
                    {
                        _prePortCompilation = CSharpCompilation.Create(projectName, trees, _frameworkMetaReferences);
                    }
                    if (_coreMetaReferences?.Any() == true)
                    {
                        _compilation = CSharpCompilation.Create(projectName, trees, _coreMetaReferences);
                    }
                }


                _fileInfo.Keys.ToList().ForEach(file =>
                {
                    var sourceFilePath = Path.GetRelativePath(_projectPath, file);
                    var fileTree       = trees.FirstOrDefault(t => t.FilePath == file);

                    if (fileTree != null)
                    {
                        var fileResult = new SourceFileBuildResult
                        {
                            SyntaxTree           = fileTree,
                            PrePortSemanticModel = _prePortCompilation?.GetSemanticModel(fileTree),
                            SemanticModel        = _compilation?.GetSemanticModel(fileTree),
                            SourceFileFullPath   = file,
                            SourceFilePath       = sourceFilePath
                        };
                        results.Add(fileResult);
                    }
                    else
                    {
                        Logger.LogError($"Cannot find a syntax tree for {file}");
                    }
                });
            });

            return(results);
        }
示例#3
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);
        }
示例#4
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);
        }