/// <summary>
        /// Gets a Roslyn workspace for the analyzed results.
        /// </summary>
        /// <param name="analyzerResult">The results from building a Buildalyzer project analyzer.</param>
        /// <param name="addProjectReferences">
        /// <c>true</c> to add projects to the workspace for project references that exist in the same <see cref="AnalyzerManager"/>.
        /// If <c>true</c> this will trigger (re)building all referenced projects. Directly add <see cref="AnalyzerResult"/> instances instead if you already have them available.
        /// </param>
        /// <returns>A Roslyn workspace.</returns>
        public static AdhocWorkspace GetWorkspace(this AnalyzerResult analyzerResult, bool addProjectReferences = false)
        {
            if (analyzerResult == null)
            {
                throw new ArgumentNullException(nameof(analyzerResult));
            }
            AdhocWorkspace workspace = new AdhocWorkspace();

            analyzerResult.AddToWorkspace(workspace, addProjectReferences);
            return(workspace);
        }
示例#2
0
        private Compilation AddProjectReferences(IExecutionContext context, List <ISymbol> symbols, Compilation compilation)
        {
            // Generate a single Workspace and add all of the projects to it
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(new AnalyzerManagerOptions
            {
                LogWriter = log
            });
            AdhocWorkspace      workspace    = new AdhocWorkspace();
            IEnumerable <IFile> projectFiles = context.FileSystem.GetInputFiles(_projectGlobs)
                                               .Where(x => x.Path.Extension == ".csproj" && x.Exists);
            List <Project> projects = new List <Project>();

            foreach (IFile projectFile in projectFiles)
            {
                Project project = workspace.CurrentSolution.Projects.FirstOrDefault(x => new FilePath(x.FilePath).Equals(projectFile.Path));
                if (project != null)
                {
                    Trace.Verbose($"Project {projectFile.Path.FullPath} was already in the workspace");
                }
                else
                {
                    Trace.Verbose($"Creating workspace project for {projectFile.Path.FullPath}");
                    ProjectAnalyzer analyzer = manager.GetProject(projectFile.Path.FullPath);
                    if (context.Bool(CodeAnalysisKeys.OutputBuildLog))
                    {
                        analyzer.AddBinaryLogger();
                    }
                    AnalyzerResult result = ReadWorkspace.CompileProjectAndTrace(analyzer, log);
                    if (result != null)
                    {
                        project = result.AddToWorkspace(workspace);
                        if (!project.Documents.Any())
                        {
                            Trace.Warning($"Project at {projectFile.Path.FullPath} contains no documents, which may be an error (check previous log output for any MSBuild warnings)");
                        }
                    }
                }
                projects.Add(project);
            }
            compilation = AddProjectReferences(projects, symbols, compilation);
            return(compilation);
        }
        /// <inheritdoc />
        protected override IEnumerable <Project> GetProjects(IExecutionContext context, IFile file)
        {
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(new AnalyzerManagerOptions
            {
                LogWriter = log
            });
            ProjectAnalyzer analyzer = manager.GetProject(file.Path.FullPath);

            if (context.Bool(CodeAnalysisKeys.OutputBuildLog))
            {
                analyzer.AddBinaryLogger();
            }
            AnalyzerResult result    = CompileProjectAndTrace(analyzer, log);
            AdhocWorkspace workspace = new AdhocWorkspace();

            result.AddToWorkspace(workspace);
            return(workspace.CurrentSolution.Projects);
        }