-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncreaseVersion.ps1
171 lines (133 loc) · 5.95 KB
/
IncreaseVersion.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
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
[CmdletBinding()]
param()
function Get-VersionPattern {
return "\d+\.\d+\.\d+\.\d+"
}
function Get-Pattern($filePath, $VersionProperty) {
Write-Debug "Get-Pattern: [FileName=$filePath, VersionProperty=$VersionProperty]"
$isCS = $filePath.ToLower().endswith(".cs")
$isCSPROJ = $filePath.ToLower().endswith(".csproj")
if($isCS -and ($VersionProperty -ne "AssemblyVersion" -and $VersionProperty -ne "AssemblyFileVersion")){
Write-Error "Version Property not valid for cs file ($VersionProperty)"
return;
}
elseif ($isCSPROJ -and ($VersionProperty -ne "AssemblyVersion" -and $VersionProperty -ne "FileVersion" -and $VersionProperty -ne "Version")){
Write-Error "Version Property not valid for csproj file ($VersionProperty)"
return;
}
# AssemblyVersion, AssemblyFileVersion
if($isCS) {
$pattern = '\[assembly\: __TYPE__\("__VersionPattern__"\)\]'
}
# AssemblyVersion, FileVersion, Version
elseif ($isCSPROJ) {
$pattern = "<__TYPE__>(__VersionPattern__)</__TYPE__>"
}
else
{
Write-Error "File type unknow."
return;
}
$versionPattern = Get-VersionPattern
return $pattern.Replace("__VersionPattern__", $versionPattern).Replace("__TYPE__", $VersionProperty);
}
function Get-CurrentVersion($filePath, $VersionProperty) {
Write-Debug "Get-CurrentVersion: [FileName=$filePath, VersionProperty=$VersionProperty]"
if($VersionProperty -eq "AllCSVersion"){
$VersionProperty = "AssemblyVersion"
}
$pattern = Get-Pattern -filePath $filePath -VersionProperty $VersionProperty
$contents = [System.IO.File]::ReadAllText($filePath)
$tempString = [RegEx]::Match($contents, $pattern)
Write-Debug "---> Line Version: $tempString"
$versionPattern = Get-VersionPattern
$versionString = [RegEx]::Match($tempString.Value, $versionPattern)
Write-Debug "---> Version: $versionString"
if (-not $versionString.Value) {
Write-Error "cs File not contain version number."
}
return $versionString.Value
}
function Get-IncVersion($versionType, $currentVersion, $customVersion) {
Write-Debug "Get-IncVersion: [VersionType=$versionType, CurrentVersion=$currentVersion, CustomVersion=$customVersion]"
if ($versionType -eq "Custom") {
if(-not $customVersion)
{
Write-Error "Custom Type is Empty"
return;
}
$versionType = $customVersion
}
if ($versionType -eq 0 -or $versionType -eq "None") {
Write-Host ("AssemblyFileVersion without change.")
return;
}
$incVersion = $currentVersion
$version = [version]$currentVersion
if ($versionType -eq 1 -or $versionType -ieq "Major") {
$incVersion = "{0}.{1}.{2}.{3}" -f ($version.Major + 1), 0, 0, 0
}
ElseIf ($versionType -eq 2 -or $versionType -ieq "Minor") {
$incVersion = "{0}.{1}.{2}.{3}" -f $version.Major, ($version.Minor + 1), 0, 0
}
ElseIf ($versionType -eq 3 -or $versionType -ieq "Build") {
$incVersion = "{0}.{1}.{2}.{3}" -f $version.Major, $version.Minor, ($version.Build + 1), 0
}
ElseIf ($versionType -eq 4 -or $versionType -ieq "Revision") {
$incVersion = "{0}.{1}.{2}.{3}" -f $version.Major, $version.Minor, $version.Build, ($version.Revision + 1)
}
Else {
try {
$a = [version]$versionType
if ($a.Revision -eq -1) {
Write-Error "Version type not valid, Not contain 4 digit ($versionType)"
return
}
$incVersion = $versionType
}
Catch {
Write-Error "Version type not valid ($versionType)"
return
}
}
return [string]$incVersion
}
function Update-Version($FilePath, $versionProperty, $newVersion) {
Write-Debug "Update-Version: [FileName=$FilePath, VersionProperty=$versionProperty, NewVersion=$newVersion]"
$pattern = Get-Pattern -filePath $FilePath -VersionProperty $versionProperty
$contents = [System.IO.File]::ReadAllText($FilePath)
$currentLine = [RegEx]::Match($contents, $pattern) #Get relevant line
$versionPattern = Get-VersionPattern
$version = [RegEx]::Match($currentLine, $versionPattern) #Get current version
$newLine = $currentLine.Value -replace $version, $newVersion #Update new version
$contents = $contents.Replace($currentLine.Value, $newLine) #Update contents
[System.IO.File]::WriteAllText($FilePath, $contents) #Save contents to file
}
Write-Debug "---> Read VSTS Inputs"
#Read Detail from vsts inputs
$filePathInput = Get-VstsInput -Name filePath -Require
$versionTypeInput = Get-VstsInput -Name versionType -Require
$variableNameInput = Get-VstsInput -Name variableName -Require
$customVersionInput = Get-VstsInput -Name CustomType
$versionPropertyInput = Get-VstsInput -Name versionProperty -Require
$updateIncVersionInput = Get-VstsInput -Name updateIncVersion -Require
$currentVersion = Get-CurrentVersion -filePath $filePathInput -VersionProperty $versionPropertyInput
Write-Host ("Current Version: " + $currentVersion)
$newVersion = Get-IncVersion -versionType $versionTypeInput -currentVersion $currentVersion -customVersion $customVersionInput
if($variableNameInput)
{
Write-Debug "--> Update variable with new version"
Write-Host ("New Version: " + $newVersion)
Write-Host ("##vso[task.setvariable variable=$variableNameInput]$newVersion")
}
if($updateIncVersionInput -and $newVersion -and $currentVersion -ne $newVersion){
Write-Host ("Check-Out & Update file with new version")
tf checkout $filePathInput
if($versionPropertyInput -eq "AllCSVersion"){
Update-Version -FilePath $filePathInput -VersionProperty "AssemblyVersion" -newVersion $newVersion
Update-Version -FilePath $filePathInput -VersionProperty "AssemblyFileVersion" -newVersion $newVersion
}
else{
Update-Version -FilePath $filePathInput -VersionProperty $versionPropertyInput -newVersion $newVersion
}
}