AArnott / CodeGeneration.Roslyn

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

Question: controlling the build order #234

Open dszryan opened 3 years ago

dszryan commented 3 years ago

how can one ensure CGR is the first to run in the presence of multiple code generation/s in the build pipeline?

amis92 commented 3 years ago

You'll probably want to add the GenerateCodeFromAttributes MSBuild target to a DependsOn property of the other generators' MSBuild target.

https://github.com/AArnott/CodeGeneration.Roslyn/blob/fd1200769415b5428fbd6c5859dbe55cf78f0b7b/src/CodeGeneration.Roslyn.Tool/build/CodeGeneration.Roslyn.Tool.targets#L15-L20

dszryan commented 3 years ago

yup - that worked.

to be precise, took the following steps.

in the project/s with the attributes and the codegen classes:

  1. added the reference to the nuget tool package
    <ItemGroup>
        <PackageReference Include="CodeGeneration.Roslyn.Tool" Version="{replace with actual veetrsion used}" />
    </ItemGroup>
  2. and the following target
    <Target Name="RunCodeGenerationRoslynFirst" BeforeTargets="Build">
        <CallTarget Targets="GenerateCodeFromAttributes" />
    </Target>

NB: added the following to the project with the custom CGR attributes/codegens and not the other generators MSBuild target

if there could be a better way - do advise, else please close the ticket.

amis92 commented 3 years ago

If you can share what exactly is that other generator, I could look and advise.

As is, your solution looks like a hacky one. Maybe a better one can be written.

amis92 commented 3 years ago

https://github.com/AArnott/CodeGeneration.Roslyn/wiki/Features#definitions

Please use terms as defined, so I'll be able to understand you quickly.

For a quick enhancement, you could replace CallTarget, which is rarely a good idea, with DependsOn=Generate... in your custom target.