Repository navigation
Expand file tree
/
Copy pathFormat-PSMDParameter.ps1
More file actions
261 lines (215 loc) · 7.41 KB
/
Copy pathFormat-PSMDParameter.ps1
File metadata and controls
261 lines (215 loc) · 7.41 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function Format-PSMDParameter
{
<#
.SYNOPSIS
Formats the parameter block on commands.
.DESCRIPTION
Formats the parameter block on commands.
This function will convert legacy functions that have their parameters straight behind their command name.
It also fixes missing CmdletBinding attributes.
Nested commands will also be affected.
.PARAMETER FullName
The file to process
.PARAMETER DisableCache
By default, this command caches the results of its execution in the PSFramework result cache.
This information can then be retrieved for the last command to do so by running Get-PSFResultCache.
Setting this switch disables the caching of data in the cache.
.PARAMETER Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
.PARAMETER WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
.EXAMPLE
PS C:\> Get-ChildItem .\functions\*\*.ps1 | Set-PSMDCmdletBinding
Updates all commands in the module to have a cmdletbinding attribute.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]
$FullName,
[switch]
$DisableCache
)
begin
{
#region Utility functions
function Invoke-AstWalk
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
[CmdletBinding()]
param (
$Ast,
[string[]]
$Command,
[string[]]
$Name,
[string]
$NewName,
[bool]
$IsCommand,
[bool]
$NoAlias
)
#Write-PSFMessage -Level Host -Message "Processing $($Ast.Extent.StartLineNumber) | $($Ast.Extent.File) | $($Ast.GetType().FullName)"
$typeName = $Ast.GetType().FullName
switch ($typeName)
{
"System.Management.Automation.Language.FunctionDefinitionAst"
{
#region Has no param block
if ($null -eq $Ast.Body.ParamBlock)
{
$baseIndent = $Ast.Extent.Text.Split("`n")[0] -replace "^(\s{0,}).*", '$1'
$indent = $baseIndent + "`t"
# Kill explicit parameter section behind name
$startIndex = "function ".Length + $Ast.Name.Length
$endIndex = $Ast.Extent.Text.IndexOf("{")
Add-FileReplacement -Path $ast.Extent.File -Start ($Ast.Extent.StartOffset + $startIndex) -Length ($endIndex - $startIndex) -NewContent "`n"
$baseParam = @"
$($indent)[CmdletBinding()]
$($indent)param (
{0}
$($indent))
"@
$parameters = @()
$paramIndent = $indent + "`t"
foreach ($parameter in $Ast.Parameters)
{
$defaultValue = ""
if ($parameter.DefaultValue) { $defaultValue = " = $($parameter.DefaultValue.Extent.Text)" }
$values = @()
foreach ($attribute in $parameter.Attributes)
{
$values += "$($paramIndent)$($attribute.Extent.Text)"
}
$values += "$($paramIndent)$($parameter.Name.Extent.Text)$($defaultValue)"
$parameters += $values -join "`n"
}
$baseParam = $baseParam -f ($parameters -join ",`n`n")
Add-FileReplacement -Path $ast.Extent.File -Start $Ast.Body.Extent.StartOffset -Length 1 -NewContent "{`n$($baseParam)"
}
#endregion Has no param block
#region Has a param block, but no cmdletbinding
if (($null -ne $Ast.Body.ParamBlock) -and (-not ($Ast.Body.ParamBlock.Attributes | Where-Object TypeName -Like "CmdletBinding")))
{
$text = [System.IO.File]::ReadAllText($Ast.Extent.File)
$index = $Ast.Body.ParamBlock.Extent.StartOffset
while (($index -gt 0) -and ($text.Substring($index, 1) -ne "`n")) { $index = $index - 1 }
$indentIndex = $index + 1
$indent = $text.Substring($indentIndex, ($Ast.Body.ParamBlock.Extent.StartOffset - $indentIndex))
Add-FileReplacement -Path $Ast.Body.ParamBlock.Extent.File -Start $indentIndex -Length ($Ast.Body.ParamBlock.Extent.StartOffset - $indentIndex) -NewContent "$($indent)[CmdletBinding()]`n$($indent)"
}
#endregion Has a param block, but no cmdletbinding
Invoke-AstWalk -Ast $Ast.Body -Command $Command -Name $Name -NewName $NewName -IsCommand $false
}
default
{
foreach ($property in $Ast.PSObject.Properties)
{
if ($property.Name -eq "Parent") { continue }
if ($null -eq $property.Value) { continue }
if (Get-Member -InputObject $property.Value -Name GetEnumerator -MemberType Method)
{
foreach ($item in $property.Value)
{
if ($item.PSObject.TypeNames -contains "System.Management.Automation.Language.Ast")
{
Invoke-AstWalk -Ast $item -Command $Command -Name $Name -NewName $NewName -IsCommand $IsCommand
}
}
continue
}
if ($property.Value.PSObject.TypeNames -contains "System.Management.Automation.Language.Ast")
{
Invoke-AstWalk -Ast $property.Value -Command $Command -Name $Name -NewName $NewName -IsCommand $IsCommand
}
}
}
}
}
function Add-FileReplacement
{
[CmdletBinding()]
param (
[string]
$Path,
[int]
$Start,
[int]
$Length,
[string]
$NewContent
)
Write-PSFMessage -Level Verbose -Message "Change Submitted: $Path | $Start | $Length | $NewContent" -Tag 'update', 'change', 'file'
if (-not $globalFunctionHash.ContainsKey($Path))
{
$globalFunctionHash[$Path] = @()
}
$globalFunctionHash[$Path] += New-Object PSObject -Property @{
Content = $NewContent
Start = $Start
Length = $Length
}
}
function Apply-FileReplacement
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
[CmdletBinding()]
param (
)
foreach ($key in $globalFunctionHash.Keys)
{
$value = $globalFunctionHash[$key] | Sort-Object Start
$content = [System.IO.File]::ReadAllText($key)
$newString = ""
$currentIndex = 0
foreach ($item in $value)
{
$newString += $content.SubString($currentIndex, ($item.Start - $currentIndex))
$newString += $item.Content
$currentIndex = $item.Start + $item.Length
}
$newString += $content.SubString($currentIndex)
[System.IO.File]::WriteAllText($key, $newString)
#$newString
}
}
function Write-Issue
{
[CmdletBinding()]
param (
$Extent,
$Data,
[string]
$Type
)
New-Object PSObject -Property @{
Type = $Type
Data = $Data
File = $Extent.File
StartLine = $Extent.StartLineNumber
Text = $Extent.Text
}
}
#endregion Utility functions
}
process
{
foreach ($path in $FullName)
{
$globalFunctionHash = @{ }
$tokens = $null
$parsingError = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$tokens, [ref]$parsingError)
Write-PSFMessage -Level VeryVerbose -Message "Ensuring Cmdletbinding for all functions in $path" -Tag 'start' -Target $Name
$issues += Invoke-AstWalk -Ast $ast -Command $Command -Name $Name -NewName $NewName -IsCommand $false
Set-PSFResultCache -InputObject $issues -DisableCache $DisableCache
if ($PSCmdlet.ShouldProcess($path, "Set CmdletBinding attribute"))
{
Apply-FileReplacement
}
$issues
}
}
}