简体   繁体   English

在TFS中构建NuGet软件包期间更新AssemblyInfo

[英]Update AssemblyInfo during build of a NuGet package in TFS

We are evaluating TFS On Premise, including the Package Management feature. 我们正在评估TFS内部部署,包括软件包管理功能。

I have several build configurations that successfully create NuGet packages and publish them to the feed within TFS. 我有几个构建配置,可以成功创建NuGet包并将它们发布到TFS的提要中。

Versions of the package are controlled via the AssemblyInfo.cs file within the solution. 软件包的版本是通过解决方案中的AssemblyInfo.cs文件控制的。 For example: [assembly: AssemblyInformationalVersion("1.0.0.0")] results in a NuGet version 1.0.0.0; 例如: [assembly: AssemblyInformationalVersion("1.0.0.0")]生成NuGet版本1.0.0.0; using AssemblyInformationalVersion because you can apply pre-release tags to your version, such as [assembly: AssemblyInformationalVersion("1.0.0.0-dev")] resulting in verison 1.0.0.0-dev 之所以使用AssemblyInformationalVersion是因为您可以将预发布标签应用于您的版本,例如[assembly: AssemblyInformationalVersion("1.0.0.0-dev")]产生版本1.0.0.0-dev

Is there a way of setting up the build configuration to modify the AssemblyInformationalVersion based on the branch being used for the build? 有没有一种方法可以设置构建配置,以根据用于构建的分支来修改AssemblyInformationalVersion

So that we can have [assembly: AssemblyInformationalVersion("1.0.0.0")] in code, but when building off branch "myNewFeature" it result in "1.0.0.0-myNewFeature" and building off master can result in ""1.0.0.0"? Rather than having to make this change to the AssemblyInfo.cs file each time we merge to a different branch. 这样我们就可以在代码中包含[assembly: AssemblyInformationalVersion("1.0.0.0")] ,但是在构建分支“ myNewFeature”时会导致“ 1.0.0.0-myNewFeature”,而构建master会导致“” 1.0.0.0 “?而不是每次我们合并到另一个分支时都必须对AssemblyInfo.cs文件进行此更改。

To update the AssemblyInfo you could use a powershell script or use build number to version the nuget package. 要更新AssemblyInfo,您可以使用Powershell脚本或使用内部版本号对nuget软件包进行版本控制。

For build definitions, $(SourceBranchName) can be used in the build number format. 对于内部版本定义,可以以内部版本号格式使用$(SourceBranchName) The list of available variables is found here-- Predefined variables 可在此处找到可用变量的列表- 预定义变量

So that when you generate Nuget packages at the end of the build, they can be versioned using the version of the branch that is being built. 这样,当您在构建结束时生成Nuget软件包时,可以使用正在构建的分支的版本对它们进行版本控制。 For more details please refer this related question: Use Branch Name in TFS 2015 Automated Build 有关更多详细信息,请参考以下相关问题: 在TFS 2015自动生成中使用分支名称

And for version nuget package you could also take a look at below blog which provides an overview of the automatic package versioning available here: 对于版本nuget软件包,您还可以在下面的博客中查看,该博客概述了此处提供的自动软件包版本控制:

I have managed to cobble together a Powershell script that is ran as the first build step in the build configuration. 我设法将Powershell脚本拼凑在一起,该脚本作为构建配置中的第一个构建步骤运行。

I used some code from these 2 sources GithubGist pietergheysens/Update-AssemblyInfoVersionFiles.ps1 and also this StackOverflow answer Get and Replace AssemblyVersion from AssemblyInfo.cs 我使用了来自这两个来源的一些代码GithubGist pietergheysens / Update-AssemblyInfoVersionFiles.ps1,以及这个StackOverflow答案,从AssemblyInfo.cs获取和替换AssemblyVersion

I personally do not know Powershell syntax and the following script may not be the most suitable. 我个人不知道Powershell语法,以下脚本可能不是最合适的。 Any advice will be appreciated. 任何建议将被认真考虑。

The powershell script is as: Powershell脚本如下:

Param ( [Parameter(Mandatory=$true)] [string]$branchName, [string]$projectDirectory ) 参数([Parameter(Mandatory = $ true)] [string] $ branchName,[string] $ projectDirectory)

$SrcPath = $env:BUILD_SOURCESDIRECTORY
if ($projectDirectory -ne ""){
    $SrcPath = "$SrcPath$projectDirectory"
}      

Write-Verbose "Executing Update-AssemblyInfoVersionFiles in path $SrcPath for product versionon branch $branchName"  -Verbose

if ($branchName.ToLower() -eq "master")
{
    $branchName = ""
}
else
{
    $branchName = "-$branchName"
}

$AllVersionFiles = Get-ChildItem $SrcPath AssemblyInfo.cs -recurse   
$pattern = '\[assembly: AssemblyInformationalVersion\("(.*)"\)\]'
foreach ($file in $AllVersionFiles) 
{ 
    #version replacements
    (Get-Content $file.FullName) | ForEach-Object{
        if($_ -match $pattern){
            # We have found the matching line
            # Edit the version number and put back.
            $currentVersion = [version]$matches[1]
            Write-Verbose " $currentVersion" -Verbose
            $newVersion = "$currentVersion$branchName"
            '[assembly: AssemblyInformationalVersion("{0}")]' -f $newVersion
            Write-Verbose "New AssemblyInformationalVersion is $newVersion"  -Verbose
        } else {
            # Output line as is
            $_
        }
    } | Set-Content $file.FullName -Force
}
Write-Verbose "Transformed Assembly Informational Version is $assemblyInformationalVersion" -Verbose

My build step looks like this: 我的构建步骤如下所示:

在此处输入图片说明

This is successfully changing the assembly version for me, based on the branch I use to build the package. 根据我用来构建程序包的分支,这已成功为我更改了程序集版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM