AArnott / CodeGeneration.Roslyn

Assists in performing Roslyn-based code generation during a build.
Microsoft Public License
408 stars 59 forks source link

Processing generators in a specific order #123

Closed codeprogression closed 5 years ago

codeprogression commented 5 years ago

Given the following attributes...

[AttributeUsage(AttributeTargets.Class)]
[CodeGenerationAttribute(typeof(Step1Generator))]
[Conditional("CodeGeneration")]
public class Step1Attribute : Attribute
{}

[AttributeUsage(AttributeTargets.Class)]
[CodeGenerationAttribute(typeof(Step2Generator))]
[Conditional("CodeGeneration")]
public class Step2Attribute : Attribute
{}

[AttributeUsage(AttributeTargets.Class)]
[CodeGenerationAttribute(typeof(Step3Generator))]
[Conditional("CodeGeneration")]
public class Step3Attribute : Attribute
{}

All classes with the Step1 attribute should be processed first, then Step2, then Step3. The output of each step would be used a later step (memoized in-memory, in temporary files, or in files to be included in compilation).

What strategy would you recommend to ensure that code is generated in a specific order? (I am not asking for a code change, just for ideas.)

codeprogression commented 5 years ago

My initial thought is to write my own custom Engine implementation which would require me to create a custom tool implementation.

I noticed that extracting the engine into its own project was a pretty recent change. Is it possible that you were considering enabling injection of alternate engine implementations?

amis92 commented 5 years ago

Sooo, in general, as you've discovered, it's not a supported scenario.

It is however an interesting idea to allow custom engine implementations.

I'll just warn you that creating such dependencies will probably result in a couple compilations.

As you might now, essentially the Engine compiles the project, and runs generators on the Compilation. If you want to re-create the Compilation after every generator runs, it's going to be quite expensive (in terms of time taken).

amis92 commented 5 years ago

Closed in favour of #137