.NET Core/标准自动增量版本控制

目录

使用自动生成的.NET Core/Standard AssemblyInfo.cs

但是InternalsVisibleTo呢?

选择退出自动装配信息生成过程并使用我们自己的自动增量方案


使用自动生成的.NET Core/Standard AssemblyInfo.cs

当您创建一个新的.NET Core/.NET Standard项目时,您将获得一组自动生成的属性,这些属性基于项目的这些设置。

实际上,可以在obj文件夹中为您创建一个自动生成的xxxxAssemblyInfo.cs类。

/------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;

using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyTitleAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.

因此,当我们构建项目时,最终将这个版本应用于项目的结果输出。

好的,这说明了AssemblyInfo 资料如何在.NET Core/Standard中工作,但是.NET Core/Standard执行自动递增版本的方式是什么?

读完所有这些内容后,最好的方法似乎是基于坚持使用自动生成的Assembly.info资料,但想出一些方案来帮助在构建时生成程序集版本。

这意味着您要确保您的.csproj文件看起来像这样,可以看出其中一些.NET Core/Standard自动生成的AssemblyInfo 资料可以直接在项目文件中使用。

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <Platforms>x64</Platforms>
</PropertyGroup>

<PropertyGroup>
  <DefineConstants>STD</DefineConstants>
  <PlatformTarget>x64</PlatformTarget>
  <AssemblyName>SomeDemoCode.Std</AssemblyName>
  <RootNamespace>SomeDemoCode.Std</RootNamespace>
  <VersionSuffix>1.0.0.$([System.DateTime]::UtcNow.ToString(mmff))</VersionSuffix>
  <AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion>
  <AssemblyVersion Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
  <Version Condition=" '$(VersionSuffix)' == '' ">0.0.1.0</Version>
  <Version Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</Version>
  <Company>SAS</Company>
  <Authors>SAS</Authors>
  <Copyright>Copyright © SAS 2020</Copyright>
  <Product>Demo 1.0</Product>
</PropertyGroup>

</Project>

有了这个,您将只使用.NET Core/Standard方法就可以获得自动版本化的Assembly版本

但是InternalsVisibleTo呢?

通常,我们仍然希望将.NET Standard项目公开给测试项目。而且,如果.NET Core/Standard项目基于默认的值或实际.csproj文件中的属性自动生成AssemblyInfo,那么我们如何添加InternalsVisibleTo,为.NET Core/Standard项目自动创建的AssemblyInfo似乎没有涵盖这一点,它似乎也不能作为csproj级别的MSBUILD 属性项使用。那么我们该怎么做呢?

幸运的是,这非常简单,我们只需要在一个自定义文件中执行以下操作,即可调用所需的任何文件,如果需要,甚至可以将其称为AssemblyInfo.cs

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("SomeDemoCode.IntegrationTests")]

选择退出自动装配信息生成过程并使用我们自己的自动增量方案

如果您想使用.NET Framework方法进行自动版本控制,通常可以使用

[assembly: AssemblyVersion("1.0.*")]

因此,您可能会想,嗯,所以我可以通过添加自己的AssemblyInfo.cs来重写它,但这将无法正常工作,您在构建时会得到此信息

> Error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute

幸运的是,我们可以选择退出此自动生成过程,在此过程中,我们必须将以下内容添加到我们的.NET Core/.NET标准csproj文件中。

<PropertyGroup>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  <Deterministic>false</Deterministic>
</PropertyGroup>

您需要确定性属性,否则将收到此错误

Wildcards are only allowed if the build is not deterministic, 
which is the default for .Net Core projects. Adding False to csproj fixes the issue.

有了这个,我们可以包括一个自定义AssemblyInfo.cs,它可以使用自动递增的版本号,在这里,我们在指定AssemblyVersion时使用通配符

using System.Reflection;

using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SomeDemoCode.Std")]
[assembly: AssemblyDescription("SomeDemoCode .NET Standard")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("SAS")]
[assembly: AssemblyProduct("SAS 1.0")]
[assembly: AssemblyCopyright("Copyright © SAS 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("CA7543D7-0F0F-4B48-9398-2712098E9324")]
 
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]

 现在,当我们构建时,您将在.NET Core/Standard项目中获得一些不错的自动递增程序集版本号。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值