2017-07-04 173 views
2

如何使用dotnet CLI根據Git中的標籤爲NuGet包生成語義版本號(,例如 MyCode.0.1.1.nupkg)?dotnet核心包版本

GitInfo正確地版本化我們的程序集,但我想將其擴展到版本控制我們的NuGet程序包。以下是我們的.csproj代碼片段:

<Project Sdk="Microsoft.NET.Sdk"> 
    <PropertyGroup> 
    <TargetFramework>netcoreapp1.1</TargetFramework> 
    <GenerateAssemblyInfo>False</GenerateAssemblyInfo> 
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild> 
    <PackageVersion>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)</PackageVersion> 
    </PropertyGroup> 
    <ItemGroup> 
    <Compile Include="..\..\GlobalAssemblyInfo.cs" Link="Properties\AssemblyInfo.cs" /> 
    </ItemGroup> 
    <ItemGroup> 
    <PackageReference Include="gitinfo" Version="1.1.66" /> 
    </ItemGroup> 
</Project> 

請注意GeneratePackageOnBuild

我該怎麼做才能生成這些構建變量,$(GitSemVerMajor),。我打電話給dotnet build

回答

1
  • .csproj中刪除<PackageVersion>行。
  • 添加InitialTargets語句來Project
    <Project Sdk="Microsoft.NET.Sdk" InitialTargets="SetVersion">
  • 添加Import的版本控制目標文件:
    <Import Project="..\..\Version.targets" />

Version.targets文件:

<?xml version="1.0" encoding="utf-8"?> 
<Project 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
     <Target Name="GitVersion"> 
     <PropertyGroup> 
      <!-- Dummy values allow dotnet restore to progress --> 
      <GitBaseVersionMajor>0</GitBaseVersionMajor> 
      <GitBaseVersionMinor>1</GitBaseVersionMinor> 
      <GitBaseVersionPatch>0</GitBaseVersionPatch> 
      <GitCommits>0</GitCommits> 
     </PropertyGroup> 
    </Target> 
    <Target Name="SetVersion" DependsOnTargets="GitVersion"> 
     <PropertyGroup> 
      <!-- CI checkouts may be against a specific commit, not a branch, so add -pr as label --> 
      <GitSemVerDashLabel Condition="'$(GitBranch)' == 'undefined'">$(GitSemVerDashLabel)-pr</GitSemVerDashLabel> 
      <!-- If there's no label, but we're not at the tip of the base version, add a -pre because this is not the base build --> 
      <GitSemVerDashLabel Condition="'$(GitSemVerDashLabel)' == '' And '$(GitCommits)' != '0'">-pre</GitSemVerDashLabel> 
      <PackageVersion Condition="'$(GitSemVerDashLabel)' == ''">$(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch)</PackageVersion> 
      <PackageVersion Condition="'$(GitSemVerDashLabel)' != ''">$(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch)$(GitSemVerDashLabel).$(GitCommits)</PackageVersion> 
      <AssemblyVersion>$(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch)</AssemblyVersion> 
     </PropertyGroup> 
    </Target> 
</Project> 
0

我做了一個CLI .NET Core項目版本化工具here我認爲這是你想要的。

你的情況,你會改變你的.csproj文件,以引用CLI工具和省略Version元素:

<Project Sdk="Microsoft.NET.Sdk"> 
    <PropertyGroup> 
    <TargetFramework>netcoreapp1.1</TargetFramework> 
    <GenerateAssemblyInfo>False</GenerateAssemblyInfo> 
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild> 
    </PropertyGroup> 
    <ItemGroup> 
    <Compile Include="..\..\GlobalAssemblyInfo.cs" Link="Properties\AssemblyInfo.cs" /> 
    </ItemGroup> 
    <ItemGroup> 
    <PackageReference Include="gitinfo" Version="1.1.66" /> 
    </ItemGroup> 
    <ItemGroup> 
    <DotNetCliToolReference Include="dotnet-setversion" Version="*" /> 
    </ItemGroup> 
</Project> 

要生成NuGet包使用特定的版本,你只需cd到項目目錄並運行dotnet restore && dotnet setversion <<SOME_VERSION>>

這會Version元素添加到.csproj文件,以便今後任何的dotnet builddotnet package等調用將把該項目作爲具有特定版本。這在CI構建期間特別有用,您可能希望在每次運行時自動增加版本號。

對於完整的披露,我不熟悉GitInfo,所以我很抱歉,如果這不適用於您的情況。如果兩者之間有任何相似之處,我個人將此工具與GitVersion一起使用。