-
Notifications
You must be signed in to change notification settings - Fork 0
/
BumpVersion.ps1
executable file
·121 lines (98 loc) · 3.53 KB
/
BumpVersion.ps1
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
#!/usr/bin/env pwsh
param (
[string]$ProjectPath = ".\Tool\BuildAutomationTool\BuildAutomationTool", # Default to current directory
[switch]$UploadPackage = $false, # Optional parameter to upload package
[ValidateSet("major", "minor", "patch")]
[string]$VersionType = "patch"
)
# Convert the path specified to an absolute path
function Convert-ToAbsolutePath {
param (
[string]$PathToCheck
)
# Check if the path is already absolute
if ([System.IO.Path]::IsPathRooted($PathToCheck)) {
# If it's already absolute, return it as is
return $PathToCheck
} else {
# If it's relative, resolve it to an absolute path
$absolutePath = Resolve-Path $PathToCheck
return $absolutePath
}
}
# Function to read the current version from the .csproj file
function Get-CurrentVersion {
param (
[string]$csprojPath
)
$csprojContent = Get-Content $csprojPath
$versionLine = $csprojContent | Select-String -Pattern '<Version>'
if ($versionLine) {
$currentVersion = [regex]::Match($versionLine, '\d+\.\d+\.\d+').Value
return $currentVersion
}
else {
Write-Host "Version not found in the .csproj file." -ForegroundColor Red
exit 1
}
}
# Function to bump the patch version (semantic versioning: Major.Minor.Patch)
function Bump-PatchVersion {
param (
[string]$CurrentVersion,
[ValidateSet("major", "minor", "patch")]
[string]$VersionType = "patch"
)
$versionParts = $CurrentVersion -split '\.'
$major = [int]$versionParts[0]
$minor = [int]$versionParts[1]
$patch = [int]$versionParts[2]
Write-Host "Updating version type ""$VersionType""." -ForegroundColor Yellow
switch ($VersionType) {
"major" {
$major = [int]$versionParts[0] + 1
}
"minor" {
$minor = [int]$versionParts[1] + 1
}
"patch" {
$patch = [int]$versionParts[2] + 1
}
default {
# Code to execute if no case matches
}
}
return "$major.$minor.$patch"
}
# Function to update the version in the .csproj file
function Update-VersionInCsproj {
param (
[string]$csprojPath,
[string]$newVersion
)
$csprojContent = Get-Content $csprojPath
$updatedContent = $csprojContent -replace '<Version>\d+\.\d+\.\d+</Version>', "<Version>$newVersion</Version>"
# Write the updated content back to the .csproj file
Set-Content $csprojPath $updatedContent
Write-Host "Updated version in $csprojPath to $newVersion" -ForegroundColor Green
}
# Function to commit version changes to git
function Commit-VersionChange {
param (
[string]$newVersion
)
git add .
git commit -m "Bump version to $newVersion"
git push
Write-Host "Committed version change to git: $newVersion" -ForegroundColor Green
}
$ProjectPath = Convert-ToAbsolutePath -PathToCheck $ProjectPath
$csprojPath = "$ProjectPath\BuildAutomationTool.csproj"
Write-Host "Starting version bump process..." -ForegroundColor Cyan
$currentVersion = Get-CurrentVersion -csprojPath $csprojPath
Write-Host "Current version: $currentVersion" -ForegroundColor Yellow
$newVersion = Bump-PatchVersion -VersionType $VersionType -CurrentVersion $currentVersion
Write-Host "New version: $newVersion" -ForegroundColor Yellow
Update-VersionInCsproj -csprojPath $csprojPath -newVersion $newVersion
# Commit-VersionChange -newVersion $newVersion
Write-Host "Version bump process completed." -ForegroundColor Cyan