我们有两种方法来制作nuget包:
-
有一个显式的nuspec文件。
-
使用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"