1+ param (
2+ [Parameter (Mandatory = $false )]
3+ [string ]$BuildSourceBranch ,
4+
5+ [Parameter (Mandatory = $false )]
6+ [string ]$BuildReason
7+ )
8+
9+ # Function to parse version from Directory.Build.props
10+ function Get-CurrentVersion {
11+ $propsFile = " Directory.Build.props"
12+ if (-not (Test-Path $propsFile )) {
13+ throw " Directory.Build.props not found"
14+ }
15+
16+ [xml ]$xml = Get-Content $propsFile
17+ $versionPrefix = $xml.Project.PropertyGroup.VersionPrefix
18+
19+ if (-not $versionPrefix ) {
20+ throw " VersionPrefix not found in Directory.Build.props"
21+ }
22+
23+ return $versionPrefix
24+ }
25+
26+ # Function to update version in Directory.Build.props
27+ function Update-VersionInProps {
28+ param ([string ]$NewVersion )
29+
30+ $propsFile = " Directory.Build.props"
31+ [xml ]$xml = Get-Content $propsFile
32+
33+ $xml.Project.PropertyGroup.VersionPrefix = $NewVersion
34+ $xml.Save ((Resolve-Path $propsFile ))
35+
36+ Write-Host " Updated version to $NewVersion in Directory.Build.props"
37+ }
38+
39+ # Function to increment patch version
40+ function Get-IncrementedVersion {
41+ param ([string ]$CurrentVersion )
42+
43+ if ($CurrentVersion -match ' ^(\d+)\.(\d+)\.(\d+)$' ) {
44+ $major = [int ]$matches [1 ]
45+ $minor = [int ]$matches [2 ]
46+ $patch = [int ]$matches [3 ] + 1
47+
48+ return " $major .$minor .$patch "
49+ }
50+ else {
51+ throw " Invalid version format: $CurrentVersion "
52+ }
53+ }
54+
55+
56+ # Main execution
57+ try {
58+ Write-Host " Starting version management script..."
59+ Write-Host " Build Source Branch: $BuildSourceBranch "
60+ Write-Host " Build Reason: $BuildReason "
61+
62+ # Get current version
63+ $currentVersion = Get-CurrentVersion
64+ Write-Host " Current version: $currentVersion "
65+
66+ # Check if this should trigger a version increment
67+ # In Azure DevOps, we want to increment when:
68+ # 1. We're building the master branch
69+ # 2. The source branch was develop (indicating a merge from develop to master)
70+ $shouldIncrement = $false
71+
72+ if ($BuildSourceBranch -eq " refs/heads/master" -or $BuildSourceBranch -eq " refs/heads/main" ) {
73+ # Check if this is a merge from develop by looking at the commit message
74+ $lastCommitMessage = git log -1 -- pretty= format:" %s"
75+ Write-Host " Checking commit message: $lastCommitMessage "
76+
77+ if ($lastCommitMessage -match " Merge.*develop.*master|Merge.*develop.*main|Merge pull request.*develop" ) {
78+ Write-Host " Detected develop to master merge from commit message"
79+ $shouldIncrement = $true
80+ }
81+ else {
82+ # Alternative check using git merge-base to see if develop was recently merged
83+ $developCommit = git rev- parse origin/ develop 2> $null
84+ $masterCommit = git rev- parse HEAD
85+
86+ if ($developCommit ) {
87+ $mergeBase = git merge-base $developCommit $masterCommit 2> $null
88+ if ($mergeBase -eq $developCommit ) {
89+ Write-Host " Detected that develop branch was merged (develop is ancestor of current commit)"
90+ $shouldIncrement = $true
91+ }
92+ }
93+ }
94+ }
95+
96+ if ($shouldIncrement ) {
97+ Write-Host " Detected develop to master merge - incrementing version"
98+
99+ # Increment version
100+ $newVersion = Get-IncrementedVersion - CurrentVersion $currentVersion
101+ Write-Host " New version: $newVersion "
102+
103+ # Update Directory.Build.props
104+ Update-VersionInProps - NewVersion $newVersion
105+
106+ # Create git tag
107+ $tagName = " v$newVersion "
108+ Write-Host " Creating tag: $tagName "
109+
110+ git config user.email
" [email protected] " 111+ git config user.name " Azure DevOps Build"
112+
113+ git add Directory.Build.props
114+ git commit - m " Bump version to $newVersion "
115+ git tag - a $tagName - m " Release version $newVersion "
116+
117+ # Push tag to origin
118+ git push origin $tagName
119+ Write-Host " Pushed tag $tagName to origin"
120+
121+ # Switch to develop and merge the version update
122+ Write-Host " Switching to develop branch to merge version update"
123+ git checkout develop
124+ git merge master -- no- ff - m " Merge version update from master"
125+ git push origin develop
126+ Write-Host " Merged version update to develop branch"
127+
128+ # Switch back to master
129+ git checkout master
130+
131+ # Set output variables for Azure DevOps
132+ Write-Host " ##vso[task.setvariable variable=NewVersion;isOutput=true]$newVersion "
133+ Write-Host " ##vso[task.setvariable variable=VersionIncremented;isOutput=true]true"
134+ }
135+ else {
136+ Write-Host " No version increment needed"
137+ Write-Host " ##vso[task.setvariable variable=NewVersion;isOutput=true]$currentVersion "
138+ Write-Host " ##vso[task.setvariable variable=VersionIncremented;isOutput=true]false"
139+ }
140+
141+ # Always output current version for build purposes
142+ Write-Host " ##vso[task.setvariable variable=BuildVersion;isOutput=true]$currentVersion "
143+
144+ Write-Host " Version management script completed successfully"
145+ }
146+ catch {
147+ Write-Error " Error in version management script: $_ "
148+ exit 1
149+ }
0 commit comments