Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get and Replace AssemblyVersion from AssemblyInfo.cs

Tags:

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))
    }
}

EDIT

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

[assembly: AssemblyVersion("1.0.0.0")] 
like image 215
NealR Avatar asked Mar 24 '16 12:03

NealR


People also ask

How do I get AssemblyInfo CS go?

It is located under the folder "Properties". AssemblyInfo. cs file also get created, when you create any Web Application project.

What is the difference between AssemblyVersion and AssemblyFileVersion?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.

How do I add AssemblyInfo project to CS?

cs by right clicking the project and chosing properties. In the application tab fill in the details and press save, this will generate the assemblyInfo. cs file for you. If you build your project after that, it should work.


1 Answers

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

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.

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.


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.

like image 194
Matt Avatar answered Oct 31 '22 13:10

Matt