gpt4 book ai didi

msbuild - 如何从 MSBuild 自动增加程序集或程序集文件版本?

转载 作者:行者123 更新时间:2023-12-05 07:39:02 25 4
gpt4 key购买 nike

约束是:使用 Visual Studio 2017。需要最终从调用 MSBuild 的 powershell 脚本调用。

不确定其相关性,但需要能够构建以下内容:

  • asp.net 461
  • asp.net-core 1.1 和 2.0 程序集

到目前为止未成功的尝试:

示例尝试 "Code Generation in a Build Process"这适用于从 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);
}
}
}
}
}

关于msbuild - 如何从 MSBuild 自动增加程序集或程序集文件版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47421588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com