Can I add an msbuild import element with DTE.Project?
I'm working on a VS plugin for handling a new test type. One of the things we want to开发者_运维百科 do is add an MSBuild import to the project file when one of our tests is in the test project, to run our custom build tasks.
I can add the import element by using Microsoft.Build.BuildEngine.Project.Imports, but if I save the project file via the BuildEngine object I get a "File has been modified outside of Visual Studio" warning. Adding new Imports to the Project.Imports collection doesn't seem to mark the project as dirty in Visual Studio though, so I can't rely on VS to save the file correctly normally.
Is there any way I can access this part of MSBuild functionality through the DTE.Project or VSLangProj.Project objects?
Thanks.
I'd suggest you add a fixed Import to your .csproj and decide inside that .targets whether to execute the tests or not:
Your .csproj
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="CustomTest.targets" Condition="Exists('CustomTest.targets')" />
Note the Condition
to check if the .targets is available.
Your CustomTest.targets
Determine which tests to run by setting the according conditions.
<Project DefaultTargets="RunTests" xmlns="...">
<ItemGroup>
<MyCustomTests Include="Test_A" Condition="Exists('Test_A.cs')" />
<MyCustomTests Include="Test_B" Condition="Exists('Test_B.cs')" />
</ItemGroup>
<Target Name="RunTests" Condition="@(MyCustomTests)!=''">
<Message Text="Running Test %(MyCustomTests.Identity)" />
</Target>
</Project>
You could even extend your MyCustomTests Item with some metadata you may need for running your test:
...
<ItemGroup>
<MyCustomTests Include="Test_A" Condition="Exists('Test_A.cs')">
<TestType>Boundary</TestType>
</MyCustomTests>
<MyCustomTests Include="Test_B" Condition="Exists('Test_B.cs')">
<TestType>SQLInjection</TestType>
</MyCustomTests>
</ItemGroup>
...
<Message Text="Running %(MyCustomTests.TestType) Test %(MyCustomTests.Identity)" />
...
精彩评论