/// <inheritdoc />
        public override Task GenerateAsync(AssemblyTransformationContext context, IProgress <Diagnostic> progress, CancellationToken cancellationToken)
        {
            var s = Service;

            void GenerateCallback()
            {
                // TODO: TBD: sprinkles in a bit of Validation as well...
                // TODO: TBD: would be better if 'Validation' actually supported a more Fluent style...
                Assumes.Present(s);

                // A lot goes in behind the Descriptor parsing the Protocol Buffer file into a usable form.
                var pd      = s.Descriptor;
                var visitor = s.CodeGenerationVisitor;

                Assumes.Present(pd);
                Assumes.Present(visitor);

                // Then we want to Visit the Descriptor to glean the critical details for CG.
                visitor.Visit(pd);

                // ReSharper disable once RedundantEmptyObjectOrCollectionInitializer
                var cgd = new CodeGeneratorDescriptor {
                };

                Assumes.NotNullOrEmpty(visitor.CompilationUnits);
                Assumes.NotNullOrEmpty(cgd.CompilationUnits);

                foreach (var(k, x) in visitor.CompilationUnits)
                {
                    // TODO: TBD: potential future direction for CGR, we may work by adding a dictionary of CompilationUnitSyntax instances...
                    Verify.Operation(k != Guid.Empty, $"Dictionary key '{nameof(k)}' should not be Empty.");
                    Assumes.NotNull(x);

                    cgd.CompilationUnits.Add(x);
                }

                Assumes.Present(Descriptors);

                Descriptors.Add(cgd);
            }

            return(Task.Run(GenerateCallback, cancellationToken));
        }
Пример #2
0
        /// <summary>
        /// Now, this is a fairly trivial, if a bit naive Code Generation implementation.
        /// We are solely interested in making sure that fundamental building blocks of
        /// Code Generation, such as inserting a Leading Preamble Text, are taking place
        /// successfully.
        /// </summary>
        /// <inheritdoc />
        public override Task GenerateAsync(AssemblyTransformationContext context, IProgress <Diagnostic> progress
                                           , CancellationToken cancellationToken)
        {
            // ReSharper disable InconsistentNaming
            const string Foo       = nameof(Foo);
            const string FruitKind = nameof(FruitKind);
            const string Apple     = nameof(Apple);
            const string Orange    = nameof(Orange);
            const string Banana    = nameof(Banana);
            const string Lime      = nameof(Lime);
            const string Lemon     = nameof(Lemon);
            const string Cherry    = nameof(Cherry);

            // ReSharper restore InconsistentNaming

            IEnumerable <CodeGeneratorDescriptor> Generate()
            {
                yield return(new CodeGeneratorDescriptor
                {
                    CompilationUnits =
                    {
                        CompilationUnit()
                        .WithMembers(SingletonList <MemberDeclarationSyntax>(
                                         GetNameSpace(Foo)
                                         .WithMembers(SingletonList <MemberDeclarationSyntax>(
                                                          GetEnumeration(FruitKind, Apple, Orange, Banana, Lime, Lemon, Cherry)
                                                          ))
                                         ))
                    }
                });
            }

            void RunGenerate()
            {
                foreach (var d in Generate())
                {
                    Descriptors.Add(d);
                }
            }

            return(Task.Run(RunGenerate, cancellationToken));
        }