我已經創建了sharedAssemblyInfo.tt的模板,它只在第一次構建或重建時更新了一次版本號。 如果我改變了一些代碼并試圖構建整個專案,它就不會反映版本號了。
uj5u.com熱心網友回復:
如果你想在每次構建專案時增加構建號,而不考慮所選的配置,你可以按照下面的步驟進行,你也可以在應用 ? 組件資訊
下面的代碼將讀取現有的AssemblyInfo.cs檔案,并使用regex查找AssemblyVersion資訊,然后根據TextTransform.exe的輸入來增加修訂和構建號碼。
- 洗掉你現有的應用程式。
- 洗掉你現有的
AssemblyInfo.cs檔案。 - 創建一個
AssemblyInfo.tt檔案來代替它。Visual Studio應該 創建AssemblyInfo.cs,并在你保存T4檔案后將其分組。
代碼:
<#@ template debug="true" hostspecific="true" language="C#" #>/span>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs") 。)
Regex pattern = new Regex("AssemblyVersion("(?<main>d ). (?<minor>d ). (?<revision>d ). (?<build> d )") ) 。
MatchCollection matches = pattern.Matches(output)。
if( matches.Count == 1 )
{
major = Convert.ToInt32(matches[0].Groups["main"].Value)。
minor = Convert.ToInt32(matches[0].Groups["minor"].Value)。)
build = Convert.ToInt32(matches[0].Groups["build"].Value) 1;
revision = Convert.ToInt32(matches[0].Groups[" revision"].Value)。
if( this.Host. ResolveParameterValue("-","-","BuildConfiguration") == "Release" )
revision ;
}
#>/span>
使用System.Reflection。
使用System.Runtime.CompilerServices。
using System.Runtime.InteropServices;
//General Informationassembly: AssemblyTitle("在此插入標題")]
[assembly: AssemblyDescription("在此插入描述")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("此處插入公司")]
[assembly: AssemblyProduct("在此插入產品")]
[assembly: AssemblyCopyright("在此插入著作權")]
[assembly: AssemblyTrademark("在此插入商標")]
[assembly: AssemblyCulture("")]
//版本資訊r()
[assembly: AssemblyVersion("<#= this.main #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]
<# ]。
int major = 1;
int minor = 0;
int revision = 0;
int build = 0;
#>/span>
- 將這個添加到你的預構建事件中:
"%CommonProgramFiles(x86)%microsoft sharedTextTemplating$(VisualStudioVersion)TextTransform. exe" -a !!BuildConfiguration! $(Configuration) "$(ProjectDir)PropertiesAssemblyInfo.tt"
如果這個方案有什么問題,請告訴我,因為我在我的專案中經常使用它。
#編輯:
對于許多專案,你可以嘗試使用VS擴展,這在這種情況下可能會有幫助。我的同事使用了一個擴展。它可以在Visual Studio Marketplace中的發布區和Visual Studio內部的 "擴展和更新 "下找到,名為 "有意的解決方案版本編輯器"。
uj5u.com熱心網友回復:
如果你使用gitlab,你可以使用GitVersionTask(https://www.nuget.org/packages/GitVersionTask)。
在每次提交時,它都會自動增加你的構建的Version,甚至重命名輸出檔案。你只需將nuget安裝到你的專案中就可以了。
分支
test-complex-udt在2提交時的輸出示例:
HtOPC.Local.1.5.2-test-complex-udt0002
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/317266.html
標籤:
