"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/tools/releaseTools.psm1" (11 Aug 2022, 23816 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #requires -Version 6.0
    2 # Copyright (c) Microsoft Corporation.
    3 # Licensed under the MIT License.
    4 
    5 class CommitNode {
    6     [string] $Hash
    7     [string[]] $Parents
    8     [string] $AuthorName
    9     [string] $AuthorGitHubLogin
   10     [string] $AuthorEmail
   11     [string] $Subject
   12     [string] $Body
   13     [string] $PullRequest
   14     [string] $ChangeLogMessage
   15     [string] $ThankYouMessage
   16     [bool] $IsBreakingChange
   17 
   18     CommitNode($hash, $parents, $name, $email, $subject, $body) {
   19         $this.Hash = $hash
   20         $this.Parents = $parents
   21         $this.AuthorName = $name
   22         $this.AuthorEmail = $email
   23         $this.Subject = $subject
   24         $this.Body = $body
   25         $this.IsBreakingChange = $body -match "\[breaking change\]"
   26 
   27         if ($subject -match "\(#(\d+)\)$") {
   28             $this.PullRequest = $Matches[1]
   29         }
   30     }
   31 }
   32 
   33 # These powershell team members don't use 'microsoft.com' for Github email or choose to not show their emails.
   34 # We have their names in this array so that we don't need to query GitHub to find out if they are powershell team members.
   35 $Script:powershell_team = @(
   36     "Robert Holt"
   37     "Travis Plunk"
   38     "dependabot-preview[bot]"
   39     "dependabot[bot]"
   40     "Joey Aiello"
   41     "Tyler James Leonhardt"
   42     "Anam Navied"
   43     "Andrew Schwartzmeyer"
   44     "Jason Helmick"
   45 )
   46 
   47 # They are very active contributors, so we keep their email-login mappings here to save a few queries to Github.
   48 $Script:community_login_map = @{
   49     "darpa@yandex.ru" = "iSazonov"
   50     "c.bergmeister@gmail.com" = "bergmeister"
   51     "github@markekraus.com" = "markekraus"
   52     "info@powercode-consulting.se" = "powercode"
   53 }
   54 
   55 # Ignore dependency bumping bot (Dependabot):
   56 $Script:attribution_ignore_list = @(
   57     'dependabot[bot]@users.noreply.github.com'
   58 )
   59 
   60 ##############################
   61 #.SYNOPSIS
   62 #In the release workflow, the release branch will be merged back to master after the release is done,
   63 #and a merge commit will be created as the child of the release tag commit.
   64 #This cmdlet takes a release tag or the corresponding commit hash, find its child merge commit, and
   65 #return its metadata in this format: <merge-commit-hash>|<parent-commit-hashes>
   66 #
   67 #.PARAMETER LastReleaseTag
   68 #The last release tag
   69 #
   70 #.PARAMETER CommitHash
   71 #The commit hash of the last release tag
   72 #
   73 #.OUTPUTS
   74 #Return the metadata of the child merge commit, in this format: <merge-commit-hash>|<parent-commit-hashes>
   75 ##############################
   76 function Get-ChildMergeCommit
   77 {
   78     [CmdletBinding(DefaultParameterSetName="TagName")]
   79     param(
   80         [Parameter(Mandatory, ParameterSetName="TagName")]
   81         [string]$LastReleaseTag,
   82 
   83         [Parameter(Mandatory, ParameterSetName="CommitHash")]
   84         [string]$CommitHash
   85     )
   86 
   87     $tag_hash = $CommitHash
   88     if ($PSCmdlet.ParameterSetName -eq "TagName") { $tag_hash = git rev-parse "$LastReleaseTag^0" }
   89 
   90     ## Get the merge commits that are reachable from 'HEAD' but not from the release tag
   91     $merge_commits_not_in_release_branch = git --no-pager log --merges "$tag_hash..HEAD" --format='%H||%P'
   92     ## Find the child merge commit, whose parent-commit-hashes contains the release tag hash
   93     $child_merge_commit = $merge_commits_not_in_release_branch | Select-String -SimpleMatch $tag_hash
   94     return $child_merge_commit.Line
   95 }
   96 
   97 ##############################
   98 #.SYNOPSIS
   99 #Create a CommitNode instance to represent a commit.
  100 #
  101 #.PARAMETER CommitMetadata
  102 #The commit metadata. It's in this format:
  103 #<commit-hash>|<parent-hashes>|<author-name>|<author-email>|<commit-subject>
  104 #
  105 #.PARAMETER CommitMetadata
  106 #The commit metadata, in this format:
  107 #<commit-hash>|<parent-hashes>|<author-name>|<author-email>|<commit-subject>
  108 #
  109 #.OUTPUTS
  110 #Return the 'CommitNode' object
  111 ##############################
  112 function New-CommitNode
  113 {
  114     param(
  115         [Parameter(ValueFromPipeline)]
  116         [ValidatePattern("^.+\|.+\|.+\|.+\|.+$")]
  117         [string]$CommitMetadata
  118     )
  119 
  120     Process {
  121         $hash, $parents, $name, $email, $subject = $CommitMetadata.Split("||")
  122         $body = (git --no-pager show $hash -s --format=%b) -join "`n"
  123         return [CommitNode]::new($hash, $parents, $name, $email, $subject, $body)
  124     }
  125 }
  126 
  127 ##############################
  128 #.SYNOPSIS
  129 #Generate the draft change log.
  130 #
  131 #.PARAMETER LastReleaseTag
  132 #The last release tag
  133 #
  134 #.PARAMETER HasCherryPick
  135 #Indicate whether there are any commits in the last release branch that were cherry-picked from the master branch
  136 #
  137 #.PARAMETER Token
  138 #The authentication token to use for retrieving the GitHub user log-in names for external contributors
  139 #
  140 #.OUTPUTS
  141 #The generated change log draft.
  142 ##############################
  143 function Get-ChangeLog
  144 {
  145     param(
  146         [Parameter(Mandatory = $true)]
  147         [string]$LastReleaseTag,
  148 
  149         [Parameter(Mandatory = $true)]
  150         [string]$ThisReleaseTag,
  151 
  152         [Parameter(Mandatory)]
  153         [string]$Token,
  154 
  155         [Parameter()]
  156         [switch]$HasCherryPick
  157     )
  158 
  159     $tag_hash = git rev-parse "$LastReleaseTag^0"
  160     $format = '%H||%P||%aN||%aE||%s'
  161     $header = @{"Authorization"="token $Token"}
  162 
  163     # Find the merge commit that merged the release branch to master.
  164     $child_merge_commit = Get-ChildMergeCommit -CommitHash $tag_hash
  165     if($child_merge_commit)
  166     {
  167         $commit_hash, $parent_hashes = $child_merge_commit.Split("||")
  168     }
  169     # Find the other parent of the merge commit, which represents the original head of master right before merging.
  170     $other_parent_hash = ($parent_hashes -replace $tag_hash).Trim()
  171 
  172     if ($HasCherryPick) {
  173         ## Sometimes we need to cherry-pick some commits from the master branch to the release branch during the release,
  174         ## and eventually merge the release branch back to the master branch. This will result in different commit nodes
  175         ## in master branch that actually represent same set of changes.
  176         ##
  177         ## In this case, we cannot simply use the revision range "$tag_hash..HEAD" because it will include the original
  178         ## commits in the master branch that were cherry-picked to the release branch -- they are reachable from 'HEAD'
  179         ## but not reachable from the last release tag. Instead, we need to exclude the commits that were cherry-picked,
  180         ## and only include the commits that are not in the last release into the change log.
  181 
  182         # Find the commits that were only in the orginal master, excluding those that were cherry-picked to release branch.
  183         $new_commits_from_other_parent = git --no-pager log --first-parent --cherry-pick --right-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode
  184         # Find the commits that were only in the release branch, excluding those that were cherry-picked from master branch.
  185         $new_commits_from_last_release = git --no-pager log --first-parent --cherry-pick --left-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode
  186         # Find the commits that are actually duplicate but having different patch-ids due to resolving conflicts during the cherry-pick.
  187         $duplicate_commits = $null
  188         if($new_commits_from_last_release -and $new_commits_from_other_parent)
  189         {
  190             $duplicate_commits = Compare-Object $new_commits_from_last_release $new_commits_from_other_parent -Property PullRequest -ExcludeDifferent -IncludeEqual -PassThru
  191         }
  192         if ($duplicate_commits) {
  193             $duplicate_pr_numbers = @($duplicate_commits | ForEach-Object -MemberName PullRequest)
  194             $new_commits_from_other_parent = $new_commits_from_other_parent | Where-Object PullRequest -NotIn $duplicate_pr_numbers
  195         }
  196 
  197         # Find the commits that were made after the merge commit.
  198         $new_commits_after_merge_commit = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode)
  199         $new_commits = $new_commits_after_merge_commit + $new_commits_from_other_parent
  200     } else {
  201         ## No cherry-pick was involved in the last release branch.
  202         ## Using a ref rang like "$tag_hash..HEAD" with 'git log' means getting the commits that are reachable from 'HEAD' but not reachable from the last release tag.
  203 
  204         ## We use '--first-parent' for 'git log'. It means for any merge node, only follow the parent node on the master branch side.
  205         ## In case we merge a branch to master for a PR, only the merge node will show up in this way, the individual commits from that branch will be ignored.
  206         ## This is what we want because the merge commit itself already represents the PR.
  207 
  208         ## First, we want to get all new commits merged during the last release
  209         $new_commits_during_last_release = @(git --no-pager log --first-parent "$tag_hash..$other_parent_hash" --format=$format | New-CommitNode)
  210         ## Then, we want to get all new commits merged after the last release
  211         $new_commits_after_last_release  = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode)
  212         ## Last, we get the full list of new commits
  213         $new_commits = $new_commits_during_last_release + $new_commits_after_last_release
  214     }
  215 
  216     # Array of unlabled PRs.
  217     $unlabeledPRs = @()
  218 
  219     # Array of PRs with multiple labels. The label "CL-BreakingChange" is allowed with some other "CL-*" label.
  220     $multipleLabelsPRs = @()
  221 
  222     # Array of PRs tagged with 'CL-BreakingChange' label.
  223     $clBreakingChange = @()
  224 
  225     # Array of PRs tagged with 'CL-BuildPackaging' label.
  226     $clBuildPackage = @()
  227 
  228     # Array of PRs tagged with 'CL-CodeCleanup' label.
  229     $clCodeCleanup = @()
  230 
  231     # Array of PRs tagged with 'CL-Docs' label.
  232     $clDocs = @()
  233 
  234     # Array of PRs tagged with 'CL-Engine' label.
  235     $clEngine = @()
  236 
  237     # Array of PRs with general cmdlet changes.
  238     $clGeneral = @()
  239 
  240     # Array of PRs tagged with 'CL-Performance' label.
  241     $clPerformance = @()
  242 
  243     # Array of PRs tagged with 'CL-Test' label.
  244     $clTest = @()
  245 
  246     # Array of PRs tagged with 'CL-Tools' label.
  247     $clTools = @()
  248 
  249     # Array of PRs tagged with 'CL-Untagged' label.
  250     $clUntagged = @()
  251 
  252     # Array of PRs tagged with 'CL-Experimental' label.
  253     $clExperimental = @()
  254 
  255     foreach ($commit in $new_commits) {
  256         Write-Verbose "authorname: $($commit.AuthorName)"
  257         if ($commit.AuthorEmail.EndsWith("@microsoft.com") -or $powershell_team -contains $commit.AuthorName -or $Script:attribution_ignore_list -contains $commit.AuthorEmail) {
  258             $commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commit.Subject)
  259         } else {
  260             if ($community_login_map.ContainsKey($commit.AuthorEmail)) {
  261                 $commit.AuthorGitHubLogin = $community_login_map[$commit.AuthorEmail]
  262             } else {
  263                 $uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)"
  264                 try{
  265                     $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header -ErrorAction Ignore
  266                 } catch{}
  267                 if($response)
  268                 {
  269                     $content = ConvertFrom-Json -InputObject $response.Content
  270                     $commit.AuthorGitHubLogin = $content.author.login
  271                     $community_login_map[$commit.AuthorEmail] = $commit.AuthorGitHubLogin
  272                 }
  273             }
  274             $commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commit.Subject), $commit.AuthorGitHubLogin)
  275             $commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin))
  276         }
  277 
  278         if ($commit.IsBreakingChange) {
  279             $commit.ChangeLogMessage = "{0} [Breaking Change]" -f $commit.ChangeLogMessage
  280         }
  281 
  282         ## Get the labels for the PR
  283         try {
  284             $pr = Invoke-RestMethod -Uri "https://api.github.com/repos/PowerShell/PowerShell/pulls/$($commit.PullRequest)" -Headers $header -ErrorAction SilentlyContinue
  285         }
  286         catch {
  287             if ($_.Exception.Response.StatusCode -eq '404') {
  288                 $pr = $null
  289                 #continue
  290             }
  291         }
  292 
  293         if($pr)
  294         {
  295             $clLabel = $pr.labels | Where-Object { $_.Name -match "^CL-"}
  296         }
  297         else {
  298             Write-Warning -Message "Tagging $($commit.Hash) by $($commit.AuthorName), as CL-BuildPackaging as it does not have a PR."
  299             $clLabel = [PSCustomObject]@{Name ='CL-BuildPackaging'}
  300         }
  301 
  302         if ($clLabel.count -gt 1 -and $clLabel.Name -notcontains 'CL-BreakingChange') {
  303             $multipleLabelsPRs += $pr
  304         }
  305         elseif ($clLabel.count -eq 0) {
  306             $unlabeledPRs += $pr
  307         }
  308         else {
  309             switch ($clLabel.Name) {
  310                 "CL-BreakingChange" { $clBreakingChange += $commit }
  311                 "CL-BuildPackaging" { $clBuildPackage += $commit }
  312                 "CL-CodeCleanup" { $clCodeCleanup += $commit }
  313                 "CL-Docs" { $clDocs += $commit }
  314                 "CL-Engine" { $clEngine += $commit }
  315                 "CL-Experimental" { $clExperimental += $commit }
  316                 "CL-General" { $clGeneral += $commit }
  317                 "CL-Performance" { $clPerformance += $commit }
  318                 "CL-Test" { $clTest += $commit }
  319                 "CL-Tools" { $clTools += $commit }
  320                 "CL-Untagged" { $clUntagged += $commit }
  321                 "CL-NotInBuild" { continue }
  322                 Default { throw "unknown tag '$cLabel' for PR: '$($commit.PullRequest)'" }
  323             }
  324         }
  325     }
  326 
  327     if ($multipleLabelsPRs.count -gt 0) {
  328         Write-Error "PRs should not be tagged with multiple CL labels. PRs with multiple labels: $($multipleLabelsPRs.number -join ' ')"
  329         $shouldThrow = $true
  330     }
  331 
  332     if ($unlabeledPRs.count -gt 0) {
  333         Write-Error "PRs should have at least one CL label. PRs missing labels: $($unlabeledPRs.number -join ' ')"
  334         $shouldThrow = $true
  335     }
  336 
  337     if ($shouldThrow) {
  338         throw "Some PRs are tagged multiple times or have no tags."
  339     }
  340 
  341     # Write output
  342 
  343     $version = $ThisReleaseTag.TrimStart('v')
  344 
  345     Write-Output "## [${version}] - $(Get-Date -Format yyyy-MM-dd)`n"
  346 
  347     PrintChangeLog -clSection $clUntagged -sectionTitle 'UNTAGGED - Please classify'
  348     PrintChangeLog -clSection $clBreakingChange -sectionTitle 'Breaking Changes'
  349     PrintChangeLog -clSection $clEngine -sectionTitle 'Engine Updates and Fixes'
  350     PrintChangeLog -clSection $clExperimental -sectionTitle 'Experimental Features'
  351     PrintChangeLog -clSection $clPerformance -sectionTitle 'Performance'
  352     PrintChangeLog -clSection $clGeneral -sectionTitle 'General Cmdlet Updates and Fixes'
  353     PrintChangeLog -clSection $clCodeCleanup -sectionTitle 'Code Cleanup' -Compress
  354     PrintChangeLog -clSection $clTools -sectionTitle 'Tools'
  355     PrintChangeLog -clSection $clTest -sectionTitle 'Tests'
  356     PrintChangeLog -clSection $clBuildPackage -sectionTitle 'Build and Packaging Improvements' -Compress
  357     PrintChangeLog -clSection $clDocs -sectionTitle 'Documentation and Help Content'
  358 
  359     Write-Output "[${version}]: https://github.com/PowerShell/PowerShell/compare/${LastReleaseTag}...${ThisReleaseTag}`n"
  360 }
  361 
  362 function PrintChangeLog($clSection, $sectionTitle, [switch] $Compress) {
  363     if ($clSection.Count -gt 0) {
  364         "### $sectionTitle`n"
  365 
  366         if ($Compress) {
  367             $items = $clSection.ChangeLogMessage -join "`n"
  368             $thankYou = "We thank the following contributors!`n`n"
  369             $thankYou += ($clSection.ThankYouMessage | Select-Object -Unique | Where-Object { if($_) { return $true} return $false}) -join ", "
  370 
  371             "<details>`n"
  372             "<summary>`n"
  373             $thankYou | ConvertFrom-Markdown | Select-Object -ExpandProperty Html
  374             "</summary>`n"
  375             $items | ConvertFrom-Markdown | Select-Object -ExpandProperty Html
  376             "</details>"
  377         }
  378         else {
  379             $clSection | ForEach-Object -MemberName ChangeLogMessage
  380         }
  381         ""
  382     }
  383 }
  384 
  385 function Get-ChangeLogMessage
  386 {
  387     param($OriginalMessage)
  388 
  389     switch -regEx ($OriginalMessage)
  390     {
  391         '^Merged PR (\d*): ' {
  392             return $OriginalMessage.replace($Matches.0,'') + " (Internal $($Matches.1))"
  393         }
  394         '^Build\(deps\): ' {
  395             return $OriginalMessage.replace($Matches.0,'')
  396         }
  397         default {
  398             return $OriginalMessage
  399         }
  400     }
  401 }
  402 
  403 ##############################
  404 #.SYNOPSIS
  405 #Gets packages which have newer packages in nuget.org
  406 #
  407 #.PARAMETER Path
  408 #The path to check for csproj files with packagse
  409 #
  410 #.PARAMETER IncludeAll
  411 #Include packages that don't need to be updated
  412 #
  413 #.OUTPUTS
  414 #Objects which represet the csproj package ref, with the current and new version
  415 ##############################
  416 function Get-NewOfficalPackage
  417 {
  418     param(
  419         [String]
  420         $Path = (Join-Path -Path $PSScriptRoot -ChildPath '..\src'),
  421         [Switch]
  422         $IncludeAll
  423     )
  424     # Calculate the filter to find the CSProj files
  425     $filter = Join-Path -Path $Path -ChildPath '*.csproj'
  426     $csproj = Get-ChildItem $filter -Recurse -Exclude 'PSGalleryModules.csproj'
  427 
  428     $csproj | ForEach-Object{
  429         $file = $_
  430 
  431         # parse the csproj
  432         [xml] $csprojXml = (Get-Content -Raw -Path $_)
  433 
  434         # get the package references
  435         $packages=$csprojXml.Project.ItemGroup.PackageReference
  436 
  437         # check to see if there is a newer package for each refernce
  438         foreach ($package in $packages)
  439         {
  440             # Get the name of the package
  441             $name = $package.Include
  442 
  443             if ($name)
  444             {
  445                 # Get the current package from nuget
  446                 $versions = Find-Package -Name $name -Source https://nuget.org/api/v2/  -ErrorAction SilentlyContinue -AllVersions |
  447                     Add-Member -Type ScriptProperty -Name Published -Value { $this.Metadata['published']} -PassThru |
  448                         Where-Object { Test-IncludePackageVersion -NewVersion $_.Version -Version $package.version}
  449 
  450                 $revsionRegEx = Get-MatchingMajorMinorRegEx -Version $package.version
  451                 $newPackage = $versions |
  452                     Sort-Object -Descending |
  453                         Select-Object -First 1
  454 
  455                 # Get the newest matching revision
  456                 $newRevision = $versions |
  457                     Where-Object {$_.Version -match $revsionRegEx } |
  458                         Sort-Object -Descending |
  459                             Select-Object -First 1
  460 
  461                 # If the current package has a different version from the version in the csproj, print the details
  462                 if ($newRevision -and $newRevision.Version.ToString() -ne $package.version -or $newPackage -and $newPackage.Version.ToString() -ne $package.version -or $IncludeAll.IsPresent)
  463                 {
  464                     if ($newRevision)
  465                     {
  466                         $newRevisionString = $newRevision.Version
  467                     }
  468                     else
  469                     {
  470                         # We don't have a new Revision, report the current version
  471                         $newRevisionString = $package.Version
  472                     }
  473 
  474                     if ($newPackage)
  475                     {
  476                         $newVersionString = $newPackage.Version
  477                     }
  478                     else
  479                     {
  480                         # We don't have a new Version, report the current version
  481                         $newVersionString = $package.Version
  482                     }
  483 
  484                     [pscustomobject]@{
  485                         Csproj = (Split-Path -Path $file -Leaf)
  486                         PackageName = $name
  487                         CsProjVersion = $Package.Version
  488                         NuGetRevision = $newRevisionString
  489                         NuGetVersion = $newVersionString
  490                     }
  491                 }
  492             }
  493         }
  494     }
  495 }
  496 
  497 ##############################
  498 #.SYNOPSIS
  499 # Returns True if NewVersion is newer than Version
  500 # Pre release are ignored if the current version is not pre-release
  501 # If the current version is pre-release, this function only determines if the version portion is NewReleaseTag
  502 # The calling function is responsible for sorting prelease version by publish date (as find-package gives them to you)
  503 # and returning the newest.
  504 #
  505 #.PARAMETER Version
  506 # The current Version
  507 #
  508 #.PARAMETER NewVersion
  509 # The potention replacement version
  510 #
  511 #.OUTPUTS
  512 # True if NewVersion should be considere as a replacement
  513 ##############################
  514 function Test-IncludePackageVersion
  515 {
  516     param(
  517         [string]
  518         $NewVersion,
  519         [string]
  520         $Version
  521     )
  522 
  523     $simpleCompare = $Version -notlike '*-*'
  524 
  525     if($simpleCompare -and $NewVersion -like '*-*')
  526     {
  527         # We are using a stable and the new version is pre-release
  528         return $false
  529     }
  530     elseif($simpleCompare -and [Version]$NewVersion -ge [Version] $Version)
  531     {
  532         # Simple comparison says the new version is newer
  533         return $true
  534     }
  535     elseif($simpleCompare)
  536     {
  537         # Simple comparison was done, but it was not newer
  538         return $false
  539     }
  540     elseif($NewVersion -notlike '*-*')
  541     {
  542         # Our current version is a pre-release but the new is not
  543         # make sure the new version is newer than the version part of the current version
  544         $versionOnly = ($Version -Split '\-')[0]
  545         if([Version]$NewVersion -ge [Version] $versionOnly)
  546         {
  547             return $true
  548         }
  549         else
  550         {
  551             return $false
  552         }
  553     }
  554     else
  555     {
  556         # Not sure, include it
  557         return $true
  558     }
  559 }
  560 
  561 ##############################
  562 #.SYNOPSIS
  563 # Get a RegEx based on a version that will match the major and minor
  564 #
  565 #.PARAMETER Version
  566 # The version to match
  567 #
  568 ##############################
  569 function Get-MatchingMajorMinorRegEx
  570 {
  571     param(
  572         [Parameter(Mandatory)]
  573         $Version
  574     )
  575 
  576     $parts = $Version -split '\.'
  577 
  578     $regEx = "^$($parts[0])\.$($parts[1])\..*"
  579     return $regEx
  580 }
  581 
  582 ##############################
  583 #.SYNOPSIS
  584 # Update the version number in code
  585 #
  586 #.PARAMETER NewReleaseTag
  587 # The new Release Tag
  588 #
  589 #.PARAMETER NextReleaseTag
  590 # The next Release Tag
  591 #
  592 #.PARAMETER Path
  593 # The path to the root of where you want to update
  594 #
  595 ##############################
  596 function Update-PsVersionInCode
  597 {
  598     param(
  599         [Parameter(Mandatory)]
  600         [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")]
  601         [String]
  602         $NewReleaseTag,
  603 
  604         [Parameter(Mandatory)]
  605         [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")]
  606         [String]
  607         $NextReleaseTag,
  608 
  609         [String]
  610         $Path = (Join-Path -Path $PSScriptRoot -ChildPath '..')
  611     )
  612 
  613     $metaDataPath = (Join-Path -Path $PSScriptRoot -ChildPath 'metadata.json')
  614     $metaData = Get-Content -Path $metaDataPath | ConvertFrom-Json
  615     $currentTag = $metaData.StableReleaseTag
  616 
  617     $currentVersion = $currentTag -replace '^v'
  618     $newVersion = $NewReleaseTag -replace '^v'
  619     $metaData.NextReleaseTag = $NextReleaseTag
  620     Set-Content -Path $metaDataPath -Encoding ascii -Force -Value ($metaData | ConvertTo-Json)
  621 
  622     Get-ChildItem -Path $Path -Recurse -File |
  623         Where-Object {$_.Extension -notin '.icns','.svg' -and $_.NAME -ne 'CHANGELOG.md' -and $_.DirectoryName -notmatch '[\\/]docs|demos[\\/]'} |
  624             Where-Object {$_ | Select-String -SimpleMatch $currentVersion -List} |
  625                 ForEach-Object {
  626                     $content = Get-Content -Path $_.FullName -Raw -ReadCount 0
  627                     $newContent = $content.Replace($currentVersion,$newVersion)
  628                     Set-Content -Path $_.FullName -Encoding ascii -Force -Value $newContent -NoNewline
  629                 }
  630 }
  631 
  632 Export-ModuleMember -Function Get-ChangeLog, Get-NewOfficalPackage, Update-PsVersionInCode