Repository navigation
Expand file tree
/
Copy pathSet-PSMDCmdletBinding.ps1
More file actions
224 lines (182 loc) · 6.04 KB
/
Copy pathSet-PSMDCmdletBinding.ps1
File metadata and controls
224 lines (182 loc) · 6.04 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
function Set-PSMDCmdletBinding
{
<#
.SYNOPSIS
Adds cmdletbinding attributes in bulk
.DESCRIPTION
Searches the specified file(s) for functions that ...
- Do not have a cmdlet binding attribute
- Do have a param block
and inserts a cmdletbinding attribute for them.
Will not change files where functions already have this attribute. Will also update internal functions.
.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.
#>
[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 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
}
}
}