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

  • Post author:
  • Post category:其他



目录


使用自动生成的.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


项目中获得一些不错的自动递增程序集版本号。