MSbuild 用于更新 assemblyinfo 文件
我正在编写一个批处理文件来自动执行一系列任务。其中一项任务是通过编辑解决方案中各个项目中的 assemblyinfo.cs 文件来更新解决方案中的 dll 版本;然后最后调用 msbuild.exe 来编译解决方案。
在这方面,是否可以编写一个命令行脚本来更新我的.net解决方案中的各种 assemblyinfo.cs 文件。我更愿意从命令行本身调用 msbuild,而不是创建另一个 msbuild 脚本文件。
如何使用 MSBuild 做到这一点?还有其他方法吗?
感谢您抽出时间...
I am writing a batch file to automate a series of tasks. One of these tasks is to update the version of the dlls in my solution, by editing the assemblyinfo.cs file in the various projects in my solution; and then finally calling msbuild.exe to compile the solution.
In this regard, is it possible to write a command line script to update the various assemblyinfo.cs files in my .net solution. I would prefer to call msbuild from the commandline itself and not create another msbuild scripted file.
How does one do this using MSBuild? Is there any other way to do it?
Thanks for your time...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果没有其他工具的帮助,使用 dos cmd 批处理文件编辑文件是相当麻烦的。
您需要使用类似 for /f 命令的命令来单步执行这些行,然后处理每一行。例如,查找以“[ assembly: AssemblyVersion”开头的行
并用其他东西替换它。
但是,如果您的 AssemblyInfo.cs 中没有太多内容(请记住,如果需要,您可以将 AssemblyInfo.cs 拆分为多个 cs 文件),我建议您使用几个 echo 语句从头开始创建该文件。
如果您有其他可用工具(例如 sed.exe),则可以轻松完成编辑。
这些天我更喜欢使用一个简单的 powershell 脚本,它可以把它当作早餐,并让你在需要时可以访问 .Net 库。
这是它的模板形式:
这是使用正则表达式来替换其中的任何版本号的形式:
Editing files with dos cmd batch files is pretty hairy without the help of other tools.
You would need to use something like the for /f command to step through the lines, and then process each line. For example look for the line that starts: "[assembly: AssemblyVersion"
and replace it with something else.
However, if you don't have much in your AssemblyInfo.cs (and remember you can split the AssemblyInfo.cs into multiple cs files if you want) I'd suggest creating the file from scratch with a couple of echo statements.
If you have other tools available like sed.exe the edits can be done easily.
My preference these days would be to go for a trivial powershell script, that could eat this for breakfast and gives you access to the .Net libraries if you need it.
Here's a templating form of it:
Here's a form that uses regular expressions to replace whatever version number is there:
这是我更新所有 assemblyinfo.cs 文件的 MSBuild 目标代码(在使用此目标之前,您必须初始化 AssemblyInfoFilesToUpdate 项目集合):
我正在使用 FileUpdate 任务来自MSBuildCommunityTasks。
This is the MSBuild target code where I update all my assemblyinfo.cs files (You have to init AssemblyInfoFilesToUpdate items collections before using this target):
I'm using FileUpdate task from MSBuildCommunityTasks.
是否没有用于修改 assemblyInfo.cs 的 MS Build 任务,
我们的publish.proj 中有类似的内容
Isn't there an MS Build task for modifying the assemblyInfo.cs
we have something like this in our publish.proj
这是一个相当老的问题,有一个公认的答案,但我也(由于 CI 构建服务器的限制)想通过批处理文件而不是 powershell 或 MS 构建任务来完成此操作。这是我想到的 - 假设解决方案目录是当前目录:
SetVersion.bat
解释
基本上,这将递归所有子文件夹并识别 AssemblyInfo.cs 文件,并且然后逐行将文件复制到新文件中。如果找到任何程序集版本条目,它们将被忽略。然后,在每个文件的末尾附加提供的版本号。
对于那些批处理专家,有些人可能会问为什么我更喜欢使用
for /F
和dir /s
而不是for /R
来迭代文件。基本上,这是因为我发现递归 for 有点笨拙,因为当没有提供通配符时它将对所有子目录(不仅仅是匹配文件)执行。使用 dir 命令更加一致且更容易预测。注意:此批处理文件将从 AssemblyInfo.cs 文件中删除所有空行
A fairly old question with an accepted answer, but I also (due to limitations on the CI build server) wanted to do this via a batch file instead of powershell or MS Build Tasks. Here is what I came up with - assumes the solution directory is the current directory:
SetVersion.bat
Explanation
Basically this will recurse all the subfolders and identify AssemblyInfo.cs files, and then line-by-line copy the files to a new file. If any of the assembly version entries are found they are omitted. Then, at the end of each file, it appends the supplied version numbers.
For those batch gurus out there, some might ask why I prefer using
for /F
withdir /s
instead offor /R
to iterate over the files. Basically, its because I find the recursive for to be a bit clunky since it will execute for all sub-directories (not just matching files) when no wildcards are supplied. Using thedir
command is more consistent and easier to predict.Note: This batch file will remove all empty lines from the AssemblyInfo.cs files
使用
WriteCodeFragment
MSBuild 任务 。有关示例,请参阅此答案。Use the
WriteCodeFragment
MSBuild Task. Refer to this answer for an example.