forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSScriptAnalyzer.psm1
57 lines (44 loc) · 2.33 KB
/
PSScriptAnalyzer.psm1
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
#
# Script module for module 'PSScriptAnalyzer'
#
Set-StrictMode -Version Latest
# Set up some helper variables to make it easier to work with the module
$PSModule = $ExecutionContext.SessionState.Module
$PSModuleRoot = $PSModule.ModuleBase
# Import the appropriate nested binary module based on the current PowerShell version
$binaryModuleRoot = $PSModuleRoot
if (($PSVersionTable.Keys -contains "PSEdition") -and ($PSVersionTable.PSEdition -ne 'Desktop')) {
$binaryModuleRoot = Join-Path -Path $PSModuleRoot -ChildPath 'coreclr'
}
else {
if ($PSVersionTable.PSVersion -lt [Version]'5.0') {
$binaryModuleRoot = Join-Path -Path $PSModuleRoot -ChildPath 'PSv3'
}
}
$binaryModulePath = Join-Path -Path $binaryModuleRoot -ChildPath 'Microsoft.Windows.PowerShell.ScriptAnalyzer.dll'
$binaryModule = Import-Module -Name $binaryModulePath -PassThru
# When the module is unloaded, remove the nested binary module that was loaded with it
$PSModule.OnRemove = {
Remove-Module -ModuleInfo $binaryModule
}
if (Get-Command Register-ArgumentCompleter -ErrorAction Ignore) {
$settingPresetCompleter = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParmeter)
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings]::GetSettingPresets() | `
Where-Object {$_ -like "$wordToComplete*"} | `
ForEach-Object { New-Object System.Management.Automation.CompletionResult $_ }
}
@('Invoke-ScriptAnalyzer', 'Invoke-Formatter') | ForEach-Object {
Register-ArgumentCompleter -CommandName $_ `
-ParameterName 'Settings' `
-ScriptBlock $settingPresetCompleter
}
Function RuleNameCompleter {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParmeter)
Get-ScriptAnalyzerRule *$wordToComplete* | `
ForEach-Object { New-Object System.Management.Automation.CompletionResult $_.RuleName }
}
Register-ArgumentCompleter -CommandName 'Invoke-ScriptAnalyzer' -ParameterName 'IncludeRule' -ScriptBlock $Function:RuleNameCompleter
Register-ArgumentCompleter -CommandName 'Invoke-ScriptAnalyzer' -ParameterName 'ExcludeRule' -ScriptBlock $Function:RuleNameCompleter
Register-ArgumentCompleter -CommandName 'Get-ScriptAnalyzerRule' -ParameterName 'Name' -ScriptBlock $Function:RuleNameCompleter
}