Skip to content

Commit 820cde9

Browse files
authored
Add documentation around CustomRulePath in Settings file to README.md (#1636)
* Add documentation CustomRulePath in README.md * Fix table of contents
1 parent 6790863 commit 820cde9

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

README.md

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ Table of Contents
2323
+ [From Chocolatey](#from-chocolatey)
2424
- [Suppressing Rules](#suppressing-rules)
2525
- [Settings Support in ScriptAnalyzer](#settings-support-in-scriptanalyzer)
26-
* [Built-in Presets](#built-in-presets)
27-
* [Explicit](#explicit)
28-
* [Implicit](#implicit)
26+
+ [Using parameter Settings](#using-parameter-settings)
27+
- [Built-in Presets](#built-in-presets)
28+
- [Explicit](#explicit)
29+
- [Implicit](#implicit)
30+
+ [Custom rules](#custom-rules)
31+
- [Using custom rules in Visual Studio Code](#using-custom-rules-in-visual-studio-code)
2932
- [ScriptAnalyzer as a .NET library](#scriptanalyzer-as-a-net-library)
3033
- [Violation Correction](#violation-correction)
3134
- [Contributions are welcome](#contributions-are-welcome)
@@ -318,7 +321,10 @@ Settings Support in ScriptAnalyzer
318321
Settings that describe ScriptAnalyzer rules to include/exclude based on `Severity` can be created and supplied to
319322
`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.
320323

321-
## Built-in Presets
324+
## Using parameter Settings
325+
326+
### Built-in Presets
327+
322328
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.
323329

324330
```powershell
@@ -327,7 +333,7 @@ PS> Invoke-ScriptAnalyzer -Path /path/to/module/ -Settings PSGallery -Recurse
327333

328334
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.
329335

330-
## Explicit
336+
### Explicit
331337

332338
The following example excludes two rules from the default set of rules and any rule
333339
that does not output an Error or Warning diagnostic record.
@@ -362,7 +368,8 @@ Then invoke that settings file:
362368
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Settings PSScriptAnalyzerSettings.psd1
363369
```
364370

365-
## Implicit
371+
### Implicit
372+
366373
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.
367374

368375
```PowerShell
@@ -371,6 +378,68 @@ Invoke-ScriptAnalyzer -Path "C:\path\to\project" -Recurse
371378

372379
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).
373380

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

376445
ScriptAnalyzer as a .NET library

0 commit comments

Comments
 (0)