无法使用Powershell更新msbuild proj文件中的值 [英] Unable to update a value in msbuild proj file using powershell

查看:79
本文介绍了无法使用Powershell更新msbuild proj文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Powershell读取.csproj文件,方法是将其转换为xml文件以增加WPF应用程序的版本值.我能够毫无问题地读取值并增加版本值,但是当我尝试使用新值保存文件时,该值不会被保存.我在此问题中粘贴的代码是用于更新质量检查详细信息.

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")`

推荐答案

在添加结束符</Choose>之后,我从第3行中删除了.Project,因此您可能需要更改我提供的内容才能正确应用于文件

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.

您从未更新过$TestQABuildVersion,仅更新了引用它的其他内容.您可能想要做的是:

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)

这篇关于无法使用Powershell更新msbuild proj文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆