Hii, there are many others had already post so many question about this..But here the scenario is different.
I need to extract the first three digits ie. $(major).$(Minor).$(Build)
from version number.
how can i do this??..i tried AssemblyInfo
Task..but that task is just for overwriting the version number.not to extract the version number.
I need to extract first three number and assign them to some property.for further use.
well,i can overwrite them using FileUpdate
task.like ::
<FileUpdate
Files="@(AssemblyFile)"
Regex='(\d+)\.(\d+)\.(\d+)\.(\d+)'
ReplacementText='$1.$2.$3.$(Revision)'>
</FileUpdate>
now how can i use their value ie. $1,$2,$3
to assign to properties.???
Thanx.
You can read lines from files, get the string using regex and change it if you need. And if you're using MSBuild 4.0 you can use Property Functions, which give you an access to .NET API. This example should give you first three numbers of the AssemblyVersion.
<Target Name="ReadAssemblyVersion">
<ReadLinesFromFile File="$(VersionFile)">
<Output TaskParameter="Lines"
ItemName="ItemsFromFile"/>
</ReadLinesFromFile>
<PropertyGroup>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+)</Pattern>
<In>@(ItemsFromFile)</In>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
</PropertyGroup>
<Message Text="Output : $(Out.Remove(0, 28))"/>
</Target>
http://blogs.msdn.com/b/visualstudio/archive/2010/04/02/msbuild-property-functions.aspx
Awesome thread, building upon the work of Alex and RobPol, I was able to define extended msbuild properties that is inspired by semver.org (Major, Minor, Patch, PreRelease). I chose to parse the AssemblyInformalVersion since that is the only attribute compatible with SemVer. Here is My example:
<PropertyGroup>
<In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs'))</In>
<Pattern>\[assembly: AssemblyInformationalVersion\("(?<Major>\d+)\.(?<Minor>\d+)\.(?<Patch>[\d]+)(?<PreReleaseInfo>[0-9A-Za-z-.]+)?</Pattern>
<AssemblyVersionMajor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Major"].Value)</AssemblyVersionMajor>
<AssemblyVersionMinor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Minor"].Value)</AssemblyVersionMinor>
<AssemblyVersionPatch>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Patch"].Value)</AssemblyVersionPatch>
<AssemblyVersionPreRelease>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["PreReleaseInfo"].Value)</AssemblyVersionPreRelease>
</PropertyGroup>
You can test the output of this operation by adding the following to your .csproj:
<Target Name="AfterBuild">
<Message Text="$(AssemblyVersionMajor)"></Message>
<Message Text="$(AssemblyVersionMinor)"></Message>
<Message Text="$(AssemblyVersionPatch)"></Message>
<Message Text="$(AssemblyVersionPreRelease)"></Message>
</Target>
Ex: Snippet from my AssemblyInfo.cs:
[assembly: AssemblyInformationalVersion("0.9.1-beta")]
Will output: Major: '0', Minor: '9', Patch: '1', PreRelease: '-beta'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With