Skip to content

Commit 4db84e5

Browse files
author
Roei Bajayo
committed
v1.0.1
1 parent 7150f7f commit 4db84e5

13 files changed

+222
-102
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
11
# SourceGeneratorQuery
2-
C# SourceGenerator Helper, which helps you query your files.
3-
See <code>/examples</code> folder
2+
C# class library that helps you query the GeneratorExecutionContext, and also adds LINQ support.
3+
4+
You can read about Source Generators on [Microsoft's site](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview).
5+
6+
Installation
7+
---
8+
This library is distributed via NuGet.
9+
10+
> PM> Install-Package [SourceGeneratorQuery](https://www.nuget.org/packages/SourceGeneratorQuery)
11+
12+
Quick Start
13+
---
14+
```csharp
15+
//..
16+
public void Execute(GeneratorExecutionContext context)
17+
{
18+
var classes = context
19+
.NewQuery() // start new query
20+
.WithPath("./Social") // filter search on specific path
21+
.GetClasses()
22+
.WithName(x => x.EndsWith("Client")); // classes ends with 'Client'
23+
24+
var syntaxNodes = classes.Select(x => x.SyntaxNode); // get the syntax nodes if you want
25+
26+
// example of iteration
27+
foreach (var c in classes)
28+
{
29+
var publicMethodsWithMyAttributeAndStartsWithGet = c.GetMethods()
30+
.WithAttribute("MyAttribute")
31+
.WithPublic()
32+
.WithName(name => name.StartsWith("Get"));
33+
34+
var methodsSyntaxNodes = publicMethodsWithMyAttributeAndStartsWithGet.Select(x => x.SyntaxNode);
35+
36+
var stringifyMethods = publicMethodsWithMyAttributeAndStartsWithGet
37+
.Select(x => $"{string.Join(" ", x.Modifiers)} {x.Name}({string.Join(", ", x.Parameters.Select(p => $"{p.Type} {p.Name}"))})")
38+
.ToArray();
39+
}
40+
}
41+
//..
42+
```
43+
44+
Examples
45+
---
46+
See <code>/src/examples</code> folder for more examples.

SourceGeneratorQuery.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ VisualStudioVersion = 17.4.33213.308
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorQuery", "src\SourceGeneratorQuery.csproj", "{517DCEE9-57EF-42C6-B162-3BD48F42E884}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C2D1711F-418B-4B0E-8B9E-6682EF305EC1}"
9+
ProjectSection(SolutionItems) = preProject
10+
.gitignore = .gitignore
11+
README.md = README.md
12+
EndProjectSection
13+
EndProject
814
Global
915
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1016
Debug|Any CPU = Debug|Any CPU
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22

3-
namespace SourceGeneratorBuilder.Declarations
3+
namespace SourceGeneratorQuery.Declarations
44
{
55
public class ArgumentDeclaration
66
{
77
public ArgumentDeclaration(AttributeArgumentSyntax node)
88
{
9-
this.node = node;
9+
this.SyntaxNode = node;
1010
}
1111

12-
private readonly AttributeArgumentSyntax node;
12+
public readonly AttributeArgumentSyntax SyntaxNode;
1313

1414
public string Expression =>
15-
node.Expression.ToString();
15+
SyntaxNode.Expression.ToString();
1616
}
1717
}

src/Declarations/AttributeDeclaration.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
using System.Collections.Generic;
55
using System.Linq;
66

7-
namespace SourceGeneratorBuilder.Declarations
7+
namespace SourceGeneratorQuery.Declarations
88
{
99
public class AttributeDeclaration
1010
{
1111
public AttributeDeclaration(AttributeSyntax node)
1212
{
13-
this.node = node;
13+
this.SyntaxNode = node;
1414
}
1515

16-
private readonly AttributeSyntax node;
16+
public readonly AttributeSyntax SyntaxNode;
1717

1818
public string Name =>
19-
node.Name.ToString();
19+
SyntaxNode.Name.ToString();
2020
public IEnumerable<ArgumentDeclaration> Arguments =>
21-
node.ArgumentList?.Arguments.Select(a => new ArgumentDeclaration(a)) ??
21+
SyntaxNode.ArgumentList?.Arguments.Select(a => new ArgumentDeclaration(a)) ??
2222
Array.Empty<ArgumentDeclaration>();
2323
}
2424
}

src/Declarations/MethodDeclaration.cs

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp;
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
4-
using SourceGeneratorBuilder.Declarations;
54
using System;
65
using System.Collections.Generic;
76
using System.Linq;
87

9-
namespace SourceGeneratorBuilder.Declarations
8+
namespace SourceGeneratorQuery.Declarations
109
{
1110
public class MethodDeclaration
1211
{
13-
public MethodDeclaration(BaseMethodDeclarationSyntax node)
12+
public MethodDeclaration(BaseMethodDeclarationSyntax node, MethodDeclaration parent)
1413
{
15-
this.node = node;
14+
this.SyntaxNode = node;
15+
Parent = parent;
1616
}
1717

18-
private readonly BaseMethodDeclarationSyntax node;
18+
public readonly BaseMethodDeclarationSyntax SyntaxNode;
19+
public readonly MethodDeclaration Parent;
1920

2021
public bool IsPublic
2122
{
@@ -51,18 +52,18 @@ public bool IsPublic
5152
public bool IsAsync =>
5253
Modifiers.Contains("async");
5354
public IEnumerable<string> Modifiers =>
54-
node.Modifiers.Select(x => x.Text);
55+
SyntaxNode.Modifiers.Select(x => x.Text);
5556

5657
public string Name
5758
{
5859
get
5960
{
60-
switch (node)
61+
switch (SyntaxNode)
6162
{
6263
case MethodDeclarationSyntax method:
6364
return method.Identifier.ToString();
6465
}
65-
switch (node)
66+
switch (SyntaxNode)
6667
{
6768
case ConstructorDeclarationSyntax method:
6869
return method.Identifier.ToString();
@@ -72,7 +73,7 @@ public string Name
7273
}
7374
set
7475
{
75-
switch (node)
76+
switch (SyntaxNode)
7677
{
7778
case MethodDeclarationSyntax method:
7879
method.WithIdentifier(SyntaxFactory.Identifier(""));
@@ -85,7 +86,7 @@ public string ReturnType
8586
{
8687
get
8788
{
88-
switch (node)
89+
switch (SyntaxNode)
8990
{
9091
case MethodDeclarationSyntax method:
9192
return method.ReturnType.ToString();
@@ -97,19 +98,66 @@ public bool ReturnTypeIsNullable
9798
{
9899
get
99100
{
100-
switch (node)
101+
switch (SyntaxNode)
101102
{
102103
case MethodDeclarationSyntax method:
103104
return method.ReturnType.IsNotNull;
104105
}
105106
return false;
106107
}
107108
}
108-
public IEnumerable<AttributeDeclaration> Attributes => node.AttributeLists
109+
public IEnumerable<AttributeDeclaration> Attributes => SyntaxNode.AttributeLists
109110
.SelectMany(x => x.Attributes.Select(a => new AttributeDeclaration(a)));
110111
public IEnumerable<ParameterDeclaration> Parameters =>
111-
node.ParameterList.Parameters.Select(p => new ParameterDeclaration(p));
112+
SyntaxNode.ParameterList.Parameters.Select(p => new ParameterDeclaration(p));
112113
public string Body =>
113-
node.Body.GetText().ToString();
114+
SyntaxNode.Body.GetText().ToString();
115+
}
116+
117+
public static class MethodDeclarationExtentions
118+
{
119+
public static IEnumerable<MethodDeclaration> WithName(this IEnumerable<MethodDeclaration> source,
120+
Func<string, bool> predicate)
121+
{
122+
return source.Where(x => predicate(x.Name));
123+
}
124+
public static IEnumerable<MethodDeclaration> WithAttribute(this IEnumerable<MethodDeclaration> source,
125+
Func<AttributeDeclaration, bool> predicate)
126+
{
127+
return source.Where(x => x.Attributes.Any(a => predicate(a)));
128+
}
129+
public static IEnumerable<MethodDeclaration> WithAttribute(this IEnumerable<MethodDeclaration> source,
130+
string name)
131+
{
132+
return source.Where(x => x.Attributes.Any(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase)));
133+
}
134+
public static IEnumerable<MethodDeclaration> WithPublic(this IEnumerable<MethodDeclaration> source)
135+
{
136+
return source.Where(x => x.IsPublic);
137+
}
138+
public static IEnumerable<MethodDeclaration> WithProtected(this IEnumerable<MethodDeclaration> source)
139+
{
140+
return source.Where(x => x.IsProtected);
141+
}
142+
public static IEnumerable<MethodDeclaration> WithInternal(this IEnumerable<MethodDeclaration> source)
143+
{
144+
return source.Where(x => x.IsInternal);
145+
}
146+
public static IEnumerable<MethodDeclaration> WithPrivate(this IEnumerable<MethodDeclaration> source)
147+
{
148+
return source.Where(x => x.IsPrivate);
149+
}
150+
public static IEnumerable<MethodDeclaration> WithReadonly(this IEnumerable<MethodDeclaration> source)
151+
{
152+
return source.Where(x => x.IsReadonly);
153+
}
154+
public static IEnumerable<MethodDeclaration> WithStatic(this IEnumerable<MethodDeclaration> source)
155+
{
156+
return source.Where(x => x.IsStatic);
157+
}
158+
public static IEnumerable<MethodDeclaration> WithAbstract(this IEnumerable<MethodDeclaration> source)
159+
{
160+
return source.Where(x => x.IsAbstract);
161+
}
114162
}
115163
}

src/Declarations/ParameterDeclaration.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,54 @@
44
using System.Collections.Generic;
55
using System.Linq;
66

7-
namespace SourceGeneratorBuilder.Declarations
7+
namespace SourceGeneratorQuery.Declarations
88
{
99
public class ParameterDeclaration
1010
{
1111
public ParameterDeclaration(ParameterSyntax node)
1212
{
13-
this.node = node;
13+
this.SyntaxNode = node;
1414
}
1515
public ParameterDeclaration(FieldDeclarationSyntax node)
1616
{
17-
this.node = node;
17+
this.SyntaxNode = node;
1818
}
1919
public ParameterDeclaration(PropertyDeclarationSyntax node)
2020
{
21-
this.node = node;
21+
this.SyntaxNode = node;
2222
}
2323
public ParameterDeclaration(MemberDeclarationSyntax node)
2424
{
25-
this.node = node;
25+
this.SyntaxNode = node;
2626
}
2727
public ParameterDeclaration(EnumDeclarationSyntax node)
2828
{
29-
this.node = node;
29+
this.SyntaxNode = node;
3030
}
3131

32-
private readonly SyntaxNode node;
32+
public readonly SyntaxNode SyntaxNode;
3333

34-
public bool IsPublic => !(node is ParameterSyntax) &&
34+
public bool IsPublic => !(SyntaxNode is ParameterSyntax) &&
3535
Modifiers.Contains("public");
36-
public bool IsProtected => !(node is ParameterSyntax) &&
36+
public bool IsProtected => !(SyntaxNode is ParameterSyntax) &&
3737
Modifiers.Contains("protected");
38-
public bool IsInternal => !(node is ParameterSyntax) &&
38+
public bool IsInternal => !(SyntaxNode is ParameterSyntax) &&
3939
Modifiers.Contains("internal");
40-
public bool IsPrivate => !(node is ParameterSyntax) &&
40+
public bool IsPrivate => !(SyntaxNode is ParameterSyntax) &&
4141
(Modifiers.Contains("private") ||
4242
!IsPublic && !IsProtected && !IsInternal);
43-
public bool IsReadonly => !(node is ParameterSyntax) &&
43+
public bool IsReadonly => !(SyntaxNode is ParameterSyntax) &&
4444
Modifiers.Contains("readonly");
45-
public bool IsStatic => !(node is ParameterSyntax) &&
45+
public bool IsStatic => !(SyntaxNode is ParameterSyntax) &&
4646
Modifiers.Contains("static");
47-
public bool IsAbstract => !(node is ParameterSyntax) &&
47+
public bool IsAbstract => !(SyntaxNode is ParameterSyntax) &&
4848
Modifiers.Contains("abstract");
4949
public IEnumerable<string> Modifiers
5050
{
5151
get
5252
{
5353
SyntaxTokenList modifiers = default;
54-
switch (node)
54+
switch (SyntaxNode)
5555
{
5656
case MemberDeclarationSyntax x:
5757
modifiers = x.Modifiers;
@@ -69,7 +69,7 @@ public IEnumerable<AttributeDeclaration> Attributes
6969
get
7070
{
7171
SyntaxList<AttributeListSyntax> attributes = default;
72-
switch (node)
72+
switch (SyntaxNode)
7373
{
7474
case MemberDeclarationSyntax x:
7575
attributes = x.AttributeLists;
@@ -86,7 +86,7 @@ public string Type
8686
{
8787
get
8888
{
89-
switch (node)
89+
switch (SyntaxNode)
9090
{
9191
case ParameterSyntax x:
9292
return x.Type.ToString();
@@ -105,7 +105,7 @@ public bool IsNullable
105105
{
106106
get
107107
{
108-
switch (node)
108+
switch (SyntaxNode)
109109
{
110110
case ParameterSyntax x:
111111
return x.Type.IsNotNull;
@@ -122,7 +122,7 @@ public string Name
122122
{
123123
get
124124
{
125-
switch (node)
125+
switch (SyntaxNode)
126126
{
127127
case ParameterSyntax x:
128128
return x.Identifier.ToString();
@@ -141,7 +141,7 @@ public string DefaultValue
141141
{
142142
get
143143
{
144-
switch (node)
144+
switch (SyntaxNode)
145145
{
146146
case ParameterSyntax x:
147147
return x.Default.Value?.ToString();
@@ -158,7 +158,7 @@ public IEnumerable<KeyValuePair<string, string>> Values
158158
{
159159
get
160160
{
161-
switch (node)
161+
switch (SyntaxNode)
162162
{
163163
case EnumDeclarationSyntax x:
164164
return x.Members.Select(m =>

0 commit comments

Comments
 (0)