NuGet 发布管理
NuGet release management
我第一次做了一些我想在 NuGet 上分享的东西,但我不知道这 "release preparation" 是怎么做到的。
目前我的以下步骤是:
- 手动将
AssemblyInfo.cs
中我的主程序集 (MyAssembly
) 的程序集版本从 1.0.0.0
更改为 1.1.0.0
- 在 Visual Studio
中构建主程序集发布版本
- 打包我的主程序集的版本 (
nuget pack -prop configuration=release
== MyAssembly.1.1.0.0.nupkg
)
- 将此包发布到 NuGet (
nuget push MyAssembly.1.1.0.0.nupkg
)
- 为引用
MyAssembly
的程序集更新 NuGet 包(例如,MyAssembly.Web.Mvc
使用 NuGet 包管理器从 v1.0.0.0
-> v1.1.0.0
获取其 MyAssembly
更新) 在 packages.config 中定义
- 将
MyAssembly.Web.Mvc
的版本 AssemblyInfo.cs
更改为 1.1.0.0
,构建发布,NuGet 打包并发布
- 对我的所有引用程序集重复步骤 6
这是 PITA 的一些步骤,总有一天我会犯错误并破坏发布。
这让我想到,我是否遗漏了一个关键点,我做错了吗?
更新
基于@ialekseev 提供的知识,我现在创建了这个:
建造.ps1
Param(
[Parameter(Mandatory=$true)]
[string]$version,
[string]$configuration = "Release",
[boolean]$tests = $false,
[boolean]$publish = $false,
[boolean]$pack = $false,
[string]$outputFolder = "build\packages"
)
# Include build functions
. "./BuildFunctions.ps1"
# The solution we are building
$solution = "NerveFramework.sln"
$assemblies = "NerveFramework", "NerveFramework.Web", "NerveFramework.Web.Mvc", "NerveFramework.Web.WebApi"
# Start by changing the assembly version
Write-Host "Changing the assembly versions to '$version'..."
$assemblyInfos = Get-ChildItem $assemblies -Filter "AssemblyInfo.cs" -Recurse | Resolve-Path -Relative
foreach ($assemblyInfo in $assemblyInfos) {
ChangeAssemblyVersion $assemblyInfo $version
}
# Build the entire solution
Write-Host "Cleaning and building $solution (Configuration: $configuration)"
BuildSolution $solution $configuration
# Change dependency version on all depending assemblies
Write-Host "Changing the NerveFramework(s) NuGet Spec version dependencies to '$version'..."
$nuspecs = Get-ChildItem $assemblies -Filter "NerveFramework*.nuspec" -Recurse | Resolve-Path -Relative
foreach ($nuspec in $nuspecs) {
ChangeNugetSpecDependencyVersion $nuspec "NerveFramework" $version
}
# Pack the assemblies and move to output folder
if ($pack) {
Write-Host "Packaging projects..."
$projects = Get-ChildItem $assemblies -Filter "NerveFramework*.csproj" -Recurse | Resolve-Path -Relative
foreach ($project in $projects) {
PackProject $project $configuration $outputFolder
}
}
# Publish the assemblies
if ($publish) {
Write-Host "Publishing packages..."
$packages = Get-ChildItem $outputFolder -Filter "*$version.nupkg" -Recurse | Resolve-Path -Relative
foreach ($package in $packages) {
PublishPackage $package
}
}
构建函数。ps1
Function BuildSolution() {
Param(
[Parameter(Mandatory=$true)]
[string]$solution,
[Parameter(Mandatory=$true)]
[string]$configuration
)
# Set the path to the .NET folder in order to use "msbuild.exe"
$env:PATH = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /t:Clean"
Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /clp:ErrorsOnly"
}
Function ChangeAssemblyVersion() {
Param(
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string]$publishVersion
)
Write-Host "-- Updating '$filePath' to version '$publishVersion'"
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyVersion = 'AssemblyVersion("' + $publishVersion + '")';
$assemblyFileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyFileVersion = 'AssemblyFileVersion("' + $publishVersion + '")';
(Get-Content $filePath -Encoding utf8) | ForEach-Object {
% { $_ -Replace $assemblyVersionPattern, $assemblyVersion } |
% { $_ -Replace $assemblyFileVersionPattern, $assemblyFileVersion }
} | Set-Content $filePath
}
Function ChangeNugetSpecDependencyVersion() {
Param(
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string]$packageId,
[Parameter(Mandatory=$true)]
[string]$publishVersion
)
[xml] $toFile = (Get-Content $filePath)
$nodes = $toFile.SelectNodes("//package/metadata/dependencies/dependency[starts-with(@id, $packageId)]")
if ($nodes) {
foreach ($node in $nodes) {
$nodeId = $node.id
Write-Host "-- Updating '$nodeId' in '$filePath' to version '$publishVersion'"
$node.version = "[" + $publishVersion +"]"
$toFile.Save($filePath)
}
}
}
Function PackProject() {
Param(
[Parameter(Mandatory=$true)]
[string]$project,
[Parameter(Mandatory=$true)]
[string]$configuration,
[Parameter(Mandatory=$true)]
[string]$outputFolder
)
if (!(Test-Path -Path $outputFolder)) {
New-Item $outputFolder -Type Directory
}
Write-Host "-- Packaging '$project'"
Invoke-Expression ".nuget\NuGet.exe pack $project -OutputDirectory '$outputFolder' -Prop Configuration=$configuration"
}
Function PublishPackage() {
Param(
[Parameter(Mandatory=$true)]
[string]$package
)
Write-Host "-- Publishing '$package'"
Invoke-Expression ".nuget\NuGet.exe push $package"
}
运行 喜欢 .\Build -version 1.2.0.0 -pack $true
你需要自动化你的程序,因为,是的,手动执行是非常容易出错的过程。
我通常会编写 powershell/cmd 可以执行以下步骤的脚本:
- 构建解决方案
- 运行 对其进行测试
- 更新程序集版本
- 创建 NuGet 包
- 发布 NuGet 包
然后您可以 运行 在本地自动完成所有这些步骤。您也可以将一些任务委派给您的构建服务器(如果有的话)。例如。 Teamcity 可以负责构建、运行测试,甚至将包发布到 NuGet。无论如何,我喜欢将所有这些脚本放在我的源代码管理中,以便我可以随时在本地 运行 它们。如果您有兴趣,可以在 GitHub 上查看我为我的一个项目编写的脚本。脚本位于 build 文件夹中,publish_local.cmd 是一个入口点。项目有多个 NuGet 依赖项,因此,我相信它与您要完成的任务类似。
我知道最好在答案中直接提供示例,但脚本太多无法包含在此处。如果您有任何问题,请做客
我第一次做了一些我想在 NuGet 上分享的东西,但我不知道这 "release preparation" 是怎么做到的。
目前我的以下步骤是:
- 手动将
AssemblyInfo.cs
中我的主程序集 (MyAssembly
) 的程序集版本从1.0.0.0
更改为1.1.0.0
- 在 Visual Studio 中构建主程序集发布版本
- 打包我的主程序集的版本 (
nuget pack -prop configuration=release
==MyAssembly.1.1.0.0.nupkg
) - 将此包发布到 NuGet (
nuget push MyAssembly.1.1.0.0.nupkg
) - 为引用
MyAssembly
的程序集更新 NuGet 包(例如,MyAssembly.Web.Mvc
使用 NuGet 包管理器从v1.0.0.0
->v1.1.0.0
获取其MyAssembly
更新) 在 packages.config 中定义
- 将
MyAssembly.Web.Mvc
的版本AssemblyInfo.cs
更改为1.1.0.0
,构建发布,NuGet 打包并发布 - 对我的所有引用程序集重复步骤 6
这是 PITA 的一些步骤,总有一天我会犯错误并破坏发布。
这让我想到,我是否遗漏了一个关键点,我做错了吗?
更新
基于@ialekseev 提供的知识,我现在创建了这个:
建造.ps1
Param(
[Parameter(Mandatory=$true)]
[string]$version,
[string]$configuration = "Release",
[boolean]$tests = $false,
[boolean]$publish = $false,
[boolean]$pack = $false,
[string]$outputFolder = "build\packages"
)
# Include build functions
. "./BuildFunctions.ps1"
# The solution we are building
$solution = "NerveFramework.sln"
$assemblies = "NerveFramework", "NerveFramework.Web", "NerveFramework.Web.Mvc", "NerveFramework.Web.WebApi"
# Start by changing the assembly version
Write-Host "Changing the assembly versions to '$version'..."
$assemblyInfos = Get-ChildItem $assemblies -Filter "AssemblyInfo.cs" -Recurse | Resolve-Path -Relative
foreach ($assemblyInfo in $assemblyInfos) {
ChangeAssemblyVersion $assemblyInfo $version
}
# Build the entire solution
Write-Host "Cleaning and building $solution (Configuration: $configuration)"
BuildSolution $solution $configuration
# Change dependency version on all depending assemblies
Write-Host "Changing the NerveFramework(s) NuGet Spec version dependencies to '$version'..."
$nuspecs = Get-ChildItem $assemblies -Filter "NerveFramework*.nuspec" -Recurse | Resolve-Path -Relative
foreach ($nuspec in $nuspecs) {
ChangeNugetSpecDependencyVersion $nuspec "NerveFramework" $version
}
# Pack the assemblies and move to output folder
if ($pack) {
Write-Host "Packaging projects..."
$projects = Get-ChildItem $assemblies -Filter "NerveFramework*.csproj" -Recurse | Resolve-Path -Relative
foreach ($project in $projects) {
PackProject $project $configuration $outputFolder
}
}
# Publish the assemblies
if ($publish) {
Write-Host "Publishing packages..."
$packages = Get-ChildItem $outputFolder -Filter "*$version.nupkg" -Recurse | Resolve-Path -Relative
foreach ($package in $packages) {
PublishPackage $package
}
}
构建函数。ps1
Function BuildSolution() {
Param(
[Parameter(Mandatory=$true)]
[string]$solution,
[Parameter(Mandatory=$true)]
[string]$configuration
)
# Set the path to the .NET folder in order to use "msbuild.exe"
$env:PATH = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /t:Clean"
Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /clp:ErrorsOnly"
}
Function ChangeAssemblyVersion() {
Param(
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string]$publishVersion
)
Write-Host "-- Updating '$filePath' to version '$publishVersion'"
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyVersion = 'AssemblyVersion("' + $publishVersion + '")';
$assemblyFileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyFileVersion = 'AssemblyFileVersion("' + $publishVersion + '")';
(Get-Content $filePath -Encoding utf8) | ForEach-Object {
% { $_ -Replace $assemblyVersionPattern, $assemblyVersion } |
% { $_ -Replace $assemblyFileVersionPattern, $assemblyFileVersion }
} | Set-Content $filePath
}
Function ChangeNugetSpecDependencyVersion() {
Param(
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string]$packageId,
[Parameter(Mandatory=$true)]
[string]$publishVersion
)
[xml] $toFile = (Get-Content $filePath)
$nodes = $toFile.SelectNodes("//package/metadata/dependencies/dependency[starts-with(@id, $packageId)]")
if ($nodes) {
foreach ($node in $nodes) {
$nodeId = $node.id
Write-Host "-- Updating '$nodeId' in '$filePath' to version '$publishVersion'"
$node.version = "[" + $publishVersion +"]"
$toFile.Save($filePath)
}
}
}
Function PackProject() {
Param(
[Parameter(Mandatory=$true)]
[string]$project,
[Parameter(Mandatory=$true)]
[string]$configuration,
[Parameter(Mandatory=$true)]
[string]$outputFolder
)
if (!(Test-Path -Path $outputFolder)) {
New-Item $outputFolder -Type Directory
}
Write-Host "-- Packaging '$project'"
Invoke-Expression ".nuget\NuGet.exe pack $project -OutputDirectory '$outputFolder' -Prop Configuration=$configuration"
}
Function PublishPackage() {
Param(
[Parameter(Mandatory=$true)]
[string]$package
)
Write-Host "-- Publishing '$package'"
Invoke-Expression ".nuget\NuGet.exe push $package"
}
运行 喜欢 .\Build -version 1.2.0.0 -pack $true
你需要自动化你的程序,因为,是的,手动执行是非常容易出错的过程。
我通常会编写 powershell/cmd 可以执行以下步骤的脚本:
- 构建解决方案
- 运行 对其进行测试
- 更新程序集版本
- 创建 NuGet 包
- 发布 NuGet 包
然后您可以 运行 在本地自动完成所有这些步骤。您也可以将一些任务委派给您的构建服务器(如果有的话)。例如。 Teamcity 可以负责构建、运行测试,甚至将包发布到 NuGet。无论如何,我喜欢将所有这些脚本放在我的源代码管理中,以便我可以随时在本地 运行 它们。如果您有兴趣,可以在 GitHub 上查看我为我的一个项目编写的脚本。脚本位于 build 文件夹中,publish_local.cmd 是一个入口点。项目有多个 NuGet 依赖项,因此,我相信它与您要完成的任务类似。
我知道最好在答案中直接提供示例,但脚本太多无法包含在此处。如果您有任何问题,请做客