Reputation: 53
I have a custom script that generates C# source files to be included as source files for the library being built. I am currently doing something like this in my .csproj
:
<!-- This automatically handles code generation needed by this library -->
<Target Name="codegen" BeforeTargets="GenerateAdditionalSources">
<Message Text="Running code generators" Importance="high" />
<Exec command="echo This will be a script that generates .cs files" />
</Target>
This calls the script and generates the .cs
files in a generated
directory under my project folder, but those files aren't seen by msbuild until the next time dotnet build
is run.
What target should I use to avoid needing to re-run dotnet build
?
Upvotes: 0
Views: 1140
Reputation: 5208
What you actually want to ask is how to add your newly generated files to the set of files that are passed to the compiler.
Your files need to be added to the Compile
Item collection.
In your codegen
target after the exec
task, add an Include
of the generated .cs
files to the Compile
Item.
Example:
<Target Name="codegen" BeforeTargets="BeforeCompile">
<Message Text="Running code generators" Importance="high" />
<Exec command="echo This will be a script that generates .cs files" />
<ItemGroup>
<Compile Include="generated\*.cs" KeepDuplicates="false" />
</ItemGroup>
</Target>
To be more readable, you might consider using BeforeCompile
on your target (as in the example).
The GenerateAdditionalSources
target itself is actually defined with BeforeTargets="BeforeCompile"
. Using BeforeCompile
explains your intent that the codegen
target is performed before compiling.
So that MSBuild can determine if codegen
needs to be performed you should consider adding Inputs
and Outputs
to the target.
Upvotes: 3