Last active
March 28, 2023 15:39
-
-
Save theTonyHo/e346d59a886433cccd990800f1cefceb to your computer and use it in GitHub Desktop.
MSBuild AutoVersion using Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- reference: https://stackoverflow.com/questions/2982559/extract-number-from-string-in-msbuild --> | |
<!-- source: https://gist.github.com/theTonyHo/e346d59a886433cccd990800f1cefceb --> | |
<Target Name="NugetPackAutoVersioning" AfterTargets="Build"> | |
<PropertyGroup Condition=" '$(AssemblyVersion)'=='' "> | |
<AssemblyInfo>$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs</AssemblyInfo> | |
<AssemblyInfoContent>$([System.IO.File]::ReadAllText('$(AssemblyInfo)'))</AssemblyInfoContent> | |
<Pattern>\[assembly: AssemblyVersion\("(\d+.\d+.\d+.\d+)</Pattern> | |
<AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match($(AssemblyInfoContent), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value) (From $(AssemblyInfo))</AssemblyVersion> | |
</PropertyGroup> | |
<PropertyGroup> | |
<In>$(AssemblyVersion)</In> | |
<Major>$([System.Text.RegularExpressions.Regex]::Match($(In), `(\d+).(\d+).(\d+).(\d+)`).Groups[1].Value)</Major> | |
<Minor>$([System.Text.RegularExpressions.Regex]::Match($(In), `(\d+).(\d+).(\d+).(\d+)`).Groups[2].Value)</Minor> | |
<Patch>$([System.Text.RegularExpressions.Regex]::Match($(In), `(\d+).(\d+).(\d+).(\d+)`).Groups[3].Value)</Patch> | |
<BuildCounter Condition="'$(BuildCounter)'==''">0</BuildCounter> | |
<BuildNumber Condition="'$(BuildNumber)'==''">$([System.DateTime]::Now.ToString("yyMMdd"))</BuildNumber> | |
<PackageVersion>$(Major).$(Minor).$(BuildNumber).$(BuildCounter)</PackageVersion> | |
<BuildSuffix Condition="'$(BuildSuffix)'==''">wip</BuildSuffix> | |
<BuildSuffix>$([MSBuild]::Escape($(BuildSuffix.Replace('/', '-').Replace('\', '-').Replace('=', '-').Replace('_', '-'))))</BuildSuffix> | |
</PropertyGroup> | |
<Message Text="Found AssemblyVersion: $(AssemblyVersion)" Importance="High" /> | |
<Message Text="Auto PackageVersion: $(PackageVersion)" Importance="High" /> | |
<Exec Command="nuget pack -Version $(PackageVersion) -Suffix $(BuildSuffix) -Properties Configuration=$(Configuration) " /> | |
</Target> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment