Skip to content

Commit afe948f

Browse files
authored
Merge pull request #386 from ignatandrei/ShadowWriterProjectInfo
2 parents 19ff8c5 + f66ffb5 commit afe948f

24 files changed

Lines changed: 752 additions & 13 deletions

File tree

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RSCG - 208 Examples of Roslyn Source Code Generators / 14 created by Microsoft /
1+
# RSCG - 209 Examples of Roslyn Source Code Generators / 14 created by Microsoft /
22

33
## Latest Update : 2025-07-26 => 26 July 2025
44

@@ -20,8 +20,30 @@ If you want to be notified each time I add a new RSCG example , please click htt
2020

2121
## Content
2222

23-
Those are the 208 Roslyn Source Code Generators that I have tested you can see and download source code example.
23+
Those are the 209 Roslyn Source Code Generators that I have tested you can see and download source code example.
2424
( including 14 from Microsoft )
25+
### 209. [ShadowWriterProjectInfo](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterProjectInfo) , in the [Builder](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#builder) category
26+
27+
Generated on : 2025-07-24 => 24 July 2025
28+
29+
<details>
30+
<summary>Expand</summary>
31+
32+
33+
34+
Author: Stefan Stolz
35+
36+
Leverage the capabilities of Roslyn source generators to help generate boilerplate code efficiently.
37+
38+
Nuget: [https://www.nuget.org/packages/ShadowWriter/](https://www.nuget.org/packages/ShadowWriter/)
39+
40+
41+
Link: [https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterProjectInfo](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterProjectInfo)
42+
43+
Source: [https://github.com/StefanStolz/ShadowWriter](https://github.com/StefanStolz/ShadowWriter)
44+
45+
</details>
46+
2547
### 208. [AsyncIt](https://ignatandrei.github.io/RSCG_Examples/v2/docs/AsyncIt) , in the [Async](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#async) category
2648

2749
Generated on : 2025-07-26 => 26 July 2025

v2/Generator/all.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,4 @@ Nr,Key,Source,Category
207207
206,ShadowWriterBuilder, https://github.com/StefanStolz/ShadowWriter,Builder
208208
207,AutoInterface, https://github.com/BlackWhiteYoshi/AutoInterface,Interface
209209
208,AsyncIt, https://github.com/oleg-shilo/AsyncIt,Async
210+
209,ShadowWriterProjectInfo, https://github.com/StefanStolz/ShadowWriter,Builder

v2/RSCGExamplesData/GeneratorDataRec.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,5 +1256,11 @@
12561256
"Category":35,
12571257
"dtStart": "2025-07-26T00:00:00",
12581258
"show": true
1259+
},
1260+
{
1261+
"ID":"ShadowWriterProjectInfo",
1262+
"Category": 4,
1263+
"dtStart": "2025-07-24T00:00:00",
1264+
"show": true
12591265
}
12601266
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
<h1>RSCG nr 209 : ShadowWriterProjectInfo</h1>
3+
4+
<h2>Info</h2>
5+
Nuget : <a href="https://www.nuget.org/packages/ShadowWriter/" target="_blank">https://www.nuget.org/packages/ShadowWriter/</a>
6+
7+
<p>You can find more details at : <a href="https://github.com/StefanStolz/ShadowWriter" target="_blank"> https://github.com/StefanStolz/ShadowWriter</a></p>
8+
9+
<p>Author :Stefan Stolz</p>
10+
11+
<p>Source: <a href="https://github.com/StefanStolz/ShadowWriter" target="_blank">https://github.com/StefanStolz/ShadowWriter</a> </p>
12+
13+
<h2>About</h2>
14+
15+
Generating C# code from project attributes
16+
17+
<h2>
18+
How to use
19+
</h2>
20+
<h3>
21+
Add reference to the <a href="https://www.nuget.org/packages/ShadowWriter/" target="_blank">ShadowWriterProjectInfo</a> in the csproj
22+
</h3>
23+
<img src="images/ShadowWriterProjectInfo/DemoAttr.csproj.png" width="580" height="580" />
24+
25+
<h3>This was for me the <b>starting</b> code</h3>
26+
27+
<br />
28+
I have <b>coded</b> the file Program.cs
29+
<br />
30+
<img src="images/ShadowWriterProjectInfo/csFiles/Program.cs.png" width="580" height="580" />
31+
<hr />
32+
<h3>And here are the <i>generated</i> files</h3>
33+
34+
<br />
35+
The file <i>generated</i> is BuilderAttribute.g.cs
36+
<br />
37+
<img src="images/ShadowWriterProjectInfo/generated/BuilderAttribute.g.cs.png" width="580" height="580" />
38+
39+
<br />
40+
The file <i>generated</i> is NullObjectAttribute.g.cs
41+
<br />
42+
<img src="images/ShadowWriterProjectInfo/generated/NullObjectAttribute.g.cs.png" width="580" height="580" />
43+
44+
<br />
45+
The file <i>generated</i> is ShadowWriter.TheProject.g.cs
46+
<br />
47+
<img src="images/ShadowWriterProjectInfo/generated/ShadowWriter.TheProject.g.cs.png" width="580" height="580" />
48+
49+
<p>
50+
You can download the code and this page as pdf from
51+
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterProjectInfo'>
52+
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterProjectInfo
53+
</a>
54+
</p>
55+
56+
57+
<p>
58+
You can see the whole list at
59+
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
60+
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
61+
</a>
62+
</p>
63+

v2/book/list.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</head>
1818
<body>
1919
<h1>
20-
This is the list of 208 RSCG with examples =>
20+
This is the list of 209 RSCG with examples =>
2121
</h1>
2222

2323
<table >
@@ -858,6 +858,10 @@ <h1>
858858
<td>208</td>
859859
<td><a href="examples/AsyncIt.html">AsyncIt</a></td>
860860
</tr>
861+
<tr>
862+
<td>209</td>
863+
<td><a href="examples/ShadowWriterProjectInfo.html">ShadowWriterProjectInfo</a></td>
864+
</tr>
861865
</table>
862866

863867

v2/book/pandocHTML.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ input-files:
222222
- examples/ShadowWriterBuilder.html
223223
- examples/AutoInterface.html
224224
- examples/AsyncIt.html
225+
- examples/ShadowWriterProjectInfo.html
225226

226227
# or you may use input-file: with a single value
227228
# defaults:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"generator":{
3+
"name":"ShadowWriterProjectInfo",
4+
"nuget":[
5+
"https://www.nuget.org/packages/ShadowWriter/"
6+
],
7+
"link":"https://github.com/StefanStolz/ShadowWriter",
8+
"author":"Stefan Stolz",
9+
"source":"https://github.com/StefanStolz/ShadowWriter"
10+
},
11+
"data":{
12+
"goodFor":["Generating C# code from project attributes"],
13+
"csprojDemo":"DemoAttr.csproj",
14+
"csFiles":["Program.cs"],
15+
"excludeDirectoryGenerated":[""],
16+
"includeAdditionalFiles":[""]
17+
},
18+
"links":{
19+
"blog":"",
20+
"video":""
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Leverage the capabilities of Roslyn source generators to help generate boilerplate code efficiently.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# ShadowWriter
2+
3+
[![NuGet](https://img.shields.io/nuget/v/ShadowWriter.svg)](https://www.nuget.org/packages/ShadowWriter)
4+
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5+
[![Build](https://img.shields.io/github/actions/workflow/status/StefanStolz/ShadowWriter/build.yml?branch=main)](https://github.com/StefanStolz/ShadowWriter/actions)
6+
7+
**ShadowWriter** is a Roslyn Source Generator designed to simplify and automate aspects of .NET development.
8+
It currently supports the following features:
9+
10+
## ✨ Features
11+
12+
Samples can be found in the [Source-Code](https://github.com/StefanStolz/ShadowWriter/tree/main/src/ShadowWriter.Sample) or in the [Wiki](https://github.com/StefanStolz/ShadowWriter/wiki).
13+
14+
### 1. Generate Null Objects
15+
The NullObject feature in ShadowWriter provides a simple way to automatically generate null object implementations for interfaces and classes. This pattern is useful for providing default "do nothing" implementations that can help avoid null reference exceptions and simplify code.
16+
17+
#### Usage
18+
To create a null object implementation, simply add the `[NullObject]` attribute to your class:
19+
20+
```csharp
21+
[NullObject]
22+
public partial class ImplementingMyInterface : IMyInterface
23+
{
24+
}
25+
```
26+
27+
### 2. Inject Project Information
28+
Embeds values from the project file (`*.csproj`) directly into your source code.
29+
This is useful for build metadata, version numbers, or project-specific configuration.
30+
31+
#### Available Properties
32+
The generated `TheProject` class provides the following static properties:
33+
34+
| Property | Description | Example |
35+
|----------|-------------|---------|
36+
| `FullPath` | The complete path to the project file | `/path/to/YourProject.csproj` |
37+
| `ProjectDirectory` | The directory containing the project file | `/path/to/` |
38+
| `Name` | The name of the project | `YourProject` |
39+
| `OutDir` | The output directory for compiled artifacts | `/path/to/artifacts/bin/YourProject/debug/` |
40+
| `Version` | The current version of the project | `1.0.0` |
41+
| `RootNamespace` | The root namespace of the project | `YourProject` |
42+
43+
#### Example Usage
44+
45+
```csharp
46+
// Access project information anywhere in your code
47+
Console.WriteLine($"Project Name: {TheProject.Name}");
48+
Console.WriteLine($"Project Version: {TheProject.Version}");
49+
Console.WriteLine($"Project Output Directory: {TheProject.OutDir}");
50+
```
51+
52+
53+
### 3. Experimental: Typed Access to EmbeddedResources
54+
Generates strongly typed wrappers for `EmbeddedResources`, allowing safe and convenient access to resources at runtime.
55+
56+
> ⚠️ Feature #3 is experimental and may change significantly in future versions.
57+
58+
Details can be found in the [Wiki](https://github.com/StefanStolz/ShadowWriter/wiki/ProjectFiles).
59+
60+
### 4. Generate Builders for Records
61+
62+
The **Builder** feature in ShadowWriter automatically generates builder classes for your `record` types. This significantly reduces boilerplate when constructing complex objects, especially with optional and nullable parameters or when you want to use a fluent API pattern for object creation.
63+
64+
#### Usage
65+
66+
To enable builder generation, simply annotate your partial `record` with the `[Builder]` attribute:
67+
68+
```csharp
69+
[Builder]
70+
public partial record WithBuilder(int Number);
71+
```
72+
73+
The generator will create a corresponding builder class (e.g., `WithBuilder.Builder`) with mutable Properties for each Parameter.
74+
75+
#### Examples
76+
77+
A variety of record types are supported:
78+
79+
```csharp
80+
// Record with a single value type
81+
[Builder]
82+
public partial record WithBuilder(int Number);
83+
```
84+
85+
The generated builder enables you to create instances using a clear, chainable API. For example:
86+
87+
```csharp
88+
var builder = new WithBuilder.Builder();
89+
builder.Number = 1;
90+
var item = builder.Build();
91+
```
92+
93+
## 📦 Installation
94+
95+
You can install ShadowWriter via NuGet:
96+
97+
```sh
98+
dotnet package add ShadowWriter
99+
```
100+
101+
⚙️ Usage
102+
ShadowWriter runs automatically during compilation.
103+
No manual setup is needed. Documentation and configuration options will be expanded in future versions.
104+
105+
📄 License
106+
This project is licensed under the MIT License.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{
3+
"$schema": "https://aka.ms/codetour-schema",
4+
"title": "ShadowWriterProjectInfo",
5+
"steps":
6+
[
7+
{
8+
"file": "DemoAttr/DemoAttr.csproj",
9+
"description": "First, we add Nuget [ShadowWriter](https://www.nuget.org/packages/ShadowWriter/) in csproj ",
10+
"pattern": "ShadowWriter"
11+
}
12+
13+
,{
14+
"file": "DemoAttr/Program.cs",
15+
"description": "File Program.cs \r\n>> dotnet run --project DemoAttr/DemoAttr.csproj ",
16+
"pattern": "this is the code"
17+
}
18+
19+
20+
,{
21+
"file": "DemoAttr/obj/GX/ShadowWriter/ShadowWriter.ProjectInfoGenerator/ShadowWriter.TheProject.g.cs",
22+
"description": "Generated File 3 from 3 : ShadowWriter.TheProject.g.cs ",
23+
"line": 1
24+
}
25+
26+
,{
27+
"file": "DemoAttr/obj/GX/ShadowWriter/ShadowWriter.NullObjectGenerator/NullObjectAttribute.g.cs",
28+
"description": "Generated File 2 from 3 : NullObjectAttribute.g.cs ",
29+
"line": 1
30+
}
31+
32+
,{
33+
"file": "DemoAttr/obj/GX/ShadowWriter/ShadowWriter.BuilderGenerator/BuilderAttribute.g.cs",
34+
"description": "Generated File 1 from 3 : BuilderAttribute.g.cs ",
35+
"line": 1
36+
}
37+
38+
],
39+
40+
"ref": "main"
41+
42+
}

0 commit comments

Comments
 (0)