-
Notifications
You must be signed in to change notification settings - Fork 4
/
ScriptCop.psm1
169 lines (141 loc) · 5.86 KB
/
ScriptCop.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
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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ModuleManifestQuality*", "", Justification="FileList is unimportant")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ForSlowScript", "", Justification="Performance is not a priority for this module")]
param()
Set-StrictMode -Off
#region Script Cop Rule Commands
. $psScriptRoot\Get-ScriptCopRule.ps1
. $psScriptRoot\Register-ScriptCopRule.ps1
. $psScriptRoot\Test-ScriptCopRule.ps1
. $psScriptRoot\Unregister-ScriptCopRule.ps1
#endregion Script Cop Rule Commands
Export-ModuleMember -Function Get-ScriptCopRule, Register-ScriptCopRule, Unregister-ScriptCopRule
#region Script Cop Fixer Commands
. $psScriptRoot\Get-ScriptCopFixer.ps1
. $psScriptRoot\Register-ScriptCopFixer.ps1
. $psScriptRoot\Test-ScriptCopFixer.ps1
. $psScriptRoot\Unregister-ScriptCopFixer.ps1
#endregion Script Cop Fixer Commands
Export-ModuleMember -Function Get-ScriptCopFixer, Register-ScriptCopFixer, Unregister-ScriptCopFixer
#region Patrol Functions
. $psScriptRoot\Get-ScriptCopPatrol.ps1
. $psScriptRoot\Register-ScriptCopPatrol.ps1
. $psScriptRoot\Save-ScriptCopPatrol.ps1
. $psScriptRoot\Unregister-ScriptCopPatrol.ps1
#endregion
Export-ModuleMember -Function Get-ScriptCopPatrol, Register-ScriptCopPatrol, Unregister-ScriptCopPatrol
#region General Purpose Functions
. $psScriptRoot\Get-FunctionFromScript.ps1
#endregion
#region Command Coverage
. $psScriptRoot\Disable-CommandCoverage.ps1
. $psScriptRoot\Enable-CommandCoverage.ps1
. $psScriptRoot\Get-CommandCoverage.ps1
#endregion
#region Major exported commands
. $psScriptRoot\Test-Command.ps1
. $psScriptRoot\Repair-Command.ps1
. $psScriptRoot\Show-ScriptCoverage.ps1
. $psScriptRoot\Test-Module.ps1
Export-ModuleMember -Function Test-Command, Test-Module,Repair-Command, Show-ScriptCoverage, Enable-CommandCoverage, Disable-CommandCoverage,Get-CommandCoverage
#endregion
#region Import Rules From Rules Directory
$RuleFiles = [IO.Directory]::GetFiles("$(Join-Path $psScriptRoot Rules)")
$RuleScripts =
foreach ($_ in $RuleFiles) {
if (($_ -as [IO.FileInfo]).Extension -eq '.ps1') {
$ExecutionContext.SessionState.InvokeCommand.GetCommand($_, 'ExternalScript')
}
}
foreach ($ruleScript in $RuleScripts) {
Test-ScriptCopRule -ErrorAction SilentlyContinue -ErrorVariable RuleImportError -CommandInfo $ruleScript
@(if ($RuleImportError) {
# Ok, see if it contains functions
$functionOnly = Get-FunctionFromScript -ScriptBlock ([ScriptBlock]::Create($ruleScript.ScriptContents))
. $ruleScript
$cmds = @()
foreach ($f in $functionOnly) {
#. ([ScriptBlock]::Create($f))
$matched = $f -match "function ((\w+-\w+)|(\w+))"
if ($matched -and $matches[1]) {
$foundCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand("$($matches[1])", 'Function')
if ($foundCmd) {
$cmds += $foundCmd
}
#$cmds+=Get-Command $matches[1] -ErrorAction SilentlyContinue
}
}
foreach ($cmd in $cmds) {
Test-ScriptCopRule -ErrorAction SilentlyContinue -ErrorVariable RuleImportError2 -CommandInfo $cmd
if ($ruleImportError2) {
Write-Verbose ($RuleImportError2 |Out-String)
} else {
$cmd
}
}
if (-not $RuleImportError2) {
Write-Debug ($RuleImportError |Out-String)
}
} else {
$ruleScript
}) | Register-ScriptCopRule
}
$FixerFiles = [IO.Directory]::GetFiles("$psScriptRoot$([IO.Path]::DirectorySeparatorChar)Fixers")
$FixerScripts =
foreach ($_ in $FixerFiles) {
if (($_ -as [IO.FileInfo]).Extension -eq '.ps1') {
$ExecutionContext.SessionState.InvokeCommand.GetCommand($_, 'ExternalScript')
}
}
foreach ($_ in $FixerScripts) {
Write-Verbose "Attempting to Import $_"
Test-ScriptCopFixer -ErrorAction SilentlyContinue -ErrorVariable RuleImportError -CommandInfo $_
@(if ($RuleImportError) {
Write-Verbose ($RuleImportError |Out-String)
# Ok, see if it contains functions
$functionOnly = Get-FunctionFromScript -ScriptBlock ([ScriptBlock]::Create($_.ScriptContents))
. $_
$cmds = @()
foreach ($f in $functionOnly) {
#. ([ScriptBlock]::Create($f))
$matched = $f -match "function ((\w+-\w+)|(\w+))"
if ($matched -and $matches[1]) {
$foundCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand("$($matches[1])", 'Function')
if ($foundCmd) {
$cmds += $foundCmd
}
#$cmds+=Get-Command $matches[1] -ErrorAction SilentlyContinue
}
}
foreach ($cmd in $cmds) {
Test-ScriptCopFixer -ErrorAction SilentlyContinue -ErrorVariable RuleImportError2 -CommandInfo $cmd
if ($ruleImportError2) {
Write-Verbose ($RuleImportError2 |Out-String)
} else {
$cmd
}
}
if (-not $RuleImportError2) {
Write-Verbose ($RuleImportError1 |Out-String)
}
} else {
$_
}) | Register-ScriptCopFixer
}
#endregion
#region Import Patrols
Get-ChildItem $psScriptRoot\Patrols -ErrorAction SilentlyContinue -Filter *.patrol.psd1 |
ForEach-Object {
$fullPath = $_.fullname
$name = $_.Name.Replace(".patrol.psd1", "")
$patrolContent = try { ([PowerShell]::Create().AddScript("
`$executionContext.SessionState.LanguageMode = 'RestrictedLanguage'
$([IO.File]::ReadAllText($fullPath))
").Invoke())[0] } catch {
Write-Debug "Error Importing $fullpath : $($_ | Out-string)"
}
if ($patrolContent) {
$patrolContent.Name = $name
Register-ScriptCopPatrol @patrolContent
}
}
#endregion