Public/Notifies.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
function Notifies {
    param(
        [parameter(Mandatory, Position = 0)]
        [scriptblock]$Script,

        [ValidateSet('Always', 'OnSuccess', 'OnFailure')]
        [string]$When = $script:Watchmen.Options.NotifierConditions.WatchmenOptions
    )

    begin {
        Write-Debug -Message "Entering: $($PSCmdlet.MyInvocation.MyCommand.Name)"
        Assert-InWatchmen -Command $PSCmdlet.MyInvocation.MyCommand.Name
        $script:Watchmen.InNotifies = $true

        if ($script:Watchmen.InConfig) {
            # If a specific notifier condition is provided, set that in options so subsequent notifiers will inherit the condition
            $script:Watchmen.Options.NotifierConditions.WatchmenOptions = $When

            Write-Verbose "Default Notifier condition for WatchmenOptions set to $When"

        } elseif ($script:Watchmen.InTest) {
            # If a specific notifier condition is provided, set that in options so subsequent notifiers within this WatchmenTest
            # block will inherit the condition
            $script:Watchmen.Options.NotifierConditions.WatchmenTest = $When

            Write-Verbose "Default Notifier condition for WatchmenTest set to $When"
        }
    }

    process {
        if ($script:Watchmen.InConfig) {        
            # Add all the notifiers to the Watchmen variable for later use
            Write-Debug -Message 'Adding notifiers to $script:Watchmen.Options.Notifiers'
            $notifiers = . $script
            foreach ($notifier in $notifiers) {
                $script:Watchmen.Options.Notifiers.($Notifier.Type) += $Notifier
            }
        } elseif ($script:Watchmen.InTest) {
            # Add locally defined notifiers to the test
            Write-Debug -Message 'Adding notifiers to $script:Watchmen.ThisTest.Notifiers'
            $notifiers = . $script
            foreach ($notifier in $notifiers) {
                $script:Watchmen.ThisTest.Notifiers.($Notifier.Type) += $Notifier
            }
        }
    }

    end {
        $script:Watchmen.InNotifies = $false
        Write-Debug -Message "Exiting: $($PSCmdlet.MyInvocation.MyCommand.Name)"
    }
}