MSbuild 用于更新 assemblyinfo 文件

发布于 2024-12-24 20:33:28 字数 277 浏览 1 评论 0原文

我正在编写一个批处理文件来自动执行一系列任务。其中一项任务是通过编辑解决方案中各个项目中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-12-31 20:33:28

如果没有其他工具的帮助,使用 dos cmd 批处理文件编辑文件是相当麻烦的。
您需要使用类似 for /f 命令的命令来单步执行这些行,然后处理每一行。例如,查找以“[ assembly: AssemblyVersion”开头的行
并用其他东西替换它。

但是,如果您的 AssemblyInfo.cs 中没有太多内容(请记住,如果需要,您可以将 AssemblyInfo.cs 拆分为多个 cs 文件),我建议您使用几个 echo 语句从头开始创建该文件。

如果您有其他可用工具(例如 sed.exe),则可以轻松完成编辑。

这些天我更喜欢使用一个简单的 powershell 脚本,它可以把它当作早餐,并让你在需要时可以访问 .Net 库。

这是它的模板形式:

(Get-Content AssemblyInfo.template.cs) -replace "{version}","1.2.3.4" > AssemblyInfo.cs

这是使用正则表达式来替换其中的任何版本号的形式:

$x = 'Version("{0}")' -f "1.2.3.4"
$content = Get-Content AssemblyInfo.cs
$content -replace 'Version\(".*"\)',$x > AssemblyInfo.cs

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:

(Get-Content AssemblyInfo.template.cs) -replace "{version}","1.2.3.4" > AssemblyInfo.cs

Here's a form that uses regular expressions to replace whatever version number is there:

$x = 'Version("{0}")' -f "1.2.3.4"
$content = Get-Content AssemblyInfo.cs
$content -replace 'Version\(".*"\)',$x > AssemblyInfo.cs
娇俏 2024-12-31 20:33:28

这是我更新所有 assemblyinfo.cs 文件的 MSBuild 目标代码(在使用此目标之前,您必须初始化 AssemblyInfoFilesToUpdate 项目集合):

  <!-- Update all the assembly info files with generated version info -->
  <Target Name="AfterGet" Condition="'$(ServerBuild)'=='true' And '$(BuildVersion)'!=''">
    <Message Text="Modifying AssemblyInfo files..." />
    <!-- Clear read-only attributes -->
    <Attrib Files="@(AssemblyInfoFilesToUpdate)" Normal="true" />
    <!-- Update AssemblyVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyVersion\(".*"\)\]"
            ReplacementText="AssemblyVersion("$(BuildVersion)")]" />
    <!-- Update AssemblyFileVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyFileVersion\(".*"\)\]"
            ReplacementText="AssemblyFileVersion("$(BuildVersion)")]" />
    <Message Text="AssemblyInfo files updated to version "$(BuildVersion)"" />
  </Target>

我正在使用 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):

  <!-- Update all the assembly info files with generated version info -->
  <Target Name="AfterGet" Condition="'$(ServerBuild)'=='true' And '$(BuildVersion)'!=''">
    <Message Text="Modifying AssemblyInfo files..." />
    <!-- Clear read-only attributes -->
    <Attrib Files="@(AssemblyInfoFilesToUpdate)" Normal="true" />
    <!-- Update AssemblyVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyVersion\(".*"\)\]"
            ReplacementText="AssemblyVersion("$(BuildVersion)")]" />
    <!-- Update AssemblyFileVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyFileVersion\(".*"\)\]"
            ReplacementText="AssemblyFileVersion("$(BuildVersion)")]" />
    <Message Text="AssemblyInfo files updated to version "$(BuildVersion)"" />
  </Target>

I'm using FileUpdate task from MSBuildCommunityTasks.

转身泪倾城 2024-12-31 20:33:28

是否没有用于修改 assemblyInfo.cs 的 MS Build 任务,

我们的publish.proj 中有类似的内容

<Target Name="SolutionInfo">
    <Message Text="Creating Version File:     $(Major).$(Minor).$(Build).$(Revision)"/>


<AssemblyInfo
        CodeLanguage="CS"
        OutputFile="$(BuildInputDir)\SolutionInfo.cs"
        AssemblyTitle="$(Company) $(Product)$(ProductAppendix)"
        AssemblyDescription="$(Company) $(Product)$(ProductAppendix)"
        AssemblyCompany="$(Company)"
        AssemblyProduct="$(Product)"
        AssemblyCopyright="Copyright © $(Company)"    
        ComVisible="false"
        CLSCompliant="false"
        Guid="9E77382C-5FE3-4313-B099-7A9F24A4C328"
        AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
        AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
</Target>

Isn't there an MS Build task for modifying the assemblyInfo.cs

we have something like this in our publish.proj

<Target Name="SolutionInfo">
    <Message Text="Creating Version File:     $(Major).$(Minor).$(Build).$(Revision)"/>


<AssemblyInfo
        CodeLanguage="CS"
        OutputFile="$(BuildInputDir)\SolutionInfo.cs"
        AssemblyTitle="$(Company) $(Product)$(ProductAppendix)"
        AssemblyDescription="$(Company) $(Product)$(ProductAppendix)"
        AssemblyCompany="$(Company)"
        AssemblyProduct="$(Product)"
        AssemblyCopyright="Copyright © $(Company)"    
        ComVisible="false"
        CLSCompliant="false"
        Guid="9E77382C-5FE3-4313-B099-7A9F24A4C328"
        AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
        AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
</Target>
你又不是我 2024-12-31 20:33:28

这是一个相当老的问题,有一个公认的答案,但我也(由于 CI 构建服务器的限制)想通过批处理文件而不是 powershell 或 MS 构建任务来完成此操作。这是我想到的 - 假设解决方案目录是当前目录:

SetVersion.bat

@echo off
setlocal enabledelayedexpansion

if [%1] == [] GOTO :USAGE
set version=%1
set fileversion=%1
set informationalversion=%1

if [%2] NEQ [] SET fileversion=%2
if [%3] NEQ [] SET informationalversion=%3

echo Setting assembly version information

for /F "delims=" %%f in ('dir /s /b Assemblyinfo.cs') do (
    echo ^> %%f

    pushd %%~dpf

    for /F "usebackq delims=" %%g in ("AssemblyInfo.cs") do (
        set ln=%%g
        set skip=0

        if "!ln:AssemblyVersion=!" NEQ "!ln!" set skip=1
        if "!ln:AssemblyFileVersion=!" NEQ "!ln!" set skip=1
        if "!ln:AssemblyInformationalVersion=!" NEQ "!ln!" set skip=1

        if !skip!==0 echo !ln! >> AssemblyInfo.cs.versioned
    )

    echo [assembly: AssemblyVersion^("%version%"^)] >> AssemblyInfo.cs.versioned
    echo [assembly: AssemblyFileVersion^("%fileversion%"^)] >> AssemblyInfo.cs.versioned
    echo [assembly: AssemblyInformationalVersion^("%informationalversion%"^)] >> AssemblyInfo.cs.versioned

    copy /y AssemblyInfo.cs AssemblyInfo.cs.orig
    move /y AssemblyInfo.cs.versioned AssemblyInfo.cs

    popd
)
echo Done^^!

GOTO:EOF

:USAGE
echo Usage:
echo.
echo SetVersion.bat Version [FileVersion] [InformationalVersion]
echo.

解释

基本上,这将递归所有子文件夹并识别 AssemblyInfo.cs 文件,并且然后逐行将文件复制到新文件中。如果找到任何程序集版本条目,它们将被忽略。然后,在每个文件的末尾附加提供的版本号。

对于那些批处理专家,有些人可能会问为什么我更喜欢使用 for /Fdir /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

@echo off
setlocal enabledelayedexpansion

if [%1] == [] GOTO :USAGE
set version=%1
set fileversion=%1
set informationalversion=%1

if [%2] NEQ [] SET fileversion=%2
if [%3] NEQ [] SET informationalversion=%3

echo Setting assembly version information

for /F "delims=" %%f in ('dir /s /b Assemblyinfo.cs') do (
    echo ^> %%f

    pushd %%~dpf

    for /F "usebackq delims=" %%g in ("AssemblyInfo.cs") do (
        set ln=%%g
        set skip=0

        if "!ln:AssemblyVersion=!" NEQ "!ln!" set skip=1
        if "!ln:AssemblyFileVersion=!" NEQ "!ln!" set skip=1
        if "!ln:AssemblyInformationalVersion=!" NEQ "!ln!" set skip=1

        if !skip!==0 echo !ln! >> AssemblyInfo.cs.versioned
    )

    echo [assembly: AssemblyVersion^("%version%"^)] >> AssemblyInfo.cs.versioned
    echo [assembly: AssemblyFileVersion^("%fileversion%"^)] >> AssemblyInfo.cs.versioned
    echo [assembly: AssemblyInformationalVersion^("%informationalversion%"^)] >> AssemblyInfo.cs.versioned

    copy /y AssemblyInfo.cs AssemblyInfo.cs.orig
    move /y AssemblyInfo.cs.versioned AssemblyInfo.cs

    popd
)
echo Done^^!

GOTO:EOF

:USAGE
echo Usage:
echo.
echo SetVersion.bat Version [FileVersion] [InformationalVersion]
echo.

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 with dir /s instead of for /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 the dir command is more consistent and easier to predict.

Note: This batch file will remove all empty lines from the AssemblyInfo.cs files

枯叶蝶 2024-12-31 20:33:28

使用 WriteCodeFragment MSBuild 任务 。有关示例,请参阅此答案

Use the WriteCodeFragment MSBuild Task. Refer to this answer for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文