/// <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(DocumentTransformationContext context, IProgress <Diagnostic> progress , CancellationToken cancellationToken) { const string dispose = nameof(IDisposable.Dispose); const string disposing = nameof(disposing); // ReSharper disable once InconsistentNaming const string IsDisposed = nameof(IsDisposed); IEnumerable <CodeGeneratorDescriptor> Generate() { var namespaceDecl = context.SourceCompilationUnit.DescendantNodesAndSelf().OfType <NamespaceDeclarationSyntax>().Single(); var classDecl = context.SourceCompilationUnit.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().Single(); yield return(new CodeGeneratorDescriptor { CompilationUnits = { CompilationUnit() .WithUsings(SingletonList(GetUsingNamespaceDirective <IDisposable>())) .WithMembers(SingletonList <MemberDeclarationSyntax>( NamespaceDeclaration(namespaceDecl.Name) .WithMembers(SingletonList <MemberDeclarationSyntax>( ClassDeclaration(classDecl.Identifier) .WithModifiers(TokenList(PublicKeywordToken, PartialKeywordToken)) .WithBaseList(BaseList( SingletonSeparatedList <BaseTypeSyntax>( SimpleBaseType(IdentifierName($"I{Buz}")) ) )) .WithMembers(List(new[] { GetDisposeMethodDecl(dispose, disposing), GetIsDisposedPropertyDecl(IsDisposed), GetDisposeImplementationMethodDecl(dispose, IsDisposed) })) )) )) } }); } void RunGenerate() { foreach (var d in Generate()) { Descriptors.Add(d); } } return(Task.Run(RunGenerate, cancellationToken)); }
/// <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(DocumentTransformationContext context, IProgress <Diagnostic> progress , CancellationToken cancellationToken) { IEnumerable <CodeGeneratorDescriptor> Generate() { var namespaceDecl = context.SourceCompilationUnit.DescendantNodesAndSelf().OfType <NamespaceDeclarationSyntax>().Single(); var classDecl = context.SourceCompilationUnit.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().Single(); // ReSharper disable once InconsistentNaming const string Initialize = nameof(Initialize); var other = $"{classDecl.Identifier}"; yield return(new CodeGeneratorDescriptor { CompilationUnits = { CompilationUnit() .WithUsings(SingletonList(GetUsingNamespaceDirective <ICloneable>())) .WithMembers(SingletonList <MemberDeclarationSyntax>( NamespaceDeclaration(namespaceDecl.Name) .WithMembers(SingletonList <MemberDeclarationSyntax>( ClassDeclaration(classDecl.Identifier) .WithModifiers(classDecl.Modifiers) .WithBaseList(BaseList( SingletonSeparatedList <BaseTypeSyntax>( SimpleBaseType(IdentifierName(nameof(ICloneable))) ) )) .WithMembers(List(new[] { GetCopyCtorDecl(other, Initialize), GetInitializeMethodDecl(Initialize, other), GetICloneableCloneMethodDecl(other) })) )) )) } }); } void RunGenerate() { foreach (var d in Generate()) { Descriptors.Add(d); } } return(Task.Run(RunGenerate, cancellationToken)); }