NuGet发布管理

5
我第一次做了些我想在NuGet上分享的东西,但我不确定如何进行“发布准备”。
目前,我的以下步骤是:
1. 在AssemblyInfo.cs中手动更改我的主程序集(MyAssembly)的程序集版本,从1.0.0.0更改为1.1.0.0 2. 用Visual Studio构建主程序集发布版本 3. 将发布版本打包为我的主程序集 (nuget pack -prop configuration=release== MyAssembly.1.1.0.0.nupkg) 4. 将此包发布到NuGet(nuget push MyAssembly.1.1.0.0.nupkg) 5. 更新引用MyAssembly的程序集的NuGet包(例如:使用NuGet Package Manager将MyAssembly.Web.Mvc的MyAssembly从v1.0.0.0更新为v1.1.0.0,并在packages.config中定义) 6. 将MyAssembly.Web.Mvc的AssemblyInfo.cs版本更改为1.1.0.0,发布生成版本,NuGet打包并发布 7. 对我所有引用程序集重复步骤6
这些步骤非常繁琐,而且有一天我会出错并破坏发布。
这使我想到,我是否漏掉了关键点,或者我做错了什么?
更新:
基于@ialekseev提供的知识,现在我创建了这个:
Build.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
    } 
}

BuildFunctions.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


1
我们使用构建服务器和自定义构建任务来升级版本。构建服务器在提交时发布NuGet。 - Florian Schmidinger
有没有更多你能分享给像我这样的新手的信息? - janhartmann
我是新手...我没有设置它 - Florian Schmidinger
我唯一能告诉你的是我们使用TeamCity和ProGet。 - Florian Schmidinger
这里我找到了一些灵感:https://dev59.com/dmox5IYBdhLWcg3wekMs - Florian Schmidinger
请点击此处查看版本:http://weblogs.asp.net/srkirkland/simple-msbuild-configuration-updating-assemblies-with-a-version-number - Florian Schmidinger
1个回答

2
您需要自动化您的流程,因为手动操作很容易出错。
我通常编写 PowerShell/cmd 脚本来执行以下步骤:
1. 构建解决方案 2. 运行测试 3. 更新程序集版本 4. 创建 NuGet 包 5. 发布 NuGet 包
然后,您可以在本地运行它以自动执行所有这些步骤。如果您有构建服务器,还可以将某些任务委托给它。例如,Teamcity 可以负责构建、运行测试,甚至将包发布到 NuGet。无论如何,我喜欢将所有这些脚本放在源代码控制中,以便随时在本地运行它们。如果您感兴趣,您可以查看我为其中一个项目编写的脚本,链接在 GitHub 上。脚本位于“build”文件夹中,“publish_local.cmd”是入口点。该项目具有多个 NuGet 依赖项,因此我相信它类似于您要完成的任务。
我知道在答案中直接提供示例会更好,但是要包含太多脚本了。如果您有任何问题,请随便问。

有趣,我会试着想明白它。但是乍一看,它似乎正好满足我的需求。如果遇到问题,我会告诉你,但看起来很不错! :-) - janhartmann
你好,我已经更新了我的问题,并附上了一个PowerShell脚本,但是在具有对我的其他程序集的依赖项的程序集上构建过程无法构建 - 显示缺少程序集或引用。 有什么建议吗? 如果您有时间,可以在此处查看我的项目:https://github.com/janhartmann/nerve-framework - janhartmann
@janhartmann,我刚刚fork了你的项目,但是我无法在VS中构建它,因为NerveFramework.Web.WebApi没有引用NerveFramework.Web。在你的解决方案中,你应该有项目之间的引用来正确构建它们。但是,在创建NuGet包时,你可以只创建...WebApi包,并在nuspec文件中指定...Web包作为NuGet依赖项(前提是...Web也在NuGet中存在)。 - alekseevi15
该死,我的错。请耐心等待,我需要先下班才能解决这个问题。 - janhartmann
这是我第一次发布东西,我应该使用“添加引用...”对话框将其添加为引用吗?抱歉有点跑题了。 - janhartmann
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接