Lombok.NET icon indicating copy to clipboard operation
Lombok.NET copied to clipboard

Make SourceGeneratorAttributes generated by code generator

Open frg2089 opened this issue 1 year ago • 3 comments

Hi, This is a nice project! However, Could you please move SourceGeneratorAttributes into SourceGenerator?

This will make it purely an analyzer package. The analyzer is not runtime-limited. And they will work on .Net Framework 4.0 and .Net Framework 3.5.

Like this:

context.RegisterPostInitializationOutput(context => context.AddSource(
    "ToStringAttribute.g.cs",
    $$"""
    namespace Lombok.NET;

    /// <summary>
    /// Tells Lombok.NET to generate a ToString implementation for this type. For enums, a ToText method will be added.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
    internal sealed class ToStringAttribute : Attribute
    {
        // members
    }
    """));
Work on old platform

image

frg2089 avatar Jul 05 '24 06:07 frg2089

Even if I did that, the package would still target netstandard2.0, which would make it unusable for .NET Framework 4.0, correct?

CollinAlpert avatar Jul 08 '24 13:07 CollinAlpert

Even if I did that, the package would still target netstandard2.0, which would make it unusable for .NET Framework 4.0, correct?

In fact, a Nuget package reference should be added using something like this.

<PackageReference Include="Lombok.NET" Version="2.4.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>analyzers</IncludeAssets>
</PackageReference>

This will make them work on .Net Framework 4.0

frg2089 avatar Jul 08 '24 14:07 frg2089

Sounds like a reasonable request, I will look into it.

CollinAlpert avatar Jul 09 '24 13:07 CollinAlpert

I am having some trouble, since Lombok.NET internally uses some of the enums which would then be generated by a source generator. I need to see if I can somehow work around that.

CollinAlpert avatar Jul 25 '24 08:07 CollinAlpert

I realize I may have mentioned a difficult feature to implement, the source code generator doesn't seem to have good access to the content generated by the source code generator.

For example, accessing attributes generated by the source code generator via IncrementalGeneratorInitializationContext.SyntaxProvider.ForAttributeWithMetadataName.

If we want to provide support for older versions of the .Net Framework, we should publish the attributes in a separate Nuget package and use a multi-target build.

Translated with DeepL.com (free version)

frg2089 avatar Aug 10 '24 00:08 frg2089