-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_update_tool.ps1
More file actions
219 lines (187 loc) · 6.58 KB
/
version_update_tool.ps1
File metadata and controls
219 lines (187 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#
# Author: Corelli
# Date: 15AUG2022
# Version: 0.1
<#
.SYNOPSIS
Updates the version of the CMakeLists in the project repo
.DESCRIPTION
Tool for updating repo CMakeLists version numbers in a standardized/semi-automated way
.PARAMETER Help
Show the help documentation
.PARAMETER AutoConfirm
Auto-accept the changes when specifying the version number as a parameter
.PARAMETER OpenURL
Open the GitLab merge request after completion
.PARAMETER TestRun
Perform a test-run of the update. Doesn't commit/push the new branch
.PARAMETER PATCH
Specifies the new PATCH version number to use.
.PARAMETER Version
Specifies the new version number to use. Must be in the CMake Version form of d.d.d.d (example: 1.23.456.7890)
.INPUTS
PATCH value can be piped in
.OUTPUTS
None
.EXAMPLE
PS> version_update_tool.ps1 # run without params
.EXAMPLE
PS> version_update_tool.ps1 12 # run and specify new PATCH value as 12
.EXAMPLE
PS> version_update_tool.ps1 -t # test run
.EXAMPLE
PS> version_update_tool.ps1 -o # open merge request url after creation
.EXAMPLE
PS> version_update_tool.ps1 -v 1.2.3.4 # specify full version number
.EXAMPLE
PS> version_update_tool.ps1 -v 1.2.3.4 -y # auto-confirm diff changes
.EXAMPLE
PS> version_update_tool.ps1 -v 1.2.3.4 -y -o # combination of above
#>
[CmdletBinding(DefaultParametersetName="default")]
param(
[switch][Alias("h")] $Help, # include to display the help message and then exit
[switch][Alias("Auto", "a", "y")] $AutoConfirm, # include to skip confirmation prompts (requries -version)
[switch][Alias("Open", "URL", "o", "u")] $OpenURL, # include to open the merge request url after creation
[switch][Alias("Test", "t")] $TestRun, # test-run, don't create merge
[Parameter(Position=0, ValueFromPipeline = $true, ParameterSetName="patch")]
[string]
[ValidateScript({
# validate patch value:
if( $_ -match '^\d+$' ){ $true }
else { throw 'Please provide a Valid PATCH Number (example: 12)' }
})][Alias("p")] $PATCH, # include to update just the PATCH value
[Parameter(ParameterSetName="version")]
[string]
[ValidateScript({
# validate version w/ start/end anchors
if( $_ -match '^(\d+)\.(\d+)\.(\d+)\.(\d+)$'){ $true }
else { throw 'Please provide a valid Version Number (example: 1.2.3.4)' }
})][Alias("v", "ver")] $version # include to skip the version select dialog and use a specific version
)
if($Help){
# call the Get-Help
Get-Help $MyInvocation.MyCommand.Path
exit 0
}
if($TestRun){
$AutoConfirm = $false # turn off AutoConfirm if test-run
}
# match cmake style version numbers w/o start/end
$versionRegexAnchors = '^(\d+)\.(\d+)\.(\d+)\.(\d+)$'
$versionRegex = '(\d+)\.(\d+)\.(\d+)\.(\d+)'
# Get current branch name
$currBranch = (git branch --show-current)
Write-Debug "$currBranch"
# Check if on develop
if($currBranch -ne "develop"){
Write-Error "You must be on the develop branch to use this tool"
exit 1
}
# Parse current git repo version string from cmakelists
$cmroot = (git rev-parse --show-toplevel)
$repoName = ("$cmroot" | Select-String -Pattern '(\/[^\/]*)?\/([^\/]*)$' -AllMatches).Matches.Groups[0].value
Write-Debug "$repoName"
$cmlist = "/src/CMakeLists.txt"
$cmfile = ($cmroot+$cmlist)
Write-Debug "$cmFile"
$versionRegex = '(\d+)\.(\d+)\.(\d+)\.(\d+)'
$matches = Select-String -Path $cmfile $versionRegex -AllMatches
if(!$matches){
Write-Error "Could not auto-detect CMAKE_PROJECT_VERSION. Please update the CMakeLists.txt to have a version in the form of MAJOR.MINOR.PATCH.TWEAK"
exit 1
}
$currVersion = $matches.Matches.Groups[0].value
Write-Debug "Version: $currVersion"
$currMAJOR = $matches.Matches.Groups[1].value
$currMINOR = $matches.Matches.Groups[2].value
$currPATCH = $matches.Matches.Groups[3].value
$currTWEAK = $matches.Matches.Groups[4].value
Write-Debug "MAJOR: $currMAJOR"
Write-Debug "MINOR: $currMINOR"
Write-Debug "PATCH: $currPATCH"
Write-Debug "TWEAK: $currTWEAK"
if($PATCH){
$version = "$currMAJOR.$currMINOR.$PATCH.$currTWEAK"
}
if(!$version){
Write-Debug "No version specified"
if($AutoConfirm){ Write-Debug "Disabling auto-confirm" }
$AutoConfirm = $false
Write-Host "Current Version is: " -NoNewLine
Write-Host "$currVersion" -ForegroundColor "yellow"
do {
$newVersion = Read-Host "Enter new Version number (d.d.d.d)"
} while( $newVersion -notmatch $versionRegexAnchors ) # regex match user input
$version = $newVersion
}
# check if update branch already exists
$target = "update-version-$version"
if( (git rev-parse --verify refs/heads/$target).Length -ne 0){
Write-Error "Target branch already exists"
Write-Debug "Resetting to develop"
git reset --hard
Exit 1
}
# reset; fetch; pull updates to develop
git reset --hard; git fetch; git pull
######
# regex replace the version in the cmake
(Get-Content $cmfile) -replace $versionRegex, $version | Out-File $cmfile -Encoding ascii
$changes = (git diff)
Write-Host "Diff: "
for($idx = 0; $idx -lt $changes.Length; $idx++){
$char = $changes[$idx][0]
# color diff
[console]::ResetColor()
if($char -eq '+'){
[console]::ForegroundColor = "green"
}
if( $char -eq '-'){
[console]::ForegroundColor = "red"
}
Write-Host $changes[$idx]
}
[console]::ResetColor()
if(!$AutoConfirm -and !$TestRun){
# prompt user to confirm diff
$title = 'Review Diff'
$question = 'Proceed?'
$choices = '&Yes', '&No'
$result = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if($result -ne 0){
Write-Host "User canceled update tool. Exiting" -ForegroundColor "yellow"
Write-Debug "Resetting to develop"
git reset --hard
exit 0
}
}
if(!$TestRun){
git switch -c $target
git commit -am "Update Version to $version"
$remoteOutput = $( $output = git push --porcelain `
-o merge_request.create `
-o merge_request.remove_source_branch `
-o merge_request.title="Non-JIRA Issue: Update Version to $version" `
-o merge_request.description="Update version to $version" `
-o merge_request.draft `
-o merge_request.label="Automated" `
-o merge_request.label="Version Update" `
-o merge_request.assign="{{tech_lead or your username}}" `
-u origin HEAD) 2>&1
$remoteOutput | Write-Host -ForegroundColor "yellow"
$output | Write-Host
if($OpenURL){
$url = "$remoteOutput" -match "(https:\/\/[^\s]*)"
if($url){
Start-Process $matches[0]
}
}
} else {
Write-Host "test run finished. resetting to develop" -ForegroundColor "yellow"
git reset --hard
}
[console]::ForegroundColor = "green"
Write-Host "Finished. Exiting"
[console]::ResetColor()
exit 0