Reputation: 301
I got a text file with a version number which updates on each build i.e: 1.4.10.301.
I would like to insert this number on each build to my globalassemblyinfo.cs file on this line: [assembly: AssemblyVersion("1.4.10.300")] and only to the version number, meaning before the build the globalassembly.cs file would be updated with the new version number from the text file (1.4.10.301).
Tried this:
$a = cat "C:\pathtofile\GlobalAssemblyInfo.cs" | select-string AssemblyVersion
which gives me only this line:
[assembly: AssemblyVersion("1.4.10.331")]
but from it I need only the number how can I pull it out? Tried using a pattern but I didn't know what to put in the pattern exactly
The code below worked like a charm and I had managed to extract the version number, now i'm trying to replace this version with the one in globalassemblyinfo.cs, so I wrote this line:
$a -replace $version,$Newversion | Set-Content "C:\pathtofile\GlobalAssemblyInfo.cs"
But it deleted the whole file content and put this string instead:
[assembly: AssemblyVersion("1.4.10.400")]
How can I just update the line without deleting the whole file content?
Upvotes: 0
Views: 484
Reputation: 301
Found the answer, apparently you have to use Get-Content command with parenthesis like this :
(Get-Content "C:\pathtofile\GlobalAssemblyInfo.cs") -replace $version,$Newversion | Set-Content "C:\pathtofile\GlobalAssemblyInfo.cs"
This link helped: https://stackoverflow.com/a/17145810/4333722
Upvotes: 0
Reputation: 174545
Once you've got the line containing the AssemblyVersion
attribute, you can grab the version field with a named capture group, like this:
if($a -match '(?<version>(\d+\.?){4})'){
$version = $Matches["version"]
}
The actual pattern: (\d+\.?){4}
matches four consecutive occurrences of one or more digits and an optional dot - the (?<version>)
part makes sure it gets captured into $Matches["version"]
Upvotes: 1