Reputation: 27852
I'm trying to us MSBuild to add a linked file to my .csproj
file.
This is .Net Framework 3.5 (and not 4.0). I mention that because I'm seen some 4.0 specific stuff trying to manipulate the XML.
Here is what I'm starting with:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MySuperCoolClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
This is what I'm trying to get:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MySuperCoolClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
<Link>MyLinkFile.ext</Link>
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
I have:
MSBuild.Community.Tasks.dll
and
MSBuild.ExtensionPack.dll
available.
Any concrete help?
One liner comments like use 'MSBuild.ExtensionPack.Xml.XmlFile' won't be helpful.
But I appreciate any pointers or coded examples immensely.
Upvotes: 3
Views: 8319
Reputation: 27852
Well, I opened up the code for "MSBuild.ExtensionPack.Xml.XmlFile(.cs)" and looked around. Thank goodness for open source.
I figured out..you gotta "build it up". And I had to add a little voodoo trick (with the "MyUniqueKeyHelper123" seen below).
I'll post here. If you're having trouble with "MSBuild.ExtensionPack.Xml.XmlFile(.cs)", get the source code and look at it. You can figure out how to set the properties by looking at the method. It was a little tricky at first, but figure-out-able.
<PropertyGroup>
<MSBuildExtensionPackPath Condition="'$(MSBuildExtensionPackPath)' == ''">.\ExtensionPackFiles</MSBuildExtensionPackPath>
<MSBuildExtensionPackLib>$(MSBuildExtensionPackPath)\MSBuild.ExtensionPack.dll</MSBuildExtensionPackLib>
</PropertyGroup>
<UsingTask AssemblyFile="$(MSBuildExtensionPackLib)" TaskName="MSBuild.ExtensionPack.Xml.XmlFile" />
<Target Name="XmlTest01Target">
<Message Text="MSBuildExtensionPackLib = $(MSBuildExtensionPackLib)" />
<!--
The goal is:
<ItemGroup>
<Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
<Link>MyLinkFile.ext</Link>
</Content>
</ItemGroup>
-->
<!-- Define a custom namespace. I used "peanut" just to show it is any name you give it -->
<ItemGroup>
<Namespaces Include="Mynamespace">
<Prefix>peanut</Prefix>
<Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
</Namespaces>
</ItemGroup>
<!--
Add the <ItemGroup> (new) Element. HOWEVER, since there will probably be multiple <ItemGroup> nodes, tag it with some unique identifier. Will Clean up later.
-->
<XmlFile
TaskAction="AddElement"
Namespaces="@(Namespaces)"
File=".\MyCSharpProjectFile.csproj"
Element="ItemGroup"
Key="MyUniqueKeyHelper123"
Value ="MyUniqueValueHelper123"
XPath="//peanut:Project"
/>
<!--
Add the <Content> (new) Element. With Attribute Value.
-->
<XmlFile
TaskAction="AddElement"
File=".\MyCSharpProjectFile.csproj"
Element="Content"
Key="Include"
Value ="..\..\SomeFunFolder\MyLinkFile.ext"
Namespaces="@(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
<!--
Add the <Content> (new) Element. With Element Value Value.
-->
<XmlFile
TaskAction="AddElement"
File=".\MyCSharpProjectFile.csproj"
Element="Link"
InnerText ="MyLinkFile.ext"
Namespaces="@(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
<!--
Clean up the "unique" attribute to leave clean xml.
-->
<XmlFile
TaskAction="RemoveAttribute"
File=".\MyCSharpProjectFile.csproj"
Element="Link"
Key="MyUniqueKeyHelper123"
Namespaces="@(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
</Target>
Upvotes: 4
Reputation: 32561
Is it feasible for you to use the following?
using System;
using System.Text;
using Microsoft.Build.BuildEngine;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
var fullPathName = @"PathToProjectFile\Project.csproj";
Project project = new Project();
project.Load(fullPathName);
var itemGroup = project.AddNewItemGroup();
var buildItem = itemGroup.AddNewItem("Content", @"..\..\SomeFunFolder\MyLinkFile.ext");
buildItem.SetMetadata("Link", "MyLinkFile.ext");
project.Save(fullPathName, Encoding.UTF8);
}
}
}
Upvotes: 2