diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc6387c..ac27b70 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: CI +name: ci on: push: @@ -8,22 +8,21 @@ on: jobs: build: - runs-on: ubuntu-latest - + strategy: matrix: dotnet: [ '6.x', '7.x', '8.x' ] steps: - - uses: actions/checkout@v3 - - name: Setup .NET - uses: actions/setup-dotnet@v3 - with: - dotnet-version: ${{ matrix.dotnet }} - - name: Restore dependencies - run: dotnet restore src/BuilderGenerator.sln - - name: Build - run: dotnet build --no-restore src/BuilderGenerator.sln - - name: Test - run: dotnet test --no-build --verbosity normal src/BuilderGenerator.sln + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: ${{ matrix.dotnet }} + - name: Restore dependencies + run: dotnet restore src/BuilderGenerator.sln + - name: Build + run: dotnet build --no-restore src/BuilderGenerator.sln + - name: Test + run: dotnet test --no-build --verbosity normal src/BuilderGenerator.sln \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 235a13e..9fa33af 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,6 +7,27 @@ on: jobs: build: runs-on: ubuntu-latest + + strategy: + matrix: + dotnet: [ '6.x', '7.x', '8.x' ] + + steps: + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: ${{ matrix.dotnet }} + - name: Restore dependencies + run: dotnet restore src/BuilderGenerator.sln + - name: Build + run: dotnet build --no-restore src/BuilderGenerator.sln + - name: Test + run: dotnet test --no-build --verbosity normal src/BuilderGenerator.sln + + publish: + needs: build + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/GitVersion.yml b/GitVersion.yml index 9ef6e40..035e5de 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -3,6 +3,7 @@ increment: Minor ignore: sha: [] update-build-number: true +next-version: 2.0.0 branches: master: regex: ^main$ diff --git a/README.md b/README.md index f654514..94afb0c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,9 @@ new MyBuilder().WithValuesFrom(anotherObject) ## Installation ## -BuilderGenerator2 is installed as an analyzer via a NuGet package - for more info on how to install/add this package to your solution: [am4u.BuilderGenerator](https://www.nuget.org/packages/am4u.BuilderGenerator/) +BuilderGenerator2 is installed as an analyzer via a NuGet package. + +For more info on how to install/add this package to your solution, visit nuget.org: [Safalin.BuilderGenerator](https://www.nuget.org/packages/Safalin.BuilderGenerator/) ## Usage ## @@ -37,35 +39,40 @@ After the package has been installed into your project: 1. Create a new partial class that will hold your builder methods. 2. Decorate it with the ```BuilderFor``` attribute, specifying the type of class that the builder is meant to build. For example: -```csharp -[BuilderFor(typeof(Foo))] -public partial class FooBuilder -{ -} -``` + ```csharp + [BuilderFor(typeof(Foo))] + public partial class FooBuilder + { + } + ``` 3. Rebuild your project. The source generator will run and autogenerate methods in a separate partial class file, for each property in the type you specified in Step 2. -You can also add factory methods to your partial class which can craft specific data scenarios: - -```csharp -[BuilderFor(typeof(Foo))] -public partial class FooBuilder -{ - public static FooBuilder Bar() - { - return new FooBuilder() - .WithBar(true); - } - - public static FooBuilder NotBar() - { - return new FooBuilder() - .WithBar(false); - } -} -``` + You can also add factory methods to your partial class which can craft specific data scenarios: + + ```csharp + [BuilderFor(typeof(Foo))] + public partial class FooBuilder + { + public static FooBuilder Bar() + { + return new FooBuilder() + .WithBar(true); + } + + public static FooBuilder NotBar() + { + return new FooBuilder() + .WithBar(false); + } + } + ``` ## Version History ## +- v2.0 + - Rewrote internal source generation logic + - Added XML comments for auto-generated With methods + - Renamed from am4u.BuilderGenerator to Safalin.BuilderGenerator + - v1.1 - Added `WithValuesFrom` fluent method to builders diff --git a/src/BuilderGenerator/ReadMe.md b/src/BuilderGenerator/ReadMe.md index 5600b11..960dd09 100644 --- a/src/BuilderGenerator/ReadMe.md +++ b/src/BuilderGenerator/ReadMe.md @@ -3,5 +3,57 @@ A fork of BuilderGenerator, a .NET Source Generator that is designed to add flue ## What are Builders? [Builders](https://en.wikipedia.org/wiki/Builder_pattern) are an object creation pattern, similar to the [Object Mother](https://martinfowler.com/bliki/ObjectMother.html) pattern. Object Mothers and Builders are most commonly used to create objects for testing, but they can be used anywhere you want "canned" objects. -## Examples and Documentation -For documentation, please see the [GitHub repository](https://github.com/am4u/BuilderGenerator2). +## Example Usage ## + +1. Create a new partial class that will hold your builder methods - for this example, we'll call it ```FooBuilder```, as the class we want to work with is called `Foo`. +2. Decorate it with the ```BuilderFor``` attribute, specifying the type of class that the builder is meant to build. For example: + ```csharp + [BuilderFor(typeof(Foo))] + public partial class FooBuilder + { + } + ``` +3. Rebuild your project. The source generator will run and autogenerate methods in a separate partial class file, for each property in the type you specified in Step 2. + + You can also add factory methods to your partial class which can craft specific data scenarios: + + ```csharp + [BuilderFor(typeof(Foo))] + public partial class FooBuilder + { + public static FooBuilder Bar() + { + return new FooBuilder() + .WithBar(true); + } + + public static FooBuilder NotBar() + { + return new FooBuilder() + .WithBar(false); + } + } + ``` + +## Features +### Record types +With this fork, you can use the `[BuilderFor]` attribute on C# record types: +```csharp +public record MyRecord(bool Hello); + +[BuilderFor(typeof(MyRecord)] +public partial class MyRecordBuilder +{ +} + +var builder = new MyRecordBuilder().WithHello(true).Build(); +``` + +### WithValuesFrom +The new `WithValuesFrom` method allows you to copy values from an existing instance into your builder: +```csharp +new MyBuilder().WithValuesFrom(anotherObject) +``` + +## Documentation & Source Code +For more information, please visit the [GitHub repository](https://github.com/safalin1/BuilderGenerator2).