使用msbuild pack目标时是否可以获取形式为1.2.3.4 + Commit的nuspec版本?

时间:2019-01-01 20:09:21

标签: msbuild nuget version pack nuspec

我们有两种方法来生成nuget包:

  1. 带有显式的nuspec文件。
  2. 使用msbuild nuspec属性。

我正在探索第二种可能性。该演示项目位于github-https://github.com/MarkKharitonov/MSBuildPackNuspecVersionDemo.git

运行build.ps1打印以下行:

Building ...
Packing ...
Dll Product Version = 1.2.3.4+8d50ec0
Nuspec Version = 1.2.3.4

我想知道是否有可能像DLL产品版本一样使Nuspec版本包括提交。

重要文件是:

Directory.Build.props

<Project>
  <PropertyGroup>
    <SourceControlInformationFeatureSupported>true</SourceControlInformationFeatureSupported>

    <Version>1.2.3.4</Version>
    <RepositoryType>git</RepositoryType>
    <RepositoryBranch>master</RepositoryBranch>
  </PropertyGroup>
  <Target Name="InitializeSourceControlInformation" />
</Project>

build.ps1

$ErrorActionPreference = "Stop"
$LocateMSBuild = $true
if (Get-Command msbuild -ErrorAction SilentlyContinue)
{
    $MSBuildVersion = [Version](msbuild /nologo /version)
    $LocateMSBuild = $MSBuildVersion.Major -lt 15
    if (!$LocateMSBuild)
    {
        $MSBuild = "msbuild"
    }
}

if ($LocateMSBuild)
{
    $MSBuildHome = @("Enterprise", "Professional", "BuildTools", "Community") |ForEach-Object {
        "C:\Program Files (x86)\Microsoft Visual Studio\2017\$_\MSBuild\15.0"
    } |Where-Object { Test-Path "$_\bin\msbuild.exe" } | Select-Object -First 1

    if (!$MSBuildHome)
    {
        throw "Failed to locate msbuild 15"
    }

    $MSBuild = "$MSBuildHome\bin\msbuild.exe"
}

$Properties = @{
    SourceRevisionId = $(git rev-parse --short HEAD)
    RepositoryUrl = $(git remote get-url origin)
}

$MSBuildProperties = $Properties.GetEnumerator() | Where-Object { 
    $_.Value
} | ForEach-Object { 
    "/p:{0}={1}" -f $_.Key,$_.Value 
}

Write-Host "Building ..."
&$MSBuild /restore /v:q /nologo /nr:false $MSBuildProperties
if ($LastExitCode)
{
    exit $LastExitCode
}

Write-Host "Packing ..."
Remove-Item src\bin\Debug\*nupkg -ErrorAction SilentlyContinue
&$MSBuild /v:q /nologo /nr:false $MSBuildProperties /t:pack
if ($LastExitCode)
{
    exit $LastExitCode
}

$Dll = "src\bin\Debug\netstandard2\PackVersionTest.dll"
$VersionInfo = (Get-Item $Dll).VersionInfo
"Dll Product Version = $($VersionInfo.ProductVersion)"

$Nuspec = "src\obj\Debug\PackVersionTest.1.2.3.4.nuspec"
$Pattern = '.*<version>(.+)</version>'
$NuspecVersion = (Select-String -path $Nuspec -Pattern $Pattern) -replace $Pattern,'$1'
"Nuspec Version = $NuspecVersion"

1 个答案:

答案 0 :(得分:0)

我相信变化 <Version>1.2.3.4</Version> <Version>1.2.3.4+$(SourceRevisionId)</Version> 应该执行此操作,因为构建脚本将git commit哈希作为名为SourceRevisionId的msbuild属性传递。