Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a target before the CoreBuild?

I'm adding a custom .tt template generation target to my project to run before CoreBuild, and there appear to be 2 ways of doing it:

<Project...>
    <Target Name="TransformOnBuild" AfterTargets="BeforeBuild">
</Project>

and

<Project...>
    <Target Name="TransformOnBuild" BeforeTargets="CoreBuild">
</Project>

If my target should run before my project is built, as the project relies on it, would it be better for me to use the latter? I've seen the former used to do things like generate text templates, but it seems like an unreliable way to do it because it might get run after CoreBuild, which is too late. Or is there some reason why AfterTargets="BeforeBuild" is still guaranteed to run before the core build?

I've also seen BeforeTargets="BeforeBuild" which will build even earlier. Is this a better place to put a `.tt text generation target?

like image 482
Jez Avatar asked Dec 07 '22 18:12

Jez


1 Answers

Building on @stjin's answer, a good solution seems to be using

BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild"

which is what the .net sdk (new-style csproj for .net core / standard projects) is doing for automatic AssemblyInfo.cs generation.

It uses the following comment to explain why:

Note that this must run before every invocation of CoreCompile to ensure that all compiler runs see the generated assembly info. There is at least one scenario involving Xaml where CoreCompile is invoked without other potential hooks such as Compile or CoreBuild, etc., so we hook directly on to CoreCompile. Furthermore, we must run after PrepareForBuild to ensure that the intermediate directory has been created.

Note that the "intermediate directory" (obj/[TargetFramework] in this case) is where the output .cs file is placed in this case which may also be what you might want to do.

like image 132
Martin Ullrich Avatar answered Mar 12 '23 15:03

Martin Ullrich