/
BuildFunctions.ps1
300 lines (251 loc) · 8.47 KB
/
BuildFunctions.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function Get-Current-Branch-Name ()
{
$branch = ([string](git rev-parse --abbrev-ref HEAD)).Trim()
$parts = $branch.Split('/')
$branch = $parts[$parts.Length - 1]
return $branch
}
function Get-Current-Commit-Hash ()
{
return ([string](git log -1 --pretty=%h)).Trim()
}
function Get-Current-Commit-Message ()
{
return ([string](git log -1 --pretty=%B)).Trim()
}
function Test-Should-Deploy ()
{
$nugetGitVersion = Get-Git-Package-Version
$buildMetaData = Get-Git-Build-MetaData
$fullSemVer = Get-Git-Full-Sem-Ver
if (Test-Tag-Build $nugetGitVersion $buildMetaData $fullSemVer) {
return $true
}
return $false
}
function Get-Published-PreRelase-Package ($PackageId)
{
$out = [string](nuget list -PreRelease id:$PackageId)
$version = $out.Split(' ')[1]
return $version
}
function Get-Published-PreRelase-Package-Version ($PackageId)
{
$version = Get-Published-PreRelase-Package $PackageId
$version = $version.Split('-')[0]
return $version
}
function Get-Published-Package ($PackageId)
{
$out = [string](nuget list id:$PackageId)
$version = $out.Split(' ')[1]
return $version
}
function Get-Published-Package-Version ($PackageId)
{
$version = Get-Published-Package $PackageId
$version = $version.Split('-')[0]
return $version
}
function Get-Git-Package-Version ()
{
Update-Ensure-Git-Not-Detached
return [string](gitversion /showvariable MajorMinorPatch)
}
function Get-Git-Build-MetaData ()
{
Update-Ensure-Git-Not-Detached
return [string](gitversion /showvariable BuildMetaData)
}
function Get-Git-Full-Sem-Ver ()
{
Update-Ensure-Git-Not-Detached
return [string](gitversion /showvariable FullSemVer)
}
function Update-NuSpec-Release-Notes($File, $releaseNotes)
{
$File = Resolve-Path $File
[xml] $fileContents = Get-Content -Encoding UTF8 -Path $File
$releaseNotesPath = "package.metadata.releaseNotes"
if ($releaseNotes -ne $null -and $releaseNotes -ne "") {
Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath $releaseNotesPath -TextValue $releaseNotes
}
$fileContents.Save($File)
}
function Update-NuSpec-Version($File, $Version)
{
$File = Resolve-Path $File
[xml] $fileContents = Get-Content -Encoding UTF8 -Path $File
$versionPath = "package.metadata.version"
if ($Version -ne $null -and $Version -ne "") {
Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath $versionPath -TextValue $Version
}
$fileContents.Save($File)
}
function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
# If a Namespace URI was not given, use the Xml document's default namespace.
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }
# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager # Need to put the comma before the variable name so that PowerShell doesn't convert it into an Object[].
}
function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.')
{
return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))"
}
function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
# Try and get the node, then return it. Returns $null if the node was not found.
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
return $node
}
function Set-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$TextValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
# Try and get the node.
$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
# If the node already exists, update its value.
if ($node)
{
$node.InnerText = $TextValue
}
# Else the node doesn't exist yet, so create it with the given value.
else
{
# Create the new element with the given value.
$elementName = $ElementPath.Substring($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1)
$element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI)
$textNode = $XmlDocument.CreateTextNode($TextValue)
$element.AppendChild($textNode) > $null
# Try and get the parent node.
$parentNodePath = $ElementPath.Substring(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter))
$parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
if ($parentNode)
{
$parentNode.AppendChild($element) > $null
}
else
{
throw "$parentNodePath does not exist in the xml."
}
}
}
function Test-Tag-Build ($nugetGitVersion, $buildMetaData, $fullSemVer) {
if ([string]::IsNullOrEmpty($buildMetaData) -and $fullSemVer -eq $nugetGitVersion) {
return $true
}
return $false
}
function Test-Stable-Release ($stableVersion, $preReleaseVersion, $nugetGitVersion, $buildMetaData, $fullSemVer)
{
# This is unlikelly to heppen, but could happen if the tag creation didn't triggered the Package
if ($stableVersion -ne $preReleaseVersion -and $preReleaseVersion -ne $nugetGitVersion) {
return 1
}
if (Test-Tag-Build $nugetGitVersion $buildMetaData $fullSemVer) {
return 2
}
return 0
}
function Set-Forced-Git-Version ($version)
{
Write-Output "next-version: $version" > GitVersion.yml
}
function Get-Prefix-Name ()
{
$branchName = Get-Current-Branch-Name
if ($branchName -ne "master") {
return $branchName
}
return "beta"
}
function Get-Next-Version-String ($PackageId)
{
$stableVersion = Get-Published-Package-Version ($PackageId)
$preReleaseVersion = Get-Published-PreRelase-Package-Version ($PackageId)
$nugetGitVersion = Get-Git-Package-Version
$buildMetaData = Get-Git-Build-MetaData
$fullSemVer = Get-Git-Full-Sem-Ver
$prefix = Get-Prefix-Name
$prefix = $prefix.Replace("-", "")
$prefix = $prefix.Replace("_", "")
$prefix = $prefix.Replace(".", "")
$nextVersion = ""
$stable = Test-Stable-Release $stableVersion $preReleaseVersion $nugetGitVersion $buildMetaData $fullSemVer
if ($stable -eq 1){
$nextVersion = $preReleaseVersion
Set-Forced-Git-Version $nextVersion
} elseif ($stable -eq 2) {
$nextVersion = $nugetGitVersion
} else {
$nextVersion = "$($nugetGitVersion)-$($prefix)-build$($buildMetaData)"
}
return $nextVersion
}
function Test-Version-Stable-Release ($version)
{
$count = $version.Split("-").Length
if ($count -eq 3) {
return $false
}
return $true
}
function Test-Package-Already-Published ($PackageId, $nextVersion)
{
if (Test-Version-Stable-Release $nextVersion) {
$publishedVersion = Get-Published-Package $PackageId
if ($publishedVersion -eq $nextVersion) {
return $true
}
return $false
}
$buildMetaData = Get-Git-Build-MetaData
$currentVersion = $nextVersion.Split('-')[0]
[int]$currentBuild = [Convert]::ToInt32($buildMetaData, 10)
$currentPrefix = Get-Prefix-Name
$version = Get-Published-PreRelase-Package $PackageId
#if the latest pre-relese returned is a stable release
if (Test-Version-Stable-Release $version) {
return $false
}
$publishedVersion = $version.Split('-')[0]
$publishedPrefix = $version.Split('-')[1]
$build = $version.Split('-')[2]
$build = $build.Replace("build", "")
[int]$publishedBuild = [Convert]::ToInt32($build, 10)
if ($currentVersion -ne $publishedVersion) {
return $false
}
if ($currentPrefix -ne $publishedPrefix) {
return $false
}
if ($currentBuild -gt $publishedPrefix) {
return $false
}
return $true
}
function Test-Git-Detached ()
{
$output = [string](git branch | head -1)
$detached = $output.Contains("detached")
return $detached
}
function Get-Git-Current-Tag
{
return [string](git describe --tags | head -1)
}
# GitVersion only works in attached branches, if we try to build a tag from the commit hash gitversion will fail
# so we ensure we are always attached to a branch
function Update-Ensure-Git-Not-Detached ()
{
$detached = Test-Git-Detached
if (!$detached) {
return
}
$currentTag = Get-Git-Current-Tag
& git checkout -B $currentTag
# We are using -B because maybe the branch alrady exist
}