Using Studio's “Custom Tool” in MSBuild

2019-04-21 03:09发布

问题:

I've got a "Custom Tool" for Visual Studio to munge some template files into code. For consistency and portability I'd like to be able to run this template processor from MSBuild when building outside of Visual Studio.

Visual Studio creates the following snippets for the files:

<!-- the template -->
<None Include="Template.in">
  <Generator>Template Processor</Generator>
  <LastGenOutput>Template.in.Designer.cs</LastGenOutput>
</None>
<!-- generated file -->
<Compile Include="Template.in.Designer.cs">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Template.in</DependentUpon>
</Compile>

The problem here is that the Template.in is only processed by the Studio, but not by MsBuild, which may lead to out-of-date Designer.cs files.

Is there a existing MSBuild task that can use the IVsSingleFileGenerator directly (including to load its location from the registry) or do I have to call the processor manually (either by implementing said MSBuild task myself or adapting the processor)?

Interestingly, the Using MSBuild article on the MSDN says:

Single file generators are accessible at design-time only, but MSBuild tasks can be used at design-time and build-time. For maximum flexibility, therefore, use MSBuild tasks to transform and generate code. For more information, see Project Items (Visual Studio SDK).


Update: I've hacked this specific custom tool into a msbuild task, but it's not pretty. I'd still prefer a well maintained generic solution. I've posted the source on my blog.

回答1:

When you specify your custom tool from within VisualStudio, what does it add to your project or solution files? Since they are just msbuild files, you might be able to use the added xml as a template for your own build files.