src/interface.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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
$ErrorActionPreference = "Stop" ."$PSScriptRoot\core.ps1" ."$PSScriptRoot\formatters.ps1" <# .SYNOPSIS Creates a new Requirement object .OUTPUTS The resulting Requirement .NOTES Dsc parameter set is unsupported due to cross-platform limitations #> function New-Requirement { [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] [OutputType([Requirement])] [CmdletBinding()] Param( # The unique identifier for the Requirement [Parameter(ParameterSetName = "Script")] [Parameter(ParameterSetName = "Dsc")] [string] $Namespace, # A description of the Requirement [Parameter(Mandatory, ParameterSetName = "Script")] [Parameter(Mandatory, ParameterSetName = "Dsc")] [string] $Describe, # The Test condition that determines if the Requirement is in its desired state [Parameter(ParameterSetName = "Script")] [scriptblock] $Test, # The Set condition that Sets the Requirement to its desired state [Parameter(ParameterSetName = "Script")] [scriptblock] $Set, # The list of Requirement Names that must be in desired state prior to this Requirement [Parameter(ParameterSetName = "Script")] [Parameter(ParameterSetName = "Dsc")] [ValidateNotNull()] [string[]] $DependsOn = @(), # The name of the DSC resource associated with the Requirement [Parameter(Mandatory, ParameterSetName = "Dsc")] [ValidateNotNullOrEmpty()] [string]$ResourceName, # The module containing the DSC resource [Parameter(Mandatory, ParameterSetName = "Dsc")] [ValidateNotNullOrEmpty()] [string]$ModuleName, # The properties passed through to the DSC resource [Parameter(Mandatory, ParameterSetName = "Dsc")] [ValidateNotNullOrEmpty()] [hashtable]$Property ) switch ($PSCmdlet.ParameterSetName) { "Script" { [Requirement]@{ Namespace = $Namespace Describe = $Describe Test = $Test Set = $Set DependsOn = $DependsOn } } "Dsc" { $dscParams = @{ Name = $ResourceName ModuleName = $ModuleName Property = $Property } [Requirement]@{ Namespace = $Namespace Describe = $Describe Test = { Invoke-DscResource -Method "Test" @dscParams }.GetNewClosure() Set = { Invoke-DscResource -Method "Set" @dscParams }.GetNewClosure() DependsOn = $DependsOn } } } } <# .SYNOPSIS Sets Requirements to their desired states .OUTPUTS The RequirementEvents logged from each stage of the Requirement lifecycle #> function Invoke-Requirement { [CmdletBinding()] [OutputType([RequirementEvent])] Param( # The Requirements to put in their desired state [Parameter(Mandatory, ValueFromPipeline)] [Requirement[]] $Requirement ) applyRequirements (sortRequirements $input) } <# .SYNOPSIS Tests whether a requirement is in its desired state #> function Test-Requirement { [CmdletBinding()] Param( # The Requirement to test its desired state [Parameter(Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [Requirement[]] $Requirement ) testRequirements (sortRequirements $input) } <# .SYNOPSIS Sets the requirement to its desired state #> function Set-Requirement { [CmdletBinding(SupportsShouldProcess)] Param( # The Requirement that sets if its in its desired state [Parameter(Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [Requirement] $Requirement ) if ($PSCmdlet.ShouldProcess($Requirement, "Set")) { &$Requirement.Set } } <# .SYNOPSIS Creates a Group of Requirements #> function New-RequirementGroup { [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] [CmdletBinding()] [Alias("Push-Namespace")] Param( # The namespace identifier [Parameter(Mandatory, Position = 0)] [Alias("Namespace")] [ValidateNotNullOrEmpty()] [string]$Name, # A scriptblock that writes Requirements to output when invoked [Parameter(Mandatory, Position = 1, ParameterSetName = "scriptblock")] [ValidateNotNullOrEmpty()] [scriptblock]$ScriptBlock, # The array of Requirements to add under the new Group [Parameter(Mandatory, Position = 1, ParameterSetName = "requirements")] [ValidateNotNullOrEmpty()] [Requirement[]]$Requirement ) if ($PSCmdlet.ParameterSetName -eq "scriptblock") { $Requirement = &$ScriptBlock } $Requirement ` | % { $r = $_.psobject.Copy() $r.Namespace = ($Name, $r.Namespace | ? { $_ }) -join $NamespaceDelimiter $r } } |