NuGet发布管理

时间:2015-01-28 07:16:36

标签: c# .net nuget nuget-package

我第一次做了一些我想在NuGet上分享的东西,但我不确定这个“发布准备”是如何完成的。

目前我的步骤如下:

  1. MyAssemblyAssemblyInfo.cs
  2. 手动将1.0.0.0中主程序集(1.1.0.0)的程序集版本更改为
  3. 在Visual Studio中构建主程序集发行版
  4. 打包我的主程序集(nuget pack -prop configuration=release == MyAssembly.1.1.0.0.nupkg
  5. 的版本
  6. 将此包发布到NuGet(nuget push MyAssembly.1.1.0.0.nupkg
  7. 更新引用MyAssembly的程序集的NuGet程序包(例如MyAssembly.Web.Mvc使用NuGet程序包管理器从MyAssembly - > v1.0.0.0更新v1.1.0.0在packages.config
  8. 中定义
  9. AssemblyInfo.cs的版本MyAssembly.Web.Mvc更改为1.1.0.0,构建版本,NuGet打包并发布
  10. 对我的所有参考组件重复步骤6
  11. 这是PITA的一些步骤,我有一天会犯错并打破发布。

    这让我想到,我错过了一个关键点,我做错了吗?

    更新

    基于@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 个答案:

答案 0 :(得分:2)

您需要自动执行程序,因为,是的,手动执行此操作非常容易出错。

我经常编写powershell / cmd脚本,可以执行以下步骤:

  1. 构建解决方案
  2. 对其进行测试
  3. 更新程序集版本
  4. 创建NuGet包
  5. 发布NuGet包
  6. 然后你可以在本地运行它来自动完成所有这些步骤。您也可以将一些任务委派给Build-server(如果有的话)。例如。 Teamcity可以负责构建,运行测试,甚至将包发布到NuGet。无论如何,我喜欢在我的源代码控制中拥有所有这些脚本,以便能够在我想要的时候在本地运行它们。如果您有兴趣,可以查看我为GitHub上的一个项目编写的脚本。脚本位于 build 文件夹中, publish_local.cmd 是一个入口点。 Project有多个NuGet依赖项,因此,我相信它与您要完成的任务类似。

    我知道在答案中直接提供示例会更好,但此处包含的脚本太多了。如果您有任何疑问,请成为我的客人。