enosix.StepsUtil.psm1

#Region './Public/steps.ps1' 0
#Requires -Version 7

param(
    [parameter(Position=0,Mandatory=$false)][string]$stepsFile = ".completed-steps.json"
)

function Invoke-Step (
        [Parameter(Mandatory = $true)]
        [string]$stepName,
        
        [Parameter(Mandatory = $true)]
        [scriptblock]$action
) {
    
    $steps = @()
    if (Test-Path $stepsFile  -PathType Leaf) {
        $steps = Get-Content -Raw -Path $stepsFile | ConvertFrom-Json -NoEnumerate
    }

    if ($steps.Contains($stepName)) {
        Write-Host "Skipping step '$stepName'"
    } else {
        Write-Host "Running step '$stepName'"

        try {
            $result = & $action
        } catch {
            Write-Host $_.Exception
            Write-Host $_.ScriptStackTrace

            Write-Error $_.ScriptStackTrace
           
            throw $_.Exception
        }

        $steps += $stepName;
    }

    $steps | ConvertTo-Json -AsArray | Set-Content -Path $stepsFile 
    return $result;
}
#EndRegion './Public/steps.ps1' 41