scripts/Invoke-pwshake.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
function Invoke-pwshake { [CmdletBinding()] param ( [Alias("Path","File","ConfigFile")] [Parameter(Position = 0, Mandatory = $false, ValueFromPipeline = $true)] [string]$ConfigPath = (Resolve-Path "${PWD}\pwshake.yaml"), [Parameter(Position = 1, Mandatory = $false)] [Alias("RunLists", "Roles")] [object[]]$Tasks = @(), [Alias("Attributes")] [Parameter(Position = 2, Mandatory = $false)] [object]$MetaData = $null, [Alias("WhatIf", "Noop")] [switch]$DryRun, [Alias("LogLevel")] [ValidateSet('Error', 'Warning', 'Minimal', 'Information', 'Verbose', 'Debug', 'Normal', 'Default', 'Silent', 'Quiet')] [Parameter(Mandatory = $false)] [string]$Verbosity = 'Default' ) process { $arguments = @{ ConfigPath = $ConfigPath MetaData = $MetaData Verbosity = $Verbosity DryRun = [bool]$DryRun } $context = @{json_sb=(New-Object 'Text.StringBuilder');thrown=$false} $context.Remove('invocations') ${global:pwshake-context}.invocations.Push(@{ arguments = ($arguments + @{Tasks = $Tasks; WorkDir = "$(Get-Location)" }) # early loading (before stages invocation) is required for base settings of logging etc. config = (Load-Config @arguments) context = $context }) try { try { $caption = "PWSHAKE arguments:" $caption | f-log-verb "$(ConvertTo-Yaml (Peek-Invocation).arguments)" | f-log-verb foreach ($stage in ${global:pwshake-context}.stages) { "Invoke-pwshake:`$stage`:{$stage}" | f-log-dbg (Peek-Invocation).config = Invoke-Expression "(Peek-Config) | $stage" } "Invoke-pwshake:stagesInvoked:`$config:`n$(ConvertTo-Yaml (Peek-Config))" | f-log-dbg $caption = "PWSHAKE config:" $caption | f-log-verb "$(ConvertTo-Yaml (Peek-Config))" | f-log-verb } finally { if ((Peek-Config).attributes.pwshake_log_to_json) { $context.json_sb.ToString() | f-json | Add-Content -Path "$((Peek-Config).attributes.pwshake_log_path).json" -Encoding UTF8 } } $arranged_tasks = (Peek-Config).invoke_tasks | Arrange-Tasks "Invoke-pwshake:`$arranged_tasks:`n$(ConvertTo-Yaml $arranged_tasks)" | f-log-dbg Push-Location (Peek-Config).attributes.work_dir $arranged_tasks | Invoke-Task } catch { if (-not (Peek-Context).caught) { # if it was not thrown in execution context, it should be logged $_ | f-log-err } throw $_ } finally { Pop-Location if ((Peek-Config).attributes.pwshake_log_to_json) { (Peek-Context).json_sb.ToString() | f-json | Add-Content -Path "$((Peek-Config).attributes.pwshake_log_path).json" -Encoding UTF8 } "Invoke-pwshake:finally:`${global:pwshake-context}.invocations.Count:{$(${global:pwshake-context}.invocations.Count)}" | f-log-dbg ${global:pwshake-context}.invocations.Pop() | Out-Null } } } function Peek-Invocation { return ${global:pwshake-context}.invocations.Peek() } function Peek-Context{ return (Peek-Invocation).context } function Peek-Config { return (Peek-Invocation).config } function Peek-Verbosity { return [PWSHAKE.VerbosityLevel](Peek-Invocation).config.attributes.pwshake_verbosity } |