- 打开
.csproj
并添加<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
到第一个PropertyGroup
- 您可能希望复制文件夹中已经生成的内容,
AssemblyInfo.cs
这样obj
您就不必自己编写所有内容。
- 在属性文件夹中创建
AssemblyInfo.tt
(T4 模板)。
- 粘贴以下内容+您之前自动生成的旧内容
AssemblyInfo.cs
<#@ template debug="true" hostspecific="True" language="C#" #>
<#@ assembly name="System.Core" #>
<# /*There's a bug with VS2022 where you have to be real specific about envDTE.*/ #>
<#@ assembly name="./PublicAssemblies/envdte.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Globalization" #>
<#@ output extension=".cs" #>
<#
var dte = ((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
string buildConfig = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
string solutionDirectory = Path.GetDirectoryName(dte.Solution.FullName);
var (gitRevision, gitBranch, gitCompactRevision) = ("", "", "");
using(var process = new System.Diagnostics.Process() {
StartInfo = new System.Diagnostics.ProcessStartInfo() {
WorkingDirectory = solutionDirectory,
FileName = @"cmd.exe",
Arguments = "/C git rev-parse HEAD & git rev-parse --abbrev-ref HEAD",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
}) {
process.Start();
string[] lines = process.StandardOutput.ReadToEnd().Split();
gitRevision = lines[0].Trim();
gitBranch = lines[1].Trim();
gitCompactRevision = gitRevision.Substring(0, 6);
}
string appPurpose = "Launcher"; // & Updater
string companyShort = "todo";
string companyFull = "todo";
string productNameShort = "todo";
string productName = $"{companyShort} {productNameShort}";
string fileName = $"{companyShort}{productNameShort}";
string exeNAME = $"{fileName}Launch";
string originalFilename = $"{exeNAME}.exe";
string CALLEXE = $"{fileName}.exe";
string BROWSEREXE = $"{fileName}Browser.exe";
string FULLINSTALLER = $"{fileName}Setup.exe";
DateTime dtBuiltDate = DateTime.UtcNow;
string cBuildYear = dtBuiltDate.Year.ToString();
string cBuildDay = dtBuiltDate.ToString("dd");
string cBuildMonth = dtBuiltDate.ToString("MM");
string cBuildTime = dtBuiltDate.ToString("T", DateTimeFormatInfo.InvariantInfo);
string assemblyVersion = $"3.0.{cBuildYear}.{cBuildMonth}{cBuildDay}";
string JOB_NAME = System.Environment.GetEnvironmentVariable("JOB_NAME") ?? "0.0";
string buildVersion = System.Environment.GetEnvironmentVariable("BUILD_NUMBER") ?? "0-dev";
string buildSeries = Regex.Replace(JOB_NAME, @"[^0-9\.]+", "");
string buildNumber = Regex.Replace(buildVersion, @"[^0-9\.]+", "");
string InternalVersion = $"{JOB_NAME}.{buildVersion}";
string fileVersion = Regex.Replace(InternalVersion, @"[^0-9\.]+", "");
#>
using System.Reflection;
[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assembly: System.Resources.NeutralResourcesLanguageAttribute("en")]
[assembly: AssemblyConfigurationAttribute("<#= buildConfig #>")]
[assembly: AssemblyProduct("<#= productName #>")]
[assembly: AssemblyTitle("<#= $"{companyShort}{productNameShort}" #>")]
[assembly: AssemblyCompany("<#= companyFull #>")]
[assembly: AssemblyDescription("<#= $"{companyShort} {productNameShort} .... {appPurpose} - ...... by {companyFull}" #>")]
[assembly: AssemblyCopyright("<#= $"© 1983-{cBuildYear} {companyFull}" #>")]
[assembly: AssemblyTrademark("<#= $"{productName} is a trademark of {companyFull}, Inc." #>")]
[assembly: AssemblyInformationalVersion("<#= InternalVersion #>")]
[assembly: AssemblyVersion("<#= assemblyVersion #>")]
[assembly: AssemblyFileVersion("<#= fileVersion #>")]
[assembly: AssemblyMetadataAttribute("OriginalFilename", "<#= originalFilename #>")]
[assembly: AssemblyMetadataAttribute("NAME", "<#= $"{productName} {appPurpose}" #>")]
[assembly: AssemblyMetadataAttribute("EXENAME", "<#= exeNAME #>")]
[assembly: AssemblyMetadataAttribute("DIRNAME", "<#= productNameShort #>")]
[assembly: AssemblyMetadataAttribute("CALLEXE", "<#= $"{fileName}.exe" #>")]
[assembly: AssemblyMetadataAttribute("BROWSEREXE", "<#= $"{fileName}Browser.exe" #>")]
[assembly: AssemblyMetadataAttribute("FULLINSTALLER", "<#= $"{fileName}Setup.exe" #>")]
[assembly: AssemblyMetadataAttribute("COMPANY", "<#= companyFull #>")]
[assembly: AssemblyMetadataAttribute("License", "<#= $"Contains copyrighted code and applications ..." #>")]
[assembly: AssemblyMetadataAttribute("TermsOfUse", "<#= "https://www.company.com/en-us/terms-of-use/" #>")]
[assembly: AssemblyMetadataAttribute("Website", "<#= "https://www.company.com/en-us" #>")]
[assembly: AssemblyMetadataAttribute("UpdateURL", "https://subdomain.product.net/version_check")]
[assembly: AssemblyMetadataAttribute("BuildYear", "<#= cBuildYear #>")]
[assembly: AssemblyMetadataAttribute("BuildDay", "<#= cBuildDay #>")]
[assembly: AssemblyMetadataAttribute("BuildMonth", "<#= cBuildMonth #>")]
[assembly: AssemblyMetadataAttribute("BuildTime", "<#= cBuildTime #>")]
[assembly: AssemblyMetadataAttribute("DateModified", "<#= $"{dtBuiltDate.ToString("MMM dd, yyyy", DateTimeFormatInfo.InvariantInfo)} at {cBuildTime}" #>")]
[assembly: AssemblyMetadataAttribute("BuildSeries", "<#= buildSeries #>")]
[assembly: AssemblyMetadataAttribute("BuildNumber", "<#= buildNumber #>")]
[assembly: AssemblyMetadataAttribute("BuildDate", "<#= dtBuiltDate.ToString("s") #>")]
[assembly: AssemblyMetadataAttribute("BuildMachine", "<#= Environment.MachineName #>")]
[assembly: AssemblyMetadataAttribute("BuildMachineUser", "<#= Environment.UserName #>")]
[assembly: AssemblyMetadataAttribute("BuildOSVersion", "<#= Environment.OSVersion #>")]
[assembly: AssemblyMetadataAttribute("BuildPlatform", "<#= Environment.OSVersion.Platform #>")]
[assembly: AssemblyMetadataAttribute("BuildClrVersion", "<#= Environment.Version #>")]
[assembly: AssemblyMetadataAttribute("BuildBranch", "<#= gitBranch #>")]
[assembly: AssemblyMetadataAttribute("BuildRevision", "<#= gitCompactRevision #>")]
[assembly: AssemblyMetadataAttribute("CommitHash", "<#= gitRevision #>")]
[assembly: AssemblyMetadataAttribute("RepositoryUrl", "")]
[assembly: AssemblyMetadataAttribute("RepositoryType", "")]
<#+
#>
您现在可以使用 C# 的全部功能来生成您想要的任何内容,例如您当前所在的 git 分支和修订版。一些技巧:
- 变量可以在
<# #>
块内的任何地方声明
- 您希望使用的任何方法都必须在文件末尾的
<#+ #>
块中声明。(+
标志很重要,必须是文件末尾的最后一件事))
- 块之外的所有内容
<# #>
都只是纯文本。
- VS2019 没有语法高亮或智能感知。该
.tt
文件是纯文本。我建议在安装T4 Support
扩展后使用 vscode 编辑它(在 vs2019 中不可用...)