iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💭

InjectableGenerator: The Source Generator of Your Dreams

に公開1件

InjectableGenerator is a source generator that allows you to inject custom logic into the source generation process. It is created by Antigravity and also works in Unity.

How to use

The definition of the Generate method is quite long, but you don't need to memorize it because you can use code fixes by entering [assembly: InjectableGenerator(typeof(...))].

using InjectableGenerator;
using System.Linq;
using Test;

[assembly: InjectableGenerator(typeof(CSharpScript))]

namespace Test
{
    public class CSharpScript
    {
        // Executed for all types within the assembly
        public static bool Generate(
            System.Type type,
            bool isPartial,
            bool isRecord,
            out string? info,
            out string? warning,
            out string? error,
            out string? source)
        {
            info = warning = error = source = null;

            // * If an error occurs with typeof, check using .Name instead
            if (type == typeof(<Target Type>))
            {
                source = "// The string set here becomes the output of the generator directly\n"
                       + "// " + string.Join(", ", type.GetFields().Select(x => x.Name));

                // Return true if you want to generate source
                return true;
            }

            // It is not mutually exclusive, so you can issue warnings and errors simultaneously
            error = "NOT ALLOWED";
            return false;
        }
    }
}

How it looks when running

You can trigger an error by specifying a message in error. The error will also appear correctly in the Unity console. (Compilation will fail.)

Use cases

Due to its architecture, there is a subtle issue where "you cannot access the source generated within the same assembly."

Additionally:

  • The code is compiled entirely on the analyzer (state where there are two assemblies: the target and the duplicate)
  • Generate is executed against "types within the assembly compiled on the analyzer"
    • FullyQualifiedName can be used, but it is in an invalid state "within the generated code"
  • The result is generated within the analyzer's target assembly
    • AI says ".NET Standard 2.0 does not support Unload in System.Runtime.Loader"
      • A state where a new assembly is loaded into memory every time the analyzer runs (presumably

These are some points of concern.

Therefore, while source generation is possible, a practical use case might be checking code conventions that cannot be caught by interfaces (e.g., implementing IDisposable despite having IDisposable members).

If the IDE becomes sluggish, measures such as splitting assemblies or referencing the package only on CI may be necessary.

Made by Antigravity

Google Antigravity is quite impressive! It created almost everything based on my rough instructions. Being able to set the pace of the work myself is also a huge plus.

It even automatically generates reports that I would have to explicitly instruct other AIs to produce. In fact, it displays edit types like [MODIFY], which is exactly the format I expected—it’s almost as if it’s reading my mind.

Apart from the tab completion being a bit annoying, the editor itself is satisfactory.

Conclusion

I’m sure many of you wish that C# had macros, but in C#, instead of macros, you need to use a source generator called Roslyn.

Unlike macros, Roslyn is arguably too high-performance—or perhaps it’s more that it makes you feel like you just want to generate code quickly without worrying about syntax trees—so I tried having an AI write the code for me... well, I’m not quite sure about it. I think the concept is solid, but it feels difficult in terms of its underlying mechanism.

https://www.nuget.org/packages/InjectableGenerator

That’s all. Thank you for your hard work.

Discussion

radian-jpradian-jp

ソースジェネレータに処理を注入は面白い発想ですね。
なんか色々応用出来そうな気がします。

1