英文:
Need a PowerShell command to update the .NET Core Project's (Assembly or File) version
问题
使用PowerShell脚本,我能够读取Team Foundation Server工作区的版本号。
现在,我想要更新.NET Core项目的Assembly或File版本属性...
我正在寻找一个可以更新项目属性的PowerShell命令...这是否可能?
或者一个可以更新appsettings.json文件数据中的某个属性的命令
例如:
"AssemblyVersion": "12345"
如果您愿意帮助我,感谢您抽出时间来查看这个问题。
...如果您正在寻找解决方案但尚无解决方案,对不起
Perry
附注:
我尝试实现的目标相当于Tortoise的"subwcrev"命令,用于更新"AssemblyInfoTemplate"。
英文:
Using PowerShell script, I am able to read the Team Foundation Server workspace version numbers.
Now I would like to update the .NET Core Project's Assembly/or/File version in its properties...
I am looking for a PowerShell command that updates properties of the project .. is that even possible?
Or a command that would update one of the properties in the appsettings.json file's data
Ex:
"AssemblyVersion": "12345"
Thank you for taking the time to look at this if you are looking to help me
...and Sorry if you are looking for a solution and there is none yet
Perry
p.s.
What I am trying to achieve is the equivalent of Tortoise's "subwcrev" command to update the "AssemblyInfoTemplate"
答案1
得分: 0
Yay! 我做到了。
我想补充一下,我大部分信息都是从这个网站获取的 ;)
请查看附带的代码和注释
### TFS历史记录
Set-Alias tfs "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
######### 我尝试过的一些命令,我想保留
### tfs history . /r /noprompt /stopafter:1 /version:W | Out-File -FilePath C:\temp\OUTPUT.txt
### Get-Content "C:\temp\OUTPUT.txt" -tail 1 | Out-File -FilePath "C:\temp\output.csv"
### $variable = Get-Content "C:\temp\OUTPUT.txt" -tail 1 ### 获取最后一行
#==========================================================================
$variable = tfs history . /r /noprompt /stopafter:1 /version:W # 获取TFS的版本
########## 下面的三行是“tfs history”命令的结果
# Changeset User Date Comment
# --------- ----------------- ---------- --------------
# 97504 Dorion, Perry 2023-02-09 v#
# 我需要的是"97504"这个数字
$vlinearray = $variable.split("`r`n") # 按行拆分
$vspacearray = $vlinearray[$vlinearray.Length -1].split(" ") # 将最后一行按空格拆分成字符串
######### 用于调试
# Write-Output $vspacearray[0] #获取最后一行的第一个字符串
###################################################
############ 保存到appsettings.json ############
### 未使用
# $pathToJson = "C:\Users\PDorion\Documents\DevNETCore\PNR\PEA\appsettings.json"
# $a = Get-Content $pathToJson | ConvertFrom-Json
# $a.AssemblyVersion = $vspacearray[0]
# $a | ConvertTo-Json | set-content $pathToJson
### 我需要一种方法来找到运行脚本的路径;我的路径与在同一项目上工作的同事不同
### 获取此脚本的路径,即"C:\Users\PDorion\Documents\DevNETCore\PNR\PEA\"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition;
$scriptName = "\PEA.csproj";
### 从TFS HISTORY获取值
### 将5位数字转换为 #.#.#.## 格式
### 我们的TFS版本号太大,与程序集编号类型不兼容
$splitval = $vspacearray[0].ToCharArray()
$Result = ""
$dot = ""
$dotcnt = 0
foreach ($num in $splitval) {
$Result = "$Result$dot$num"
$dotcnt++
if ($dotcnt -lt 4 ) {
$dot = "."
}else{
$dot = ""
}
}
### Write-Output "Result $Result"
$projFile = "$scriptPath$scriptName";
### 获取项目xml内容,即属性
$config = (Get-Content $projFile) -as [Xml];
$ns = New-Object System.Xml.XmlNamespaceManager($config.NameTable);
$ns.AddNamespace("cs", $config.DocumentElement.NamespaceURI);
### 以下代码有点丑,但能用
$config.DocumentElement.SelectNodes("//cs:AssemblyVersion", $ns) | % {
$node = $_;
$node.InnerText = $Result;
}
$config.DocumentElement.SelectNodes("//cs:FileVersion", $ns) | % {
$node = $_;
$node.InnerText = $Result;
}
### 保存
$config.Save($projFile);
英文:
Yé!
I've done it.
And I would like to add that I got most of my info from this site
See the code attached c/w comments
### TFS HISTORY
Set-Alias tfs "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
######### some commands I tried that I want to keep
### tfs history . /r /noprompt /stopafter:1 /version:W | Out-File -FilePath C:\temp\OUTPUT.txt
### Get-Content "C:\temp\OUTPUT.txt" -tail 1 | Out-File -FilePath "C:\temp\output.csv"
### $variable = Get-Content "C:\temp\OUTPUT.txt" -tail 1 ### gets the last line
#==========================================================================
$variable = tfs history . /r /noprompt /stopafter:1 /version:W # get the version of tfs
########## the three lines below are the result of the "tfs history" command
# Changeset User Date Comment
# --------- ----------------- ---------- --------------
# 97504 Dorion, Perry 2023-02-09 v#
# what I need is the "97504" number
$vlinearray = $variable.split("`r`n") # split into lines
$vspacearray = $vlinearray[$vlinearray.Length -1].split(" ") # split the last line into space separated strings
######### for debugging purposes
# Write-Output $vspacearray[0] #get the first string of the last line
###################################################
############ SAVES to appsettings.json ############
### not being used
# $pathToJson = "C:\Users\PDorion\Documents\DevNETCore\PNR\PEA\appsettings.json"
# $a = Get-Content $pathToJson | ConvertFrom-Json
# $a.AssemblyVersion = $vspacearray[0]
# $a | ConvertTo-Json | set-content $pathToJson
### I needed a way to find the path of the ruinning script; mine differs from my co-workers who work on the same project
### gets path of this script i.e. "C:\Users\PDorion\Documents\DevNETCore\PNR\PEA\"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition;
$scriptName = "\PEA.csproj";
### get the value from TFS HISTORY
### DOTIFY the 5 digit number ==> #.#.#.## format
### our TFS version numbers are too big and do not jive well with Assembly number type
$splitval = $vspacearray[0].ToCharArray()
$Result = ""
$dot = ""
$dotcnt = 0
foreach ($num in $splitval) {
$Result = "$Result$dot$num"
$dotcnt++
if ($dotcnt -lt 4 ) {
$dot = "."
}else{
$dot = ""
}
}
### Write-Output "Result $Result"
$projFile = "$scriptPath$scriptName";
### get Project xml content i.e. the properties
$config = (Get-Content $projFile) -as [Xml];
$ns = New-Object System.Xml.XmlNamespaceManager($config.NameTable);
$ns.AddNamespace("cs", $config.DocumentElement.NamespaceURI);
### the following is ugly but it works
$config.DocumentElement.SelectNodes("//cs:AssemblyVersion", $ns) | % {
$node = $_;
$node.InnerText = $Result;
}
$config.DocumentElement.SelectNodes("//cs:FileVersion", $ns) | % {
$node = $_;
$node.InnerText = $Result;
}
### save the sucker
$config.Save($projFile);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论