Patch-ModuleManifest.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
<#
    .Author
    Trevor Sullivan <trevor@trevorsullivan.net>

    .Description
    Publishes the PowerShell module to the PowerShell Gallery.
    This PowerShell script is invoked by AppVeyor after the "Deploy" phase has completed successfully.

    .Parameter Path
    The path to the module manifest file.

    .Parameter BuildNumber
    The unique build number, typically passed in from a Continuous Integration (CI) service, such as AppVeyor.
#>


function Patch-ModuleManifest {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)] 
        [string] $Path
      , [string] $BuildNumber
    )

    if (!$Path) {
        $Path = Get-ChildItem -Path $env:APPVEYOR_BUILD_FOLDER\* -Include *.psd1;
        if (!$Path) { throw 'Could not find a module manifest file'; }
    }

    $ManifestContent = Get-Content -Path $Path -Raw;
    $ManifestContent = $ManifestContent -replace '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')', ('${{ModuleVersion}}.{0}' -f $BuildNumber);
    Set-Content -Path $Path -Value $ManifestContent;

    $ManifestContent -match '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')';
    Get-Content -Path $Path;
    Write-Verbose -Message ('Module Version patched: ' + $Matches.ModuleVersion);
}