我最终创建了一个不同的解决方案来完成让构建服务器自动版本化我们的程序集的任务。我没有使用 T4 动态创建 AssemblyInfo.cs 文件以通过项目链接共享到解决方案中的其他项目(因此必须解决如何为此目的保持文件重新生成的烦恼),完全避开了T4。与第 3 方远程构建机器集成、管理允许 T4 操作的插件和 SDK 等工作量很大。有一个更简单的解决方案涉及 MSBuild 社区任务 我发现 这个问题. 。与 T4 相比,该解决方案非常简单!下面是它的震动方式:
每次构建此项目,application.versioning.csproj,都会在第三方,nuget安装的“ msbuildCommunity Tasks”库中定义的MSBUILD任务动态生成 [程序集文件版本], [大会信息版], [组装版] 基于当前UTC日期时间,并将它们输入到新文件AutoverSion.cs中,该文件位于该项目的“属性”目录中(以及AssemblyInfo.cs文件)。汇编Info.cs将这些汇编属性永远被淘汰,以避免构建多重定义的汇编属性的错误)。在生成自动version.cs(发生在构建之前)之后,编译器将上述程序集属性集成到已构建的组件中。此版本控制反映了它们构建时的 UTC 时间(见下文)。
.sln 中的每个其他 .csproj 都订阅此动态的构建时程序集版本控制,创建指向生成的 AutoVersion.cs 文件的文件链接。这些程序集还必须修剪其 AssemblyInfo.cs。每次MyApplication.versioning.csproj都会构建(或重建)时,用于.sln中所有订阅的.csprojs的汇编版本。这是版本控制项目的 .csproj:
<!-- START DYNAMIC ASSEMBLY VERSIONING WORK-->
<!--MSBuild Community Tasks path as installed via the nuget package 'Install-Package MSBuildTasks'-->
<PropertyGroup>
<MSBuildCommunityTasksPath>$(MSBuildThisFileDirectory)..\.build</MSBuildCommunityTasksPath>
<My-PropertiesDir>Properties</My-PropertiesDir>
</PropertyGroup>
<PropertyGroup>
<!--this should only be incremented (starting at zero) for MAJOR application releases this should never be reset only incremented!-->
<ManualMajorVersion>0</ManualMajorVersion>
<!--3 digits maximum should only be manually incremented (starting at zero) for feature releases-->
<!--!this should be reset to '0' at the start of each caldenar Year-->
<ManualMinorVersion>0</ManualMinorVersion>
</PropertyGroup>
<!--Import MSBuild Community Tasks library installed from Nuget -->
<!--This library contains defined MSBUILD tasks useful for dynamically generating Assembly information via MSBUILD https://github.com/loresoft/msbuildtasks-->
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
<Target Name="BeforeBuild">
<Time Format="yy.MM.dd.HHmm" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="My-VersionNumber" />
</Time>
<Message Text="Auto versioning from UTC time: $(My-VersionNumber) ...">
</Message>
<Time Format="yy" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="Year" />
</Time>
<Time Format="MM" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="Month" />
</Time>
<Time Format="dd" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="Day" />
</Time>
<Time Format="HH" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="Hour" />
</Time>
<Time Format="mm" Kind="Utc">
<Output TaskParameter="FormattedTime" PropertyName="Minute" />
</Time>
<ItemGroup>
<My-AssemblyInfo Include="$(My-PropertiesDir)\AutoVersion.cs" />
<Compile Include="@(My-AssemblyInfo)" />
</ItemGroup>
<MakeDir Directories="$(My-PropertiesDir)" />
<PropertyGroup>
<GeneratedAssemblyVersion>$(ManualMajorVersion).$(Year)$(ManualMinorVersion).$(Month)$(Day).$(Hour)$(Minute)</GeneratedAssemblyVersion>
</PropertyGroup>
<AssemblyInfo OutputFile="@(My-AssemblyInfo)" CodeLanguage="CS" AssemblyFileVersion="$(GeneratedAssemblyVersion)" AssemblyInformationalVersion="$(GeneratedAssemblyVersion)" AssemblyVersion="$(GeneratedAssemblyVersion)" Condition="$(GeneratedAssemblyVersion) != '' " />
</Target>
<!-- END DYNAMIC ASSEMBLY VERSIONING WORK-->
以及单元测试来亲自验证:
/// <summary> A test to validate the configured, auto-generated assembly versioning is working as expected </summary>
[Test]
public void AssemblyVersioningTest()
{
DirectoryInfo currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
DirectoryInfo versioningDir = new DirectoryInfo(currentDirectory.FullName + "\\" + VERSIONING_DYNAMIC_FILE_DIRECTORY);
// verify versioning directory located/loaded/exists
Assert.IsTrue(versioningDir.Exists);
// locate the VERSIONING_DYNAMIC_FILE file within the VERSIONING_DYNAMIC_FILE_DIRECTORY directory
string dynamicFilePath = versioningDir.FullName + "\\" + VERSIONING_DYNAMIC_FILE;
// get the FileInfo for the file that is used to dynamically generate assembly versioning
FileInfo dynamicVersioningFileInfo = new FileInfo(dynamicFilePath);
Assert.IsTrue(dynamicVersioningFileInfo.Exists);
// get the two digit creation Dates/Times for the assembly's file as strings
// since that's what the versioning reflects
DateTime dynamicVersioningFileLastWriteTime = dynamicVersioningFileInfo.LastWriteTime;
#region Created VERSIONING_DYNAMIC_FILE
string dynamicVersioningFileLastWriteTimeYear = dynamicVersioningFileLastWriteTime.ToUniversalTime().ToString("yy");
string dynamicVersioningFileLastWriteTimeMonth = dynamicVersioningFileLastWriteTime.ToUniversalTime().ToString("MM");
string dynamicVersioningFileLastWriteTimeDay = dynamicVersioningFileLastWriteTime.ToUniversalTime().ToString("dd");
string dynamicVersioningFileLastWriteTimeHour = dynamicVersioningFileLastWriteTime.ToUniversalTime().ToString("HH");
string dynamicVersioningFileLastWriteTimeMinute = dynamicVersioningFileLastWriteTime.ToUniversalTime().ToString("mm");
#endregion Created VERSIONING_DYNAMIC_FILE
// get *this* assembly from the .sln using reflection since this assembly consumes/is dependent upon the versioning functionality we
// are testing
Assembly theAssemblyExecutingThisTest = Assembly.GetExecutingAssembly();
// get this assembly's version
// we will investigate this to compare it to a reverse-engineering of what we would
// expect it to be based
AssemblyName testingAssemblyName = theAssemblyExecutingThisTest.GetName();
Version testingAssemblyVersion = testingAssemblyName.Version;
#region Generated Assembly Versioning
// get the first two digits of the assemblyVersion.MinorVersion - these represent the year
string testingAssemblyVersionMinorYear = testingAssemblyVersion.Minor.ToString().Substring(0, 2);
// the rest of the digits represent the manually-configured version and can be 1-3 chars long
string testingAssemblyVersionMinorManual = GetMinorManualFromVersionString(testingAssemblyVersion.Minor.ToString());
string testingAssemblyVersionBuildMonth = testingAssemblyVersion.Build.ToString("D4").Substring(0, 2);
string testingAssemblyVersionBuildDay = testingAssemblyVersion.Build.ToString("D4").Substring(2, 2);
string testingAssemblyVersionRevisionHour = testingAssemblyVersion.Revision.ToString("D4").Substring(0, 2);
string testingAssemblyVersionRevisionMinute = testingAssemblyVersion.Revision.ToString("D4").Substring(2, 2);
#endregion Generated Assembly Versioning
// verify the assembly's minor version: last two digits match of assembly file creation year
// (pad minorversion 4 spaces in case released minor version is empty)
Assert.AreEqual(dynamicVersioningFileLastWriteTimeYear,
testingAssemblyVersionMinorYear,
"Assembly's minor version: last two digits do not match assembly file last write time year");
// verify the assembly's minor version is comprised of two digit 'released minor version' + two digit year of assembly file creation date
Assert.AreEqual(dynamicVersioningFileLastWriteTimeYear + testingAssemblyVersionMinorManual,
testingAssemblyVersionMinorYear + testingAssemblyVersionMinorManual,
"Assembly's minor version: not comprised of two digit year of assembly file last write time date + dynamically-sized 'minor manual version' + ");
// verify the Assembly's build version is comprised of two digit month + two digit day of assembly file creation date
Assert.AreEqual(dynamicVersioningFileLastWriteTimeMonth + dynamicVersioningFileLastWriteTimeDay,
testingAssemblyVersionBuildMonth + testingAssemblyVersionBuildDay,
"Assembly's build version: not comprised of two digit month + two digit day of assembly file last write time date");
// verify the Assembly's revision version is comprised two digit hour + two digit minute of assembly file creation date
Assert.AreEqual(dynamicVersioningFileLastWriteTimeHour + dynamicVersioningFileLastWriteTimeMinute,
testingAssemblyVersionRevisionHour + testingAssemblyVersionRevisionMinute,
"Assembly's revision version: comprised two digit hour + two digit minute of assembly file last write time date");
}