从 AssemblyInfo.cs 获取和替换 AssemblyVersion [英] Get and Replace AssemblyVersion from AssemblyInfo.cs

查看:49
本文介绍了从 AssemblyInfo.cs 获取和替换 AssemblyVersion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式(仅在测试中)用于更新 AssemblyInfo.cs 文件中的 AssemblyVersion.但是,我想知道从 .cs 文件本身中提取和替换此值的最佳方法是什么?

I have a regex I'm using (in test only) to update the AssemblyVersion from the AssemblyInfo.cs file. I'm wondering, however, what the best way to pull and replace this value from the .cs file itself would be?

这是我最好的猜测,这显然不起作用,但总体思路已经到位.希望有一些更优雅的东西.

Here is my best guess which, obviously, isn't working but the general idea is in place. Was hoping for something a little more elegant.

Get-Content $file | Foreach-Object{
    $var = $_
    if($var -contains "AssemblyVersion"){
        $temp = [regex]::match($s, '"([^"]+)"').Groups[1].Value.Substring(0, $prog.LastIndexOf(".")+1) + 1234
        $var = $var.SubString(0, $var.FirstIndexOf('"') + $temp + $var.SubString($var.LastIndexOf('"'), $var.Length-1))
    }
}

编辑

这里的每个请求是我希望在 AssemblyInfo 中更新的行:

Per request here is the line I'm looking to update in the AssemblyInfo:

[assembly: AssemblyVersion("1.0.0.0")] 

推荐答案

并不是真的想改变你的正则表达式,而是想向你展示你可以尝试的流程.

Not really intending to change your regex but wanting to show you the flow of what you could be trying.

$path = "C:\temp\test.txt"
$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'
(Get-Content $path) | ForEach-Object{
    if($_ -match $pattern){
        # We have found the matching line
        # Edit the version number and put back.
        $fileVersion = [version]$matches[1]
        $newVersion = "{0}.{1}.{2}.{3}" -f $fileVersion.Major, $fileVersion.Minor, $fileVersion.Build, ($fileVersion.Revision + 1)
        '[assembly: AssemblyVersion("{0}")]' -f $newVersion
    } else {
        # Output line as is
        $_
    }
} | Set-Content $path

对每一行运行并检查是否存在匹配的行.当找到匹配时,版本存储为 [version] 类型.使用它我们根据需要更新版本.然后输出更新的行.不匹配的行按原样输出.

Run for every line and check to see if the matching line is there. When a match is found the version is stored as a [version] type. Using that we update the version as needed. Then output the updated line. Non-matching lines are just outputted as is.

文件被读入,因为它在括号中,所以在管道开始处理之前关闭句柄.这让我们可以写回同一个文件.循环中的每一轮都会输出一行,然后将其发送到 set-content 以写回文件.

The file is read in and since it is in brackets the handle is closed before the pipeline starts to process. This lets us write back to the same file. Each pass in the loop outputs a line which is then sent to set-content to write back to the file.

请注意 $var -contains "AssemblyVersion" 不会像您预期的那样工作,因为 -contains 是一个数组运算符.-match 会更可取,只要您知道它是一个支持正则表达式的运算符,因此请注意元字符.-like "*AssemblyVersion*" 也可以工作并支持简单的通配符.

Note that $var -contains "AssemblyVersion" would not have worked as you expected as -contains is an array operator. -match would be preferable as long as you know that it is a regex supporting operator so be careful for meta-characters. -like "*AssemblyVersion*" would also work and supports simple wildcards.

这篇关于从 AssemblyInfo.cs 获取和替换 AssemblyVersion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆