tasks/powershell.tasks.ps1

# PowerShellModulesToPublish must be an array of the following structure:
# @(
# @{
# ModulePath = "<path-to-psd1-file>"
# FunctionsToExport = @("*")
# CmdletsToExport = @()
# AliasesToExport = @()
# }
# )
$PowerShellModulesToPublish = @()

# Control flags
$SkipPesterTests = $false
$SkipPowerShellPublish = $false
$EnablePowerShellModuleForcePublish = $false

# Options
$PesterVersion = "4.10.1"
$PesterTestsDir = $null
$PesterOutputFormat = "NUnitXml"
$PesterOutputFilePath = "PesterTestResults.xml"
$PesterShowOptions = @("Summary","Fails")
$PowerShellRepository = "PSGallery"
$PowerShellGalleryApiKey = $null


# synopsis: Installs the required version of Pester
task InstallPester {

    [array]$existingModule = Get-Module -ListAvailable Pester
    $existingPesterVersions = $existingModule |
                                Select-Object -ExpandProperty Version |
                                ForEach-Object { $_.ToString() }
    if (!$existingModule -or $existingPesterVersions -notcontains $PesterVersion) {
        Install-Module Pester -RequiredVersion $PesterVersion -Force -Scope CurrentUser -SkipPublisherCheck
    }
    Get-Module Pester | Remove-Module
    Import-Module Pester -RequiredVersion $PesterVersion
}

# Synopsis: Runs all the available Pester tests
task RunPesterTests -If {!$SkipPesterTests -and $PesterTestsDir} InstallPester,{

    $results = Invoke-Pester -Path $PesterTestsDir `
                             -OutputFormat $PesterOutputFormat `
                             -OutputFile $PesterOutputFilePath `
                             -PassThru `
                             -Show $PesterShowOptions

    if ($results.FailedCount -gt 0) {
        throw ("{0} out of {1} tests failed - check previous logging for more details" -f $results.FailedCount, $results.TotalCount)
    }
}

# Synopsis: Pubishes PowerShell Modules to the PowerShell Gallery
task PublishPowerShellModules -If {!$SkipPowerShellPublish} Version,{

    # A nominal attempt to make a NuGet-compatible pre-release tag compatible with the
    # additional restrictions enforced by PowerShell Gallery
    $safePreReleaseTag = $env:GITVERSION_NuGetPreReleaseTag -replace "-",""
    
    foreach ($module in $PowerShellModulesToPublish) {

        Write-Build White "Publishing module: $($module.ModulePath)"

        # Ensure any required modules are installed
        $manifest = Get-Content -Raw $module.ModulePath | Invoke-Expression
        $manifest.RequiredModules |
            Where-Object { $_ } |
            ForEach-Object { Install-Module -Name $_ -Scope CurrentUser -Force -Repository PSGallery }

        Update-ModuleManifest -Path $module.ModulePath `
                              -ModuleVersion $script:GitVersion.MajorMinorPatch `
                              -Prerelease $safePreReleaseTag `
                              -FunctionsToExport $module.FunctionsToExport `
                              -CmdletsToExport $module.CmdletsToExport `
                              -AliasesToExport $module.AliasesToExport
        
        Publish-Module -Name $module.ModulePath `
                       -Repository $PowerShellRepository `
                       -NuGetApiKey $PowerShellGalleryApiKey `
                       -AllowPrerelease:$(![string]::IsNullOrEmpty($script:GitVersion.NuGetPreReleaseTag)) `
                       -Force:$EnablePowerShellModuleForcePublish `
                       -Verbose
        }
}