Update Xml File (vsixmanifest)

2019-03-01 07:49发布

问题:

I'm creating a Visual Studio 2013 Package (vsix) (shameless plug: pMixins ). As part of my quest to use TeamCity as a continuous integration server, I have configured Team City to build the .vsix Package (Visual Studio Package (vsix) - Team City without Visual Studio installed).

Now I want to configure Team City to set the Version in the VSIX Schema:

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest 
    Version="2.0.0" 
    xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" 
    xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
    <Metadata>
        <Identity  Version="1.0" Language="en-US" Publisher="Company" />

Following the advice in Using msbuild I want to update a config file with values from teamcity and How do I update an XML attribute from an MSBuild script? I have updated Microsoft.VsSDK.targets to use XmlPoke with the following Target:

<Target 
     Name="UpdateVSIXVersion" 
     BeforeTargets="PrepareForBuild" 
     Condition="$(VSIXVersion) != '' And $(VSIXVersion) != '*Undefined*'">

    <Message Text= "Updating VSIX Version" />

    <XmlPoke 
        XmlInputPath="source.extension.vsixmanifest"
        Query="/PackageManifest/Metadata/Identity/@Version"
        Value="$(VSIXVersion)">         
    </XmlPoke>      
 </Target>

I updated Team City with a system Parameter to set VSIXVersion:

But, when I check TeamCity, it made 0 replacements:

How do I get Team City to correctly update the .vsixmanifest xml?

回答1:

After much searching I finally found the XmlPoke expects a namespace when the Xml file contains a namespace, even for the default namespace (Modifying .config File in an MSBuild Project).

However, I couldn't find any documentation for the XmlPoke.Namespaces parameter and the above referenced code didn't work. After much trial an error, I finally got it to work with this:

<Target 
    Name="UpdateVSIXVersion" 
    BeforeTargets="PrepareForBuild" 
    Condition="$(VSIXVersion) != '' And $(VSIXVersion) != '*Undefined*'">

    <Message Text= "Updating VSIX Version" />

    <XmlPoke 
        XmlInputPath="source.extension.vsixmanifest"
        Query="/n:PackageManifest/n:Metadata/n:Identity/@Version"
        Value="$(VSIXVersion)"
        Namespaces="&lt;Namespace Prefix='n' Uri='http://schemas.microsoft.com/developer/vsx-schema/2011' Name='DoNotKnowWhatThisIsFor-ButItIsRequired' /&gt;">
    </XmlPoke>      

Notes:

  • This requires MSBuild 12 to be configured on Team City
  • The Namespaces needs to be escaped.
  • The Name parameter is required, otherwise MSBuild will error out
  • The original XPath query had to be updated with the artificial namespace prefix.

MSBuild file is on Github if anyone needs it: https://github.com/ppittle/pMixins/blob/master/tools/vssdk_tools/v12.0/VSSDK/Microsoft.VsSDK.targets