/// <summary>
        /// Override to handle more than the basic <see cref="Compilation.GetDiagnostics"/>
        /// validation. <see cref="Diagnostic"/> validation is the first thing we must rule out.
        /// Because if there is any problem actually compiling, then there is not much point
        /// performing any further validation or analysis.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void CompilationManager_OnEvaluateCompilation(object sender, CompilationDiagnosticEventArgs e)
        {
            // TODO: TBD: which we might capture in some sort of options...
            const DiagnosticSeverity minimumSeverity = Error;

            bool FilterDiagnosticSeverity(Diagnostic diagnostic) => diagnostic.Severity >= minimumSeverity;

            e.Compilation.AssertIsAssignableFrom <Compilation>().AssertSame(e.Compilation);

            var diagnostics = e.Diagnostics.AssertNotNull().Where(FilterDiagnosticSeverity).ToList();

            diagnostics.ForEach(ReportDiagnostic);

            diagnostics.Any().AssertFalse();
        }
        protected override void CompilationManager_OnEvaluateCompilation(object sender, CompilationDiagnosticEventArgs e)
        {
            base.CompilationManager_OnEvaluateCompilation(sender, e);

            //// TODO: TBD: may in fact make the base abstract...
            //base.CompilationManager_OnEvaluateCompilation(sender, e);

            // TODO: TBD: in and of itself Creating ReferenceService and DocumentTransformation is based on the Tool project...
            // TODO: TBD: perhaps which could be refactored to a better place...
            // TODO: TBD: fluent assertions notwithstanding...

            // TODO: TBD: do we need any ReferencePathList elements for this?
            var referencePath = GetRange <string>();
            // TODO: TBD: ditto GeneratorSearchPathList ... ?
            var generatorSearchPath = GetRange <string>();

            AssemblyReferenceServiceManager CreateReferenceService() => new AssemblyReferenceServiceManager(
                TransformationName, IntermediateAssemblyReferenceRegistryFileName
                , referencePath.ToArray(), generatorSearchPath.ToArray());

            var docs = e.Project.Documents;

            // TODO: TBD: this approach is loosely informed by the original project:
            // https://github.com/AArnott/CodeGeneration.Roslyn/blob/master/src/CodeGeneration.Roslyn.Tests/Helpers/CompilationTestsBase.cs#L75

            // ReSharper disable PossibleMultipleEnumeration
            docs.Any().AssertTrue();
            // TODO: TBD: "1" (or however many... may want to capture "sources" as a property which we can use to verify...)
            docs.Count().AssertEqual(1);

            // TODO: TBD: may better leverage the tree(s) in the actual e.Compilation instead...
            docs.First().TryGetSyntaxTree(out var tree).AssertTrue();
            // ReSharper restore PossibleMultipleEnumeration

            var transformation = new DocumentTransformation(CreateReferenceService().AssertNotNull())
            {
                ProjectDirectory = ProjectDirectory,
                InputDocument    = tree.AssertNotNull()
            };

            var compilation = e.Compilation as CSharpCompilation;

            // TODO: TBD: may want to convey task cancellation from other sources than `default´.
            var results = transformation.TransformAsync(compilation, Progress, default).Result;

            TransformedSources = results.Select(x => $"{x.GetText()}").ToArray();
        }
        protected override void CompilationManager_OnEvaluateCompilation(object sender, CompilationDiagnosticEventArgs e)
        {
            base.CompilationManager_OnEvaluateCompilation(sender, e);

            var compilation = e.GetCompilation <CSharpCompilation>().AssertNotNull();
        }
Exemple #4
0
        // TODO: TBD: may furnish Generic type derived from Compilation...
        // TODO: TBD: i.e. CSharpCompilation, but this must also align with the Language, etc...
        /// <summary>
        /// Event handler occurs when <see cref="EvaluateCompilation"/> is requested.
        /// </summary>
        /// <param name="project"></param>
        /// <param name="diagnosticFilter"></param>
        protected virtual void OnEvaluateCompilation(Project project, ICompilationDiagnosticFilter diagnosticFilter)
        {
            var e = new CompilationDiagnosticEventArgs(project, diagnosticFilter);

            EvaluateCompilation?.Invoke(this, e);
        }