Public/Publish-CCRelease.ps1

function Publish-CCRelease {
    <#
    .SYNOPSIS
        Makes a release: writes the version into the manifests and the changelog, commits, tags and pushes. With -WhatIf, returns the plan and the command to run.
    .DESCRIPTION
        The git tag is the version, and this is the one place it is typed. Name the part to increment
        and the version is counted from the latest tag, so it is not typed at all. Give -Version when
        the number is a decision, or for a first release, which has no tag to count from.

        In order:

          1. Runs the readiness checks (Test-CCReleaseReady) and stops on a failure, before anything
             is written.
          2. Refuses a version that is not higher than every existing version tag.
          3. Writes the version into every manifest that states one, and renames '## [Unreleased]' in
             CHANGELOG.md to '## [X.Y.Z] - date', leaving a fresh Unreleased heading above it.
          4. Commits exactly those files as 'release: vX.Y.Z', and tags the commit with an annotated tag.
          5. Pushes the commit and the tag in one atomic push. The tag starts the release workflow,
             which checks the tag again and publishes.

        With -WhatIf nothing is written or pushed. The plan comes back as an object, and its Command
        property is the exact line to run to make that release, with the version pinned so that what
        was reviewed is what runs. An agent prepares a release this way and a person runs the command.

        There is no prompt. A published version is permanent: read the plan first.
    .PARAMETER Part
        Increment this part of the latest version tag: Major, Minor or Patch.
    .PARAMETER Version
        Release exactly this version. Needed for a first release.
    .PARAMETER Message
        A summary for the tag's annotation, which becomes 'vX.Y.Z - <Message>'.
    .PARAMETER NoFetch
        Do not fetch before checking. Compare with what was last known of the remote.
    .EXAMPLE
        Publish-CCRelease -Part Minor -WhatIf
    .EXAMPLE
        Publish-CCRelease -Path . -Version 1.4.0 -Message 'release gate (D-028)'
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', DefaultParameterSetName = 'Part')]
    [OutputType([pscustomobject])]
    param(
        [Parameter(Position = 0)]
        [string]$Path = '.',

        [Parameter(Mandatory, ParameterSetName = 'Part')]
        [ValidateSet('Major', 'Minor', 'Patch')]
        [string]$Part,

        [Parameter(Mandatory, ParameterSetName = 'Version')]
        [string]$Version,

        [string]$Message,

        [switch]$NoFetch
    )

    $full = (Resolve-Path -LiteralPath $Path).Path
    $planner = [CodeCompass.Core.Releases.ReleasePlanner]::new()

    $partValue = $null
    if ($Part) { $partValue = [CodeCompass.Core.Versioning.VersionPart]::$Part }
    $target = $null
    if ($Version) { $target = [CodeCompass.Core.Versioning.SemanticVersion]::Parse($Version) }

    $plan = $planner.Plan($full, $partValue, $target, -not $NoFetch)

    $command = $null
    if ($plan.CanProceed) {
        $command = "Publish-CCRelease -Path '$($full -replace "'", "''")' -Version $($plan.Version)"
        if ($Message) { $command += " -Message '$($Message -replace "'", "''")'" }
    }

    $result = [ordered]@{
        Version   = if ($plan.Version) { $plan.Version.ToString() } else { $null }
        Previous  = if ($plan.Current) { $plan.Current.ToString() } else { $null }
        Tag       = $plan.Tag
        Branch    = $plan.Branch
        Remote    = $plan.Remote
        Files     = @($plan.Files)
        Blockers  = @($plan.Blockers)
        Readiness = @($plan.Readiness | ConvertTo-CCCheckRow)
        Released  = $false
        Command   = $command
    }

    if (-not $plan.CanProceed) {
        # Under -WhatIf the blockers are the answer. Otherwise they are the reason nothing happened.
        if ($WhatIfPreference) { return [pscustomobject]$result }
        throw "Not released. Nothing was written.`n - $(@($plan.Blockers) -join "`n - ")"
    }

    $tag = $plan.Tag
    $annotation = if ($Message) { "$tag - $Message" } else { $tag }

    if (-not $PSCmdlet.ShouldProcess($full, "Release $tag from $($plan.Branch): write $(@($plan.Files) -join ', '), commit, tag, and push to $($plan.Remote)")) {
        return [pscustomobject]$result
    }

    $written = @($planner.Apply($plan, [datetime]::Now))

    $step = Invoke-CCGit -Path $full -Arguments (@('add', '--') + $written)
    if ($step.Ok) { $step = Invoke-CCGit -Path $full -Arguments @('commit', '-q', '-m', "release: $tag") }
    if (-not $step.Ok) {
        Invoke-CCGit -Path $full -Arguments (@('reset', '-q', '--') + $written) | Out-Null
        Invoke-CCGit -Path $full -Arguments (@('checkout', '--') + $written) | Out-Null
        throw "Not released. The release commit could not be made, and the files were put back.`n$($step.Output)"
    }

    $step = Invoke-CCGit -Path $full -Arguments @('tag', '-a', $tag, '-m', $annotation)
    if (-not $step.Ok) {
        # The tree was clean before the commit, so taking the commit back loses nothing.
        Invoke-CCGit -Path $full -Arguments @('reset', '-q', '--hard', 'HEAD~1') | Out-Null
        throw "Not released. The tag $tag could not be made, and the release commit was taken back.`n$($step.Output)"
    }

    $push = "git push --atomic $($plan.Remote) $($plan.Branch) $tag"
    $step = Invoke-CCGit -Path $full -Arguments @('push', '--atomic', $plan.Remote, $plan.Branch, $tag)
    if (-not $step.Ok) {
        throw @"
Not released. The release commit and the tag $tag exist here and were not pushed.
$($step.Output)

To push them once the cause is fixed:
    $push
To take the release back instead:
    git tag -d $tag
    git reset --hard HEAD~1
"@

    }

    $result.Files = $written
    $result.Released = $true
    [pscustomobject]$result
}