Skip to content

C# SourceGenerator helper which helps you query your files, and adds LINQ support

Notifications You must be signed in to change notification settings

roeibajayo/SourceGeneratorQuery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SourceGeneratorQuery

C# class library that helps you query the GeneratorExecutionContext, and also adds LINQ support.

You can read about Source Generators on Microsoft's site.

Installation

This library is distributed via NuGet.

PM> Install-Package SourceGeneratorQuery

Quick Start

//..
        public void Execute(GeneratorExecutionContext context)
        {
            var classes = context
                .NewQuery() // start new query
                .WithPath("./Social") // filter search on specific path
                .GetClasses()
                .WithName(x => x.EndsWith("Client")); // classes ends with 'Client'

            var syntaxNodes = classes.Select(x => x.SyntaxNode); // get the syntax nodes if you want

            // example of iteration
            foreach (var c in classes)
            {
                var publicMethodsWithMyAttributeAndStartsWithGet = c.GetMethods()
                            .WithAttribute("MyAttribute")
                            .WithPublic()
                            .WithName(name => name.StartsWith("Get"));

                var methodsSyntaxNodes = publicMethodsWithMyAttributeAndStartsWithGet.Select(x => x.SyntaxNode);

                var stringifyMethods = publicMethodsWithMyAttributeAndStartsWithGet
                    .Select(x => $"{string.Join(" ", x.Modifiers)} {x.Name}({string.Join(", ", x.Parameters.Select(p => $"{p.Type} {p.Name}"))})")
                    .ToArray();
            }
        }
//..

Examples

See /src/examples folder for more examples.

About

C# SourceGenerator helper which helps you query your files, and adds LINQ support

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages