Example #1
0
        private static async Task InstrumentThenCompileProject(
            Project project,
            Config config,
            string outputFilePath,
            Func <long> memberIdGenerator,
            IDictionary <string, string> memberIdsToNames)
        {
            var originalSyntaxTrees = new List <SyntaxTree>();
            var modifiedSyntaxTrees = new List <SyntaxTree>();

            var documentsToInstrument = project.Documents
                                        .Where(d => Filtering.ShouldMutateDocument(d, config))
                                        .Where(d => !d.IsAutomaticallyGenerated());

            foreach (var originalDocument in documentsToInstrument)
            {
                var originalSyntaxTree = await originalDocument.GetSyntaxTreeAsync().ConfigureAwait(false);

                var modifiedSyntaxTree = await Instrumentation.InstrumentDocument(
                    originalSyntaxTree,
                    originalDocument,
                    memberIdsToNames.Add,
                    memberIdGenerator);

                originalSyntaxTrees.Add(originalSyntaxTree);
                modifiedSyntaxTrees.Add(modifiedSyntaxTree);
            }

            var compilation = (await project.GetCompilationAsync().ConfigureAwait(false))
                              .RemoveSyntaxTrees(originalSyntaxTrees)
                              .AddSyntaxTrees(modifiedSyntaxTrees);

            var compilationResult = ProjectCompilation.CompileProject(
                outputFilePath,
                compilation);

            if (!compilationResult.Success)
            {
                var diagnostics = string.Join(Environment.NewLine, compilationResult.Diagnostics);
                throw new Exception(
                          $"Failed to compile project {compilation.AssemblyName}{Environment.NewLine}{diagnostics}");
            }
        }