|
| 1 | +# ShadowWriter |
| 2 | + |
| 3 | +[](https://www.nuget.org/packages/ShadowWriter) |
| 4 | +[](LICENSE) |
| 5 | +[](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. |
0 commit comments