Gobie by Mike Conrad
Nuget / site data
Details
Info
Name: Gobie
Package Description
Author: Mike Conrad
NuGet: https://www.nuget.org/packages/Gobie/
You can find more details at https://github.com/GobieGenerator/Gobie/
Original Readme
Gobie
Overview
Source generation in C# is a very powerful tool, but its complexity reduces how and where it is used.
This is my attempt to make source generation for low/medium complexity scenarios easily accessible. Gobie allows developers define and use custom source generation without writing any generator code themselves or learning the Roslyn APIs. This happens in two steps.
- Devs define what they want to generate in C#. Typically this would be text templates, along with definitions for what parameters are needed to populate the template.
- From step 1, Gobie creates marker attributes which can be used to tag classes, fields, ... that need code generation.
- Using the marker attributes, devs mark their code with the generated attributes, and provide custom arguments where needed. This step work just like consuming any other source generator.
- Code is generated based on the templates provided.
While this is in early development I'm going to keep a dev log on my blog.
Feedback & Contribution
I am very much looking for feedback at this point. I can see several possible use cases for this approach to generation and am very interested in hearing whether others are interested in this concept or not. There are quite a few remaining technical challenges and substantial development work ahead, so it would be great to learn if this is something the community would find useful.
Contributors are welcome, but given the early state of this project please open an issue so we can discuss anything you are interested in working on.
About
templating for classes , fields ...
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
- ClassGenAddId.cs
This is the CSharp Project that references Gobie
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Gobie" Version="0.5.0-alpha" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Gobie in Program.cs
using GobieDemo;
Person p = new();
p.Name = "Andrei ";
//this comes from Gobie template
p.Id = 1;
This is the use of Gobie in Person.cs
using Gobie;
namespace GobieDemo;
[ClassGenAddId()]
partial class Person
{
public string? Name { get; set; }
}
This is the use of Gobie in ClassGenAddId.cs
using Gobie;
namespace GobieDemo;
[GobieGeneratorName("ClassGenAddId")]
public sealed class ClassGenAddId : GobieClassGenerator
{
[GobieFileTemplate("ID")]
private const string LogString = @"
using System;
namespace {{ClassNamespace}};
partial class {{ClassName}}
{
public int Id { get; set; }
}
";
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Person_ClassGenAddIdAttribute.g.cs
- Person_ClassGenAddIdAttribute_ID.g.cs
- _Gobie.ClassGenAddIdAttribute.g.cs
namespace GobieDemo
{
public partial class Person
{
}
}
using System;
namespace GobieDemo;
partial class Person
{
public int Id { get; set; }
}
namespace Gobie
{
/// <summary> This attribute will cause the generator defined by this thing here to
/// run <see cref = "Gobie.ClassGenAddId"/> to run. </summary>
public sealed class ClassGenAddIdAttribute : global::Gobie.GobieClassGeneratorAttribute
{
public ClassGenAddIdAttribute()
{
}
}
}
Usefull
Download Example (.NET C# )
Share Gobie
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Gobie