Skip to content

Commit e53fc60

Browse files
committed
Add documentation CustomRulePath in README.md
1 parent 5797a04 commit e53fc60

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

README.md

+69-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ Settings Support in ScriptAnalyzer
318318
Settings that describe ScriptAnalyzer rules to include/exclude based on `Severity` can be created and supplied to
319319
`Invoke-ScriptAnalyzer` using the `Setting` parameter. This enables a user to create a custom configuration for a specific environment. We support the following modes for specifying the settings file.
320320

321-
## Built-in Presets
321+
## Using parameter Settings
322+
323+
### Built-in Presets
324+
322325
ScriptAnalyzer ships a set of built-in presets that can be used to analyze scripts. For example, if the user wants to run *PowerShell Gallery* rules on their module, then they use the following command.
323326

324327
```powershell
@@ -327,7 +330,7 @@ PS> Invoke-ScriptAnalyzer -Path /path/to/module/ -Settings PSGallery -Recurse
327330

328331
Along with `PSGallery` there are a few other built-in presets, including, `DSC` and `CodeFormatting`, that can be used. These presets can be tab completed for the `Settings` parameter.
329332

330-
## Explicit
333+
### Explicit
331334

332335
The following example excludes two rules from the default set of rules and any rule
333336
that does not output an Error or Warning diagnostic record.
@@ -362,7 +365,8 @@ Then invoke that settings file:
362365
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Settings PSScriptAnalyzerSettings.psd1
363366
```
364367

365-
## Implicit
368+
### Implicit
369+
366370
If you place a PSScriptAnayzer settings file named `PSScriptAnalyzerSettings.psd1` in your project root, PSScriptAnalyzer will discover it if you pass the project root as the `Path` parameter.
367371

368372
```PowerShell
@@ -371,6 +375,68 @@ Invoke-ScriptAnalyzer -Path "C:\path\to\project" -Recurse
371375

372376
Note that providing settings explicitly takes higher precedence over this implicit mode. Sample settings files are provided [here](https://github.com/PowerShell/PSScriptAnalyzer/tree/master/Engine/Settings).
373377

378+
## Custom rules
379+
380+
It is possible to provide one or more paths to custom rules in the settings file.
381+
It is important that these paths either point to a module's folder (implicitly
382+
uses the module manifest) or to the module's script file (.psm1). The module
383+
should export the custom rules (as functions) for them to be available to
384+
PS Script Analyzer.
385+
386+
In this example the property `CustomRulePath` points to two different modules.
387+
Both modules exports the rules (the functions) with the verb `Measure`
388+
so that us used for the property `IncludeRules`.
389+
390+
```powershell
391+
@{
392+
CustomRulePath = @(
393+
'.\output\RequiredModules\DscResource.AnalyzerRules'
394+
'.\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1'
395+
)
396+
397+
IncludeRules = @(
398+
'Measure-*'
399+
)
400+
}
401+
```
402+
403+
It is also possible to used default rules by adding those to `IncludeRules`.
404+
When including default rules is important that the property `IncludeDefaultRules`
405+
is set to `$true` otherwise the default rules will not be triggered.
406+
407+
```powershell
408+
@{
409+
CustomRulePath = @(
410+
'.\output\RequiredModules\DscResource.AnalyzerRules'
411+
'.\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1'
412+
)
413+
414+
IncludeDefaultRules = $true
415+
416+
IncludeRules = @(
417+
# Default rules
418+
'PSAvoidDefaultValueForMandatoryParameter'
419+
'PSAvoidDefaultValueSwitchParameter'
420+
421+
# Custom rules
422+
'Measure-*'
423+
)
424+
}
425+
```
426+
427+
### Using custom rules in Visual Studio Code
428+
429+
It is also possible to use the custom rules that are provided in the
430+
settings file in Visual Studio Code. This is done by adding a Visual Studio
431+
Code workspace settings file (`.vscode/settings.json`).
432+
433+
```json
434+
{
435+
"powershell.scriptAnalysis.settingsPath": ".vscode\\analyzersettings.psd1",
436+
"powershell.scriptAnalysis.enable": true,
437+
}
438+
```
439+
374440
[Back to ToC](#table-of-contents)
375441

376442
ScriptAnalyzer as a .NET library

0 commit comments

Comments
 (0)