MSbuild for updating the assemblyinfo file

前端 未结 5 2061
礼貌的吻别
礼貌的吻别 2021-02-13 19:24

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 vario

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 20:21

    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
    

提交回复
热议问题