Types/OpenPackage.Publisher/PowerShellGallery.ps1

<#
.SYNOPSIS
    Publishes Packages to PowerShell Gallery
.DESCRIPTION
    Publishes Packages to PowerShell Gallery, using Publish-Module
.LINK
    Publish-Module
#>

[CmdletBinding(PositionalBinding=$false,SupportsShouldProcess,ConfirmImpact='High')]
param(
# One or more packages to publish.
# Any non-package input will be ignored.
[Parameter(ValueFromPipeline)]
[Alias('Package')]
[PSObject[]]
$InputObject,

# The NuGetApiKey used to publish the packages.
[string]
$NuGetApiKey
)

# Gather all piped input
$allInput = @($input)

# If there was none, gather all non-piped input.
if (-not $allInput) {
    $allInput += $InputObject
}

# Get Publish-Module
$publishModuleCommand = $executionContext.SessionState.InvokeCommand.GetCommand('Publish-Module', 'Function,Cmdlet')
if (-not $publishModuleCommand) {
    # and throw if it does not exist.
    throw "No Publish-Module"
    return
}

# If there is no key, throw.
if (-not $NuGetApiKey) {
    throw "No Api Key"
}

# To publish we need these properties to exist in the manifest
$mustHaves = 'guid', 'description','author','companyname','copyright'

# Get our github event
$gitHubEvent = if ($env:GITHUB_EVENT_PATH) {
    [IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json
} else { $null }

# If there was no github event
if (-not $gitHubEvent) {
    # we will prompt
    Write-Warning "No github event found, prompting for confirmation"    
} else {
    # If the github event was not a pull request merge
    if (-not ($gitHubEvent.head_commit.message -match "Merge Pull Request #(?<PRNumber>\d+)") -and 
        (-not $gitHubEvent.psobject.properties['inputs'])) {
        # skip publishing.
        "::warning::Pull Request has not merged, skipping github release" | Out-Host
        return
    }    
}


# Go over each input
:nextInput foreach ($in in $allInput) {
    # If it it was not a package, continue
    if ($in -isnot [IO.Packaging.Package]) { continue }
    $package = $in        
    
    # If there is no package identifier, continue.
    if (-not $Package.Identifier) { Write-Error "No package identifier"; continue}

    # Check out manifest
    $PackageManifest = $package.PowerShellManifest
    
    # If we do not have a "must-have"
    $notHavingIt = $false
    foreach ($mustHave in $mustHaves) {
        if (-not $PackageManifest.$mustHave) {
            # tell them which ones
            Write-Error "Package $($package.identifier) has no $mustHave"
            $notHavingIt = $true        
        }
    }

    if ($notHavingIt) {
        continue
    }
    
    
    # If there is no github event, prompt for confirmation
    if ((-not $githubEvent) -and (-not $PSCmdlet.ShouldProcess(
        "Publish $($package.Identifier) $($package.Version)"
    ))) {
        continue
    }

    
    # Publish-Module requires the directory name to be correct
    # so we will install into a path
    $installPath = "./PowerShellGallery/$($package.Identifier)"
    
    # Install the module into the install path
    $null = $in | 
        Install-OpenPackage -DestinationPath $installPath -Force -Clear
    # Call publish module
    & $publishModuleCommand -Path $installPath -NugetApiKey $NuGetApiKey

    # Clean up
    Remove-Item -Recurse -Force $installPath

    # If the PowerShellGallery path exists, and is empty
    if ((Test-Path ./PowerShellGallery) -and 
        -not (Get-ChildItem ./PowerShellGallery)) {
        # clean it up.
        Remove-Item -Path ./PowerShellGallery
    }            
}