MSbuild用于更新assemblyinfo文件

use*_*358 12 .net msbuild build-automation automation

我正在编写一个批处理文件来自动执行一系列任务.其中一项任务是通过编辑解决方案中各个项目中的assemblyinfo.cs文件来更新我的解决方案中的dll版本; 然后最后调用msbuild.exe来编译解决方案.

在这方面,是否可以编写命令行脚本来更新我的.net解决方案中的各种assemblyinfo.cs文件.我更喜欢从命令行本身调用msbuild而不是创建另一个msbuild脚本文件.

如何使用MSBuild做到这一点?还有其他办法吗?

谢谢你的时间...

Ste*_*enD 10

使用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
Run Code Online (Sandbox Code Playgroud)

这是一个使用正则表达式替换那里的版本号的表单:

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


Lud*_*dwo 9

这是MSBuild目标代码,我更新了所有的assemblyinfo.cs文件(在使用此目标之前,您必须初始化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\(&quot;.*&quot;\)\]"
            ReplacementText="AssemblyVersion(&quot;$(BuildVersion)&quot;)]" />
    <!-- Update AssemblyFileVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyFileVersion\(&quot;.*&quot;\)\]"
            ReplacementText="AssemblyFileVersion(&quot;$(BuildVersion)&quot;)]" />
    <Message Text="AssemblyInfo files updated to version &quot;$(BuildVersion)&quot;" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

我正在使用MSBuildCommunityTasks中的FileUpdate任务.


Cil*_*vic 7

是否有用于修改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>
Run Code Online (Sandbox Code Playgroud)