|
| 1 | +function Get-Current-Branch-Name () |
| 2 | +{ |
| 3 | + $branch = ([string](git rev-parse --abbrev-ref HEAD)).Trim() |
| 4 | + $parts = $branch.Split('/') |
| 5 | + $branch = $parts[$parts.Length - 1] |
| 6 | + return $branch |
| 7 | +} |
| 8 | + |
| 9 | +function Get-Current-Commit-Hash () |
| 10 | +{ |
| 11 | + return ([string](git log -1 --pretty=%h)).Trim() |
| 12 | +} |
| 13 | + |
| 14 | +function Get-Current-Commit-Message () |
| 15 | +{ |
| 16 | + return ([string](git log -1 --pretty=%B)).Trim() |
| 17 | +} |
| 18 | + |
| 19 | +function Test-Should-Deploy () |
| 20 | +{ |
| 21 | + $nugetGitVersion = Get-Git-Package-Version |
| 22 | + $buildMetaData = Get-Git-Build-MetaData |
| 23 | + $fullSemVer = Get-Git-Full-Sem-Ver |
| 24 | + |
| 25 | + if (Test-Tag-Build $nugetGitVersion $buildMetaData $fullSemVer) { |
| 26 | + return $true |
| 27 | + } |
| 28 | + return $false |
| 29 | +} |
| 30 | + |
| 31 | +function Get-Published-PreRelase-Package ($PackageId) |
| 32 | +{ |
| 33 | + $out = [string](nuget list -PreRelease id:$PackageId) |
| 34 | + $version = $out.Split(' ')[1] |
| 35 | + return $version |
| 36 | +} |
| 37 | + |
| 38 | +function Get-Published-PreRelase-Package-Version ($PackageId) |
| 39 | +{ |
| 40 | + $version = Get-Published-PreRelase-Package $PackageId |
| 41 | + $version = $version.Split('-')[0] |
| 42 | + return $version |
| 43 | +} |
| 44 | + |
| 45 | +function Get-Published-Package ($PackageId) |
| 46 | +{ |
| 47 | + $out = [string](nuget list id:$PackageId) |
| 48 | + $version = $out.Split(' ')[1] |
| 49 | + return $version |
| 50 | +} |
| 51 | + |
| 52 | +function Get-Published-Package-Version ($PackageId) |
| 53 | +{ |
| 54 | + $version = Get-Published-Package $PackageId |
| 55 | + $version = $version.Split('-')[0] |
| 56 | + return $version |
| 57 | +} |
| 58 | + |
| 59 | +function Get-Git-Package-Version () |
| 60 | +{ |
| 61 | + Update-Ensure-Git-Not-Detached |
| 62 | + return [string](gitversion /showvariable MajorMinorPatch) |
| 63 | +} |
| 64 | + |
| 65 | +function Get-Git-Build-MetaData () |
| 66 | +{ |
| 67 | + Update-Ensure-Git-Not-Detached |
| 68 | + return [string](gitversion /showvariable BuildMetaData) |
| 69 | +} |
| 70 | + |
| 71 | +function Get-Git-Full-Sem-Ver () |
| 72 | +{ |
| 73 | + Update-Ensure-Git-Not-Detached |
| 74 | + |
| 75 | + return [string](gitversion /showvariable FullSemVer) |
| 76 | +} |
| 77 | + |
| 78 | +function Update-NuSpec-Release-Notes($File, $releaseNotes) |
| 79 | +{ |
| 80 | + $File = Resolve-Path $File |
| 81 | + |
| 82 | + [xml] $fileContents = Get-Content -Encoding UTF8 -Path $File |
| 83 | + |
| 84 | + $releaseNotesPath = "package.metadata.releaseNotes" |
| 85 | + |
| 86 | + if ($releaseNotes -ne $null -and $releaseNotes -ne "") { |
| 87 | + Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath $releaseNotesPath -TextValue $releaseNotes |
| 88 | + } |
| 89 | + $fileContents.Save($File) |
| 90 | +} |
| 91 | + |
| 92 | +function Update-NuSpec-Version($File, $Version) |
| 93 | +{ |
| 94 | + $File = Resolve-Path $File |
| 95 | + |
| 96 | + [xml] $fileContents = Get-Content -Encoding UTF8 -Path $File |
| 97 | + |
| 98 | + $versionPath = "package.metadata.version" |
| 99 | + |
| 100 | + if ($Version -ne $null -and $Version -ne "") { |
| 101 | + Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath $versionPath -TextValue $Version |
| 102 | + } |
| 103 | + |
| 104 | + $fileContents.Save($File) |
| 105 | +} |
| 106 | + |
| 107 | +function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "") |
| 108 | +{ |
| 109 | + # If a Namespace URI was not given, use the Xml document's default namespace. |
| 110 | + if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI } |
| 111 | + |
| 112 | + # 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. |
| 113 | + [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable) |
| 114 | + $xmlNsManager.AddNamespace("ns", $NamespaceURI) |
| 115 | + return ,$xmlNsManager # Need to put the comma before the variable name so that PowerShell doesn't convert it into an Object[]. |
| 116 | +} |
| 117 | + |
| 118 | +function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.') |
| 119 | +{ |
| 120 | + return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))" |
| 121 | +} |
| 122 | + |
| 123 | +function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') |
| 124 | +{ |
| 125 | + $xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI |
| 126 | + [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter |
| 127 | + |
| 128 | + # Try and get the node, then return it. Returns $null if the node was not found. |
| 129 | + $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager) |
| 130 | + return $node |
| 131 | +} |
| 132 | + |
| 133 | +function Set-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$TextValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') |
| 134 | +{ |
| 135 | + # Try and get the node. |
| 136 | + $node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter |
| 137 | + |
| 138 | + # If the node already exists, update its value. |
| 139 | + if ($node) |
| 140 | + { |
| 141 | + $node.InnerText = $TextValue |
| 142 | + } |
| 143 | + # Else the node doesn't exist yet, so create it with the given value. |
| 144 | + else |
| 145 | + { |
| 146 | + # Create the new element with the given value. |
| 147 | + $elementName = $ElementPath.Substring($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1) |
| 148 | + $element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI) |
| 149 | + $textNode = $XmlDocument.CreateTextNode($TextValue) |
| 150 | + $element.AppendChild($textNode) > $null |
| 151 | + |
| 152 | + # Try and get the parent node. |
| 153 | + $parentNodePath = $ElementPath.Substring(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter)) |
| 154 | + $parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter |
| 155 | + |
| 156 | + if ($parentNode) |
| 157 | + { |
| 158 | + $parentNode.AppendChild($element) > $null |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + throw "$parentNodePath does not exist in the xml." |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +function Test-Tag-Build ($nugetGitVersion, $buildMetaData, $fullSemVer) { |
| 168 | + if ([string]::IsNullOrEmpty($buildMetaData) -and $fullSemVer -eq $nugetGitVersion) { |
| 169 | + return $true |
| 170 | + } |
| 171 | + return $false |
| 172 | +} |
| 173 | + |
| 174 | +function Test-Stable-Release ($stableVersion, $preReleaseVersion, $nugetGitVersion, $buildMetaData, $fullSemVer) |
| 175 | +{ |
| 176 | + # This is unlikelly to heppen, but could happen if the tag creation didn't triggered the Package |
| 177 | + if ($stableVersion -ne $preReleaseVersion -and $preReleaseVersion -ne $nugetGitVersion) { |
| 178 | + return 1 |
| 179 | + } |
| 180 | + if (Test-Tag-Build $nugetGitVersion $buildMetaData $fullSemVer) { |
| 181 | + return 2 |
| 182 | + } |
| 183 | + return 0 |
| 184 | +} |
| 185 | + |
| 186 | +function Set-Forced-Git-Version ($version) |
| 187 | +{ |
| 188 | + Write-Output "next-version: $version" > GitVersion.yml |
| 189 | +} |
| 190 | + |
| 191 | +function Get-Prefix-Name () |
| 192 | +{ |
| 193 | + $branchName = Get-Current-Branch-Name |
| 194 | + |
| 195 | + if ($branchName -ne "master") { |
| 196 | + return $branchName |
| 197 | + } |
| 198 | + return "beta" |
| 199 | +} |
| 200 | + |
| 201 | +function Get-Next-Version-String ($PackageId) |
| 202 | +{ |
| 203 | + $stableVersion = Get-Published-Package-Version ($PackageId) |
| 204 | + $preReleaseVersion = Get-Published-PreRelase-Package-Version ($PackageId) |
| 205 | + |
| 206 | + $nugetGitVersion = Get-Git-Package-Version |
| 207 | + $buildMetaData = Get-Git-Build-MetaData |
| 208 | + $fullSemVer = Get-Git-Full-Sem-Ver |
| 209 | + |
| 210 | + $prefix = Get-Prefix-Name |
| 211 | + $prefix = $prefix.Replace("-", "") |
| 212 | + $prefix = $prefix.Replace("_", "") |
| 213 | + $prefix = $prefix.Replace(".", "") |
| 214 | + $nextVersion = "" |
| 215 | + |
| 216 | + $stable = Test-Stable-Release $stableVersion $preReleaseVersion $nugetGitVersion $buildMetaData $fullSemVer |
| 217 | + if ($stable -eq 1){ |
| 218 | + $nextVersion = $preReleaseVersion |
| 219 | + Set-Forced-Git-Version $nextVersion |
| 220 | + } elseif ($stable -eq 2) { |
| 221 | + $nextVersion = $nugetGitVersion |
| 222 | + } else { |
| 223 | + $nextVersion = "$($nugetGitVersion)-$($prefix)-build$($buildMetaData)" |
| 224 | + } |
| 225 | + return $nextVersion |
| 226 | +} |
| 227 | + |
| 228 | +function Test-Version-Stable-Release ($version) |
| 229 | +{ |
| 230 | + $count = $version.Split("-").Length |
| 231 | + if ($count -eq 3) { |
| 232 | + return $false |
| 233 | + } |
| 234 | + return $true |
| 235 | +} |
| 236 | + |
| 237 | +function Test-Package-Already-Published ($PackageId, $nextVersion) |
| 238 | +{ |
| 239 | + if (Test-Version-Stable-Release $nextVersion) { |
| 240 | + $publishedVersion = Get-Published-Package $PackageId |
| 241 | + if ($publishedVersion -eq $nextVersion) { |
| 242 | + return $true |
| 243 | + } |
| 244 | + return $false |
| 245 | + } |
| 246 | + |
| 247 | + $buildMetaData = Get-Git-Build-MetaData |
| 248 | + $currentVersion = $nextVersion.Split('-')[0] |
| 249 | + |
| 250 | + [int]$currentBuild = [Convert]::ToInt32($buildMetaData, 10) |
| 251 | + $currentPrefix = Get-Prefix-Name |
| 252 | + |
| 253 | + $version = Get-Published-PreRelase-Package $PackageId |
| 254 | + #if the latest pre-relese returned is a stable release |
| 255 | + if (Test-Version-Stable-Release $version) { |
| 256 | + return $false |
| 257 | + } |
| 258 | + |
| 259 | + $publishedVersion = $version.Split('-')[0] |
| 260 | + $publishedPrefix = $version.Split('-')[1] |
| 261 | + $build = $version.Split('-')[2] |
| 262 | + $build = $build.Replace("build", "") |
| 263 | + [int]$publishedBuild = [Convert]::ToInt32($build, 10) |
| 264 | + |
| 265 | + if ($currentVersion -ne $publishedVersion) { |
| 266 | + return $false |
| 267 | + } |
| 268 | + if ($currentPrefix -ne $publishedPrefix) { |
| 269 | + return $false |
| 270 | + } |
| 271 | + if ($currentBuild -gt $publishedPrefix) { |
| 272 | + return $false |
| 273 | + } |
| 274 | + return $true |
| 275 | +} |
| 276 | + |
| 277 | +function Test-Git-Detached () |
| 278 | +{ |
| 279 | + $output = [string](git branch | head -1) |
| 280 | + $detached = $output.Contains("detached") |
| 281 | + return $detached |
| 282 | +} |
| 283 | + |
| 284 | +function Get-Git-Current-Tag |
| 285 | +{ |
| 286 | + return [string](git describe --tags | head -1) |
| 287 | +} |
| 288 | + |
| 289 | +# GitVersion only works in attached branches, if we try to build a tag from the commit hash gitversion will fail |
| 290 | +# so we ensure we are always attached to a branch |
| 291 | +function Update-Ensure-Git-Not-Detached () |
| 292 | +{ |
| 293 | + $detached = Test-Git-Detached |
| 294 | + if (!$detached) { |
| 295 | + return |
| 296 | + } |
| 297 | + $currentTag = Get-Git-Current-Tag |
| 298 | + & git checkout -B $currentTag |
| 299 | + # We are using -B because maybe the branch alrady exist |
| 300 | +} |
0 commit comments