CollinAlpert / Lombok.NET

.NET adaptation for Java's Lombok using Source Generators.
MIT License
309 stars 16 forks source link

Make SourceGeneratorAttributes generated by code generator #50

Closed frg2089 closed 2 months ago

frg2089 commented 4 months ago

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](https://github.com/CollinAlpert/Lombok.NET/assets/42184238/939220b9-9e49-4a37-aa3d-d6a8c018c5ed)
CollinAlpert commented 3 months ago

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

frg2089 commented 3 months ago

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

CollinAlpert commented 3 months ago

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

CollinAlpert commented 3 months ago

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.

frg2089 commented 2 months ago

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)