限制条件是:
使用 Visual Studio 2017。
最终需要从调用 MSBuild 的 powershell 脚本中调用。
不确定其相关性,但需要能够构建以下内容:
- ASP.NET 461
- asp.net-core 1.1 和 2.0 程序集
迄今为止不成功的尝试:
- 如何拥有自动递增的版本号(Visual Studio)?
-
构建过程中的代码生成- 描述为 Microsoft 关于在 MSBuild 中使用“TextTemplate”的最新文档。各州需要复制某些 DLL 来构建服务器。文件未位于文档中指定的位置,并且不知道在哪里复制它们,我已经找到了所有文件。额外修改
.csproj's
import Microsoft.TextTemplating.targets
正确位置的路径,但在运行 MSBuild 时我得到了。 “错误 MSB4018:“TransformTemplates”任务意外失败。”
- 这个 SO 答案 - MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
尝试示例 of “构建过程中的代码生成”这适用于从 VS 构建,但不适用于 MSBuild:
放置在项目的根目录-handleVersioning.tt:
<#@ template language="C#" #>
using System.Reflection;
[assembly: AssemblyVersion("1.0.0.*")]
[assembly: AssemblyFileVersion("<#= this.Year #>.<#= this.Month #>.<#= this.Day #>.<#= this.Minute #>")]
<#+
int Year = DateTime.UtcNow.Year;
int Month = DateTime.UtcNow.Month;
int Day = DateTime.UtcNow.Day;
int Minute = unchecked((int)DateTime.UtcNow.TimeOfDay.TotalMinutes);
#>
.csproj:
<Import Project="...hardcoded...\Microsoft.CSharp.targets" />
<!-- This is the important line: -->
<Import Project="...hardcoded...\TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
像这样调用:msbuild myProject.csproj /t:TransformAll
您可以编写简单的控制台应用程序,然后在“项目属性”>“构建事件”中添加“预构建事件命令行”,如下所示:“D:\SomePath\MyAssemblyInfoPatcher.exe”“$(ProjectDir)Properties\AssemblyInfo.cs”
示例应用程序代码(适用于 VS 2022)
using System;
using System.IO;
using System.Linq;
namespace MyAssemblyInfoPatcher
{
internal class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
string path = args[0].ToString();
Console.WriteLine(string.Format("Current App version is set to: {0}", path));
string now_date = DateTime.Now.ToString("yyyy.MM.dd.HHmm");
if (File.Exists(path))
{
string _AssemblyVersion = string.Empty;
string _AssemblyFileVersion = string.Empty;
var lines = File.ReadLines(string.Format(path));
for (int i = 0; i < lines.Count(); i++)
{
if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyVersion"))
{
_AssemblyVersion = lines.ElementAt(i).ToString();
}
else if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyFileVersion"))
{
_AssemblyFileVersion = lines.ElementAt(i).ToString();
}
}
string _replace_assembly = File.ReadAllText(path);
if (_AssemblyVersion != string.Empty)
{
_replace_assembly = _replace_assembly.Replace(_AssemblyVersion, string.Format("[assembly: AssemblyVersion(\"{0}\")]", now_date));
}
if (_AssemblyFileVersion != string.Empty)
{
_replace_assembly = _replace_assembly.Replace(_AssemblyFileVersion, string.Format("[assembly: AssemblyFileVersion(\"{0}\")]", now_date));
}
File.WriteAllText(path, _replace_assembly);
}
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)