user3618372
user3618372

Reputation: 169

Unable to update a value in msbuild proj file using powershell

I am trying to read a .csproj file using powershell by converting it into xml file to increment WPF app version value. I am able to read the value without any issue and incremented the version value but when I try to save the file with new value, the value doesn't get saved. The code I pasted in this question is to update the QA details.

I am trying to read the below file. How do i update and save file

<Choose>
<When Condition=" '$(BuildEnvironment)' == 'QA' ">
  <PropertyGroup>
    <ApplicationVersion>1.0.0.1</ApplicationVersion>
    <PublishDirectory>C:\TestDirectory</PublishDirectory>
    <InstallUrl>http://testurl/testapp</InstallUrl>
    <ProductName>Test1.QA</ProductName>
    <PublishAssemblyName>Test1.QA</PublishAssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <Tokens Include="ApplicationManifestFileName">
      <ReplacementValue>Test1.QA.application</ReplacementValue>
      <Visible>false</Visible>
    </Tokens>
  </ItemGroup>
</When>
<When Condition=" '$(BuildEnvironment)' == 'Production' ">
  <PropertyGroup>
    <ApplicationVersion>1.0.0.0</ApplicationVersion>
    <PublishDirectory>C:\TestDirectory2</PublishDirectory>
    <InstallUrl>http://test2url/test2app/</InstallUrl>
    <ProductName>Test=2.Prod</ProductName>
    <PublishAssemblyName>Test2.Prod</PublishAssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <Tokens Include="ApplicationManifestFileName">
      <ReplacementValue>Tes2.Prod.application</ReplacementValue>
      <Visible>false</Visible>
    </Tokens>
  </ItemGroup>
</When>

$Test1QAMSBuildFile = 'C:\Directory\Test.csproj'
[xml]$Test1QABuildVersion = Get-Content $Test1QAMSBuildFile
$CurrentQAVersion= $Test1QABuildVersion.Project.Choose.when.PropertyGroup | ? { $_.ProductName -eq 'Test1.QA' } | Select-Object -Property ApplicationVersion
$PropertyVersion= $CurrentQAVersion.ApplicationVersion
$UpdateVersion= $PropertyVersion.split(".")
$major= [int] ($Updateversion[0])
$minor= [int] ($Updateversion[1])
$patch= [int] ($Updateversion[2])
$revision=[int] ($Updateversion[3]) 
$newrevisionversion= $revision+1
$newVersion =( [string] $major )+ "."+ ( [string] $minor) +"."+ ([string]$patch ) +"."+ ([string]$newrevisionversion )
$UpdateVersion ="$newVersion"
$TestQABuildVersion.Save("Test1QAMSBuildFile")`

Upvotes: 0

Views: 561

Answers (2)

WiiBopp
WiiBopp

Reputation: 2840

Below is code that will work on the example you have given. The main thing you need to keep in mind is that you are dealing with an xml object rather than a powershell object. Selecting the node to be updated requires using SelectSingleNode and specifying an x-path. Using the xml InnerText property allows you to get and set the value. The code below has been tested with the Test.csproj file you provided.

$Test1QAMSBuildFile = 'C:\Directory\Test.csproj'
[xml]$Test1QABuildVersion = Get-Content $Test1QAMSBuildFile
$node = $Test1QABuildVersion.SelectSingleNode("/Choose/When/PropertyGroup/ApplicationVersion[../ProductName = 'Test1.QA']")
$PropertyVersion= $node.InnerText
$UpdateVersion= $PropertyVersion.split(".")
$UpdateVersion[3] = (($UpdateVersion[3] -as [int]) + 1).ToString()
$newVersion = $UpdateVersion -join '.'
$node.InnerText = $newVersion
$Test1QABuildVersion.Save($Test1QAMSBuildFile)

In the case of a real csproj file you will also have to deal with namespaces since the Project element specifies a namespace. I've done bulk modifications to csproj files in the past and it involved quite a learning curve.

Upvotes: 1

TheMadTechnician
TheMadTechnician

Reputation: 36297

After I added the closing </Choose> I removed .Project from line 3, so you may need to alter what I give you to properly apply to your file.

You never updated $TestQABuildVersion, only some other things that referenced it. What you probably want to do is:

$Test1QAMSBuildFile = 'C:\Directory\Test.csproj'
[xml]$Test1QABuildVersion = Get-Content $Test1QAMSBuildFile
$PropertyVersion = $Test1QABuildVersion.Choose.when.PropertyGroup | ? { $_.ProductName -eq 'Test1.QA' } | Select -Expand ApplicationVersion
$UpdateVersion = $PropertyVersion.split(".")
$UpdateVersion[3] = 1+$UpdateVersion[3]
$newVersion = $UpdateVersion -join "."
$Test1QABuildVersion.Choose.when.PropertyGroup | ? { $_.ProductName -eq 'Test1.QA' } | %{$_.ApplicationVersion = $newVersion}
$TestQABuildVersion.Save($Test1QAMSBuildFile)

Upvotes: 2

Related Questions