Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 如何在Visual Studio中使用PowerShell获取项目版本?_Visual Studio_Nuget_Powershell 2.0_Post Build Event - Fatal编程技术网

Visual studio 如何在Visual Studio中使用PowerShell获取项目版本?

Visual studio 如何在Visual Studio中使用PowerShell获取项目版本?,visual-studio,nuget,powershell-2.0,post-build-event,Visual Studio,Nuget,Powershell 2.0,Post Build Event,我正在编写一个简单的PowerShell脚本,用于在Post-build事件上创建NuGet包。为此,我想将包版本自动设置为项目版本。目前,它已在我的命令中硬编码: nuget pack .\MyProject.csproj -properties configuration=release -version 1.0.0 -outputdirectory .\NuGet\Packages 是否有办法将上述命令更改为类似以下内容(伪代码): 然后将具有相同版本的包推送到NuGet服务器 nuge

我正在编写一个简单的
PowerShell
脚本,用于在
Post-build
事件上创建
NuGet
包。为此,我想将包版本自动设置为项目版本。目前,它已在我的命令中硬编码:

nuget pack .\MyProject.csproj -properties configuration=release -version 1.0.0 -outputdirectory .\NuGet\Packages
是否有办法将上述命令更改为类似以下内容(伪代码):

然后将具有相同版本的包推送到
NuGet
服务器

nuget push .\MyProject.$currentVersion$.nupkg -configfile .\NuGet.config

您可以通过从Assembly.cs获取projectversion来实现这一点

function Get-ProjectVersion([string] $ProjectName)
{
    $project = Get-Project -Name $ProjectName
    $assemblyPath = $project.ProjectItems.Item("Properties").ProjectItems.Item("AssemblyInfo.cs").Properties.Item("FullPath").Value

    $assemblyInfo = Get-Content -Path $assemblyPath
    if (!$assemblyInfo)
    {
        return $null
    }                

    $versionMatches = [regex]::Match($assemblyInfo, "\[assembly\: AssemblyVersion\(`"((\d+)\.(\d+)\.(\d+)\.?(\d*)){1}`"\)\]")

    if (!$versionMatches -or !$versionMatches.Groups -or $versionMatches.Groups.Count -le 0)
    {
        return $null
    }

    return $versionMatches.Groups[1].Value
}
像这样打电话和使用它

$currentVersion = Get-ProjectVersion -ProjectName "MyProject"
nuget pack ".\MyProject.csproj" -properties "configuration=release" -version "$currentVersion" -outputdirectory ".\NuGet\Packages"
nuget push ".\MyProject.$currentVersion.nupkg" -configfile ".\NuGet.config"
警告:

$project=Get project-Name$ProjectName
只能在Visual Studio中的Package Manager控制台中使用。或者,可以将部件文件位置添加为参数

$currentVersion = Get-ProjectVersion -ProjectName "MyProject"
nuget pack ".\MyProject.csproj" -properties "configuration=release" -version "$currentVersion" -outputdirectory ".\NuGet\Packages"
nuget push ".\MyProject.$currentVersion.nupkg" -configfile ".\NuGet.config"