代码之家  ›  专栏  ›  技术社区  ›  mark

使用msbuild pack target时,是否可以获取nuspec版本的表单1.2.3.4+commit?

  •  0
  • mark  · 技术社区  · 5 年前

    我们有两种方法来制作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
    

    我想知道Nuspec版本是否可以像Dll产品版本一样包含提交。

    重要文件包括:

    目录.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 回复  |  直到 5 年前
        1
  •  0
  •   zivkan    5 年前

    我相信改变 <Version>1.2.3.4</Version> <Version>1.2.3.4+$(SourceRevisionId)</Version> 应该这样做,因为生成脚本将git commit散列作为名为SourceRevisionId的msbuild属性传递。