|
| 1 | +function New-PipeScript { |
| 2 | + <# |
| 3 | + .Synopsis |
| 4 | + Creates new PipeScript. |
| 5 | + .Description |
| 6 | + Creates new PipeScript and PowerShell ScriptBlocks. |
| 7 | + .EXAMPLE |
| 8 | + New-PipeScript -Parameter @{a='b'} |
| 9 | + #> |
| 10 | + [Alias('New-ScriptBlock')] |
| 11 | + param( |
| 12 | + # Defines one or more parameters for a ScriptBlock. |
| 13 | + # Parameters can be defined in a few ways: |
| 14 | + # * As a ```[Collections.Dictionary]``` of Parameters |
| 15 | + # * As the ```[string]``` name of an untyped parameter. |
| 16 | + # * As an ```[Object[]]```. |
| 17 | + # * As a ```[ScriptBlock]``` |
| 18 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 19 | + [ValidateScript({ |
| 20 | + $validTypeList = [System.Collections.IDictionary],[System.String],[System.Object[]],[System.Management.Automation.ScriptBlock] |
| 21 | + $thisType = $_.GetType() |
| 22 | + $IsTypeOk = |
| 23 | + $(@( foreach ($validType in $validTypeList) { |
| 24 | + if ($_ -as $validType) { |
| 25 | + $true;break |
| 26 | + } |
| 27 | + })) |
| 28 | + if (-not $isTypeOk) { |
| 29 | + throw "Unexpected type '$(@($thisType)[0])'. Must be 'System.Collections.IDictionary','string','System.Object[]','scriptblock'." |
| 30 | + } |
| 31 | + return $true |
| 32 | + })] |
| 33 | + |
| 34 | + $Parameter, |
| 35 | + |
| 36 | + # The dynamic parameter block. |
| 37 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 38 | + [ValidateScript({ |
| 39 | + if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) { |
| 40 | + throw "ScriptBlock should not have any named blocks" |
| 41 | + } |
| 42 | + return $true |
| 43 | + })] |
| 44 | + [ValidateScript({ |
| 45 | + if ($_.Ast.ParamBlock.Parameters.Count) { |
| 46 | + throw "ScriptBlock should not have parameters" |
| 47 | + } |
| 48 | + return $true |
| 49 | + })] |
| 50 | + [Alias('DynamicParameterBlock')] |
| 51 | + [ScriptBlock] |
| 52 | + $DynamicParameter, |
| 53 | + # The begin block. |
| 54 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 55 | + [ValidateScript({ |
| 56 | + if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) { |
| 57 | + throw "ScriptBlock should not have any named blocks" |
| 58 | + } |
| 59 | + return $true |
| 60 | + })] |
| 61 | + [ValidateScript({ |
| 62 | + if ($_.Ast.ParamBlock.Parameters.Count) { |
| 63 | + throw "ScriptBlock should not have parameters" |
| 64 | + } |
| 65 | + return $true |
| 66 | + })] |
| 67 | + [Alias('BeginBlock')] |
| 68 | + [ScriptBlock] |
| 69 | + $Begin, |
| 70 | + # The process block. |
| 71 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 72 | + [ValidateScript({ |
| 73 | + if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) { |
| 74 | + throw "ScriptBlock should not have any named blocks" |
| 75 | + } |
| 76 | + return $true |
| 77 | + })] |
| 78 | + [ValidateScript({ |
| 79 | + if ($_.Ast.ParamBlock.Parameters.Count) { |
| 80 | + throw "ScriptBlock should not have parameters" |
| 81 | + } |
| 82 | + return $true |
| 83 | + })] |
| 84 | + [Alias('ProcessBlock')] |
| 85 | + [ScriptBlock] |
| 86 | + $Process, |
| 87 | + # The end block. |
| 88 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 89 | + [ValidateScript({ |
| 90 | + if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) { |
| 91 | + throw "ScriptBlock should not have any named blocks" |
| 92 | + } |
| 93 | + return $true |
| 94 | + })] |
| 95 | + [ValidateScript({ |
| 96 | + if ($_.Ast.ParamBlock.Parameters.Count) { |
| 97 | + throw "ScriptBlock should not have parameters" |
| 98 | + } |
| 99 | + return $true |
| 100 | + })] |
| 101 | + [Alias('EndBlock')] |
| 102 | + [ScriptBlock] |
| 103 | + $End, |
| 104 | + |
| 105 | + # The script header. |
| 106 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 107 | + [string] |
| 108 | + $Header |
| 109 | + ) |
| 110 | + begin { |
| 111 | + $ParametersToCreate = [Ordered]@{} |
| 112 | + $allDynamicParameters = @() |
| 113 | + $allBeginBlocks = @() |
| 114 | + $allEndBlocks = @() |
| 115 | + $allProcessBlocks = @() |
| 116 | + $allHeaders = @() |
| 117 | + } |
| 118 | + process { |
| 119 | + if ($parameter) { |
| 120 | + # The -Parameter can be a dictionary of parameters. |
| 121 | + if ($Parameter -is [Collections.IDictionary]) { |
| 122 | + $parameterType = '' |
| 123 | + # If it is, walk thur each parameter in the dictionary |
| 124 | + foreach ($EachParameter in $Parameter.GetEnumerator()) { |
| 125 | + # Continue past any parameters we already have |
| 126 | + if ($ParametersToCreate.Contains($EachParameter.Key)) { |
| 127 | + continue |
| 128 | + } |
| 129 | + # If the parameter is a string and the value is not a variable |
| 130 | + if ($EachParameter.Value -is [string] -and $EachParameter.Value -notlike '*$*') { |
| 131 | + $parameterName = $EachParameter.Key |
| 132 | + $ParametersToCreate[$EachParameter.Key] = |
| 133 | + @( |
| 134 | + $parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]" |
| 135 | + $parameterType |
| 136 | + '$' + $parameterName |
| 137 | + ) -ne '' |
| 138 | + } |
| 139 | + # If the value is a string and the value contains variables |
| 140 | + elseif ($EachParameter.Value -is [string]) { |
| 141 | + # embed it directly. |
| 142 | + $ParametersToCreate[$EachParameter.Key] = $EachParameter.Value |
| 143 | + } |
| 144 | + # If the value is a ScriptBlock |
| 145 | + elseif ($EachParameter.Value -is [ScriptBlock]) { |
| 146 | + # Embed it |
| 147 | + $ParametersToCreate[$EachParameter.Key] = |
| 148 | + # If there was a param block on the script block |
| 149 | + if ($EachParameter.Value.Ast.ParamBlock) { |
| 150 | + # embed the parameter block (except for the param keyword) |
| 151 | + $EachParameter.Value.Ast.ParamBlock.Extent.ToString() -replace |
| 152 | + '^[\s\r\n]param(' -replace ')[\s\r\n]$' |
| 153 | + } else { |
| 154 | + # Otherwise |
| 155 | + $EachParameter.Value.ToString() -replace |
| 156 | + "\`$$($eachParameter.Key)[\s\r\n]$" -replace # Replace any trailing variables |
| 157 | + 'param()[\s\r\n]{0,}$' # then replace any empty param blocks. |
| 158 | + } |
| 159 | + } |
| 160 | + elseif ($EachParameter.Value -is [Object[]]) { |
| 161 | + $ParametersToCreate[$EachParameter.Key] = |
| 162 | + $EachParameter.Value -join [Environment]::Newline |
| 163 | + } |
| 164 | + } |
| 165 | + } elseif ($Parameter -is [string]) { |
| 166 | + $ParametersToCreate[$Parameter] = @( |
| 167 | + "[Parameter(ValueFromPipelineByPropertyName)]" |
| 168 | + "`$$Parameter" |
| 169 | + ) |
| 170 | + } elseif ($Parameter -is [Object[]]) { |
| 171 | + $currentParam = @() |
| 172 | + $currentParamName = '' |
| 173 | + foreach ($EachParameter in $Parameter) { |
| 174 | + if ($EachParameter -is [string] -and -not $EachParameter.Contains(' ')) { |
| 175 | + if ($currentParam) { |
| 176 | + $ParametersToCreate[$currentParamName] = $currentParam |
| 177 | + $currentParam = @() |
| 178 | + $currentParamName = '' |
| 179 | + } |
| 180 | + $currentParam += "`$$EachParameter" |
| 181 | + $currentParamName = $EachParameter |
| 182 | + } elseif ($EachParameter -is [string] -and $EachParameter.Contains(' ')) { |
| 183 | + $currentParam = @( |
| 184 | + if ($EachParameter.Contains("`n")) { |
| 185 | + "<#" + [Environment]::newLine + $EachParameter + [Environment]::newLine + '#>' |
| 186 | + } else { |
| 187 | + "# $EachParameter" |
| 188 | + } |
| 189 | + ) + $currentParam |
| 190 | + } elseif ($EachParameter -is [type]) { |
| 191 | + $currentParam += "[$($EachParameter.Fullname)]" |
| 192 | + } |
| 193 | + } |
| 194 | + if ($currentParamName) { |
| 195 | + $ParametersToCreate[$currentParamName] = $currentParam |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + if ($header) { |
| 200 | + $allHeaders += $Header |
| 201 | + } |
| 202 | + if ($DynamicParameter) { |
| 203 | + $allDynamicParameters += $DynamicParameter |
| 204 | + } |
| 205 | + if ($Begin) { |
| 206 | + $allBeginBlocks += $begin |
| 207 | + } |
| 208 | + if ($process) { |
| 209 | + $allProcessBlocks += $process |
| 210 | + } |
| 211 | + if ($end) { |
| 212 | + $allEndBlocks += $end |
| 213 | + } |
| 214 | + |
| 215 | + } |
| 216 | + end { |
| 217 | + $newParamBlock = |
| 218 | + "param(" + [Environment]::newLine + |
| 219 | + $(@(foreach ($toCreate in $ParametersToCreate.GetEnumerator()) { |
| 220 | + $toCreate.Value -join [Environment]::NewLine |
| 221 | + }) -join (',' + [Environment]::NewLine)) + |
| 222 | + [Environment]::NewLine + |
| 223 | + ')' |
| 224 | + $createdScriptBlock = [scriptblock]::Create(" |
| 225 | +$($allHeaders -join [Environment]::Newline) |
| 226 | +$newParamBlock |
| 227 | +$(if ($allDynamicParameters) { |
| 228 | + @(@("dynamicParam {") + $allDynamicParameters + '}') -join [Environment]::Newline |
| 229 | +}) |
| 230 | +$(if ($allBeginBlocks) { |
| 231 | + @(@("begin {") + $allBeginBlocks + '}') -join [Environment]::Newline |
| 232 | +}) |
| 233 | +$(if ($allProcessBlocks) { |
| 234 | + @(@("process {") + $allProcessBlocks + '}') -join [Environment]::Newline |
| 235 | +}) |
| 236 | +$(if ($allEndBlocks -and -not $allBeginBlocks -and -not $allProcessBlocks) { |
| 237 | + $allEndBlocks -join [Environment]::Newline |
| 238 | +} elseif ($allEndBlocks) { |
| 239 | + @(@("end {") + $allEndBlocks + '}') -join [Environment]::Newline |
| 240 | +}) |
| 241 | +") |
| 242 | + $createdScriptBlock |
| 243 | + } |
| 244 | +} |
| 245 | + |
0 commit comments