-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a task to run PSScriptAnalyzer. It utilizes the Cake.Powershell module to run a script that will get ps1/psm1 files that are not excluded and run them through the specified settings file. Either a default formatting only run can be done, or custom run(s) can be specified. A custom base analysis path, setting file and set of folder exclusions can be specified for each run. For example, a custom run can be created to check that the Chocolatey powershell helpers use only the PSv2 cmdlets. The default run is for formatting, it checks files against the build in formatting related rules. This run can be expanded in the future, once enough projects are up to a baseline of good scripting practices.
- Loading branch information
1 parent
4df729e
commit fb2fa47
Showing
6 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
@{ | ||
IncludeRules = @( | ||
'PSUseBOMForUnicodeEncodedFile', | ||
'PSMisleadingBacktick', | ||
'PSAvoidUsingCmdletAliases', | ||
'PSAvoidTrailingWhitespace', | ||
'PSAvoidSemicolonsAsLineTerminators', | ||
'PSUseCorrectCasing', | ||
'PSPlaceOpenBrace', | ||
'PSPlaceCloseBrace', | ||
'PSAlignAssignmentStatement', | ||
'PSUseConsistentWhitespace', | ||
'PSUseConsistentIndentation' | ||
) | ||
|
||
Rules = @{ | ||
|
||
<# | ||
PSAvoidUsingCmdletAliases = @{ | ||
'allowlist' = @('') | ||
}#> | ||
|
||
PSAvoidSemicolonsAsLineTerminators = @{ | ||
Enable = $true | ||
} | ||
|
||
PSUseCorrectCasing = @{ | ||
Enable = $true | ||
} | ||
|
||
PSPlaceOpenBrace = @{ | ||
Enable = $true | ||
OnSameLine = $true | ||
NewLineAfter = $true | ||
IgnoreOneLineBlock = $false | ||
} | ||
|
||
PSPlaceCloseBrace = @{ | ||
Enable = $true | ||
NewLineAfter = $true | ||
IgnoreOneLineBlock = $false | ||
NoEmptyLineBefore = $true | ||
} | ||
|
||
PSAlignAssignmentStatement = @{ | ||
Enable = $true | ||
CheckHashtable = $true | ||
} | ||
|
||
PSUseConsistentIndentation = @{ | ||
Enable = $true | ||
Kind = 'space' | ||
PipelineIndentation = 'IncreaseIndentationForFirstPipeline' | ||
IndentationSize = 4 | ||
} | ||
|
||
PSUseConsistentWhitespace = @{ | ||
Enable = $true | ||
CheckInnerBrace = $true | ||
CheckOpenBrace = $true | ||
CheckOpenParen = $true | ||
CheckOperator = $true | ||
CheckPipe = $true | ||
CheckPipeForRedundantWhitespace = $false | ||
CheckSeparator = $true | ||
CheckParameter = $false | ||
IgnoreAssignmentOperatorInsideHashTable = $true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
BuildParameters.Tasks.PSScriptAnalyzerTask = Task("Run-PSScriptAnalyzer") | ||
.WithCriteria(() => BuildParameters.ShouldRunPSScriptAnalyzer, "Skipping because PSScriptAnalyzer is not enabled") | ||
.Does(() => | ||
{ | ||
var powerShellAnalysisScript = GetFiles("./tools/Chocolatey.Cake.Recipe*/Content/run-psscriptanalyzer.ps1").FirstOrDefault(); | ||
|
||
if (powerShellAnalysisScript == null) | ||
{ | ||
Warning("Unable to find PowerShell Analysis script, so unable to run analysis."); | ||
return; | ||
} | ||
|
||
if (BuildParameters.GetPSScriptAnalyzerSettings != null) | ||
{ | ||
foreach (var PSScriptAnalyzerSetting in BuildParameters.GetPSScriptAnalyzerSettings()) | ||
{ | ||
Information(string.Format("Running PSScriptAnalyzer {0}", PSScriptAnalyzerSetting.Name); | ||
|
||
StartPowershellFile(MakeAbsolute(powerShellAnalysisScript), new PowershellSettings() | ||
.WithModule("PSScriptAnalyzer") | ||
.SetFormatOutput(true) | ||
.WithArguments(args => { | ||
args.AppendQuoted("AnalyzePath", PSScriptAnalyzerSetting.AnalysisPath.ToString()) | ||
.AppendQuoted("SettingsPath", PSScriptAnalyzerSetting.SettingsPath.ToString()) | ||
.AppendArray("ExcludePaths", PSScriptAnalyzerSetting.ExcludePaths); | ||
})); | ||
} | ||
} | ||
else | ||
{ | ||
Information("There are no PSScriptAnalyzer Settings defined for this build, running with default format checking settings."); | ||
|
||
var settingsFile = GetFiles("./tools/Chocolatey.Cake.Recipe*/Content/formatting-settings.psd1").FirstOrDefault(); | ||
|
||
if (settingsFile == null) | ||
{ | ||
Warning("Unable to find PowerShell Analysis settings, so unable to run analysis."); | ||
return; | ||
} | ||
|
||
var excludePaths = new List<String> { "tools", "code_drop"}; | ||
|
||
StartPowershellFile(MakeAbsolute(powerShellAnalysisScript), new PowershellSettings() | ||
.WithModule("PSScriptAnalyzer") | ||
.SetFormatOutput(true) | ||
.WithArguments(args => { | ||
args.AppendQuoted("AnalyzePath", BuildParameters.RootDirectoryPath.ToString()) | ||
.AppendQuoted("SettingsPath", settingsFile.ToString()) | ||
.AppendArray("ExcludePaths", excludePaths); | ||
})); | ||
} | ||
}); | ||
|
||
public class PSScriptAnalyzerSettings | ||
{ | ||
public FilePath AnalysisPath { get; set; } | ||
public FilePath SettingsPath { get; set; } | ||
public List<String> ExcludePaths { get; set; } | ||
public string Name { get; set; } | ||
|
||
public PSScriptAnalyzerSettings() | ||
{ | ||
Name = "Unnamed"; | ||
} | ||
|
||
public PSScriptAnalyzerSettings(FilePath analysisPath, | ||
FilePath settingsPath, | ||
List<String> excludePaths = null, | ||
string name = "Unnamed") | ||
{ | ||
AnalysisPath = analysisPath; | ||
SettingsPath = settingsPath; | ||
ExcludePaths = excludePaths; | ||
Name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[cmdletBinding()] | ||
Param( | ||
[Parameter()] | ||
[String] | ||
$AnalyzePath, | ||
|
||
[Parameter()] | ||
[String] | ||
$SettingsPath, | ||
|
||
[Parameter()] | ||
[String[]] | ||
$ExcludePaths | ||
) | ||
|
||
#Requires -Modules PSScriptAnalyzer | ||
|
||
Push-Location -Path $AnalyzePath | ||
|
||
try { | ||
if ($PSBoundParameters.ContainsKey('ExcludePaths')) { | ||
$ExcludePaths = $ExcludePaths | ForEach-Object { (Resolve-Path -Path $_).Path } | ||
} | ||
|
||
$scripts = Get-ChildItem -Path $AnalyzePath -Filter "*.ps1" -Recurse | ForEach-Object { | ||
if ($PSBoundParameters.ContainsKey('ExcludePaths')) { | ||
foreach ($path in $ExcludePaths) { | ||
if ($_.FullName.StartsWith($path)) { | ||
return | ||
} | ||
} | ||
} | ||
$_ | ||
} | ||
$modules = Get-ChildItem -Path $AnalyzePath -Filter "*.psm1" -Recurse | ForEach-Object { | ||
if ($PSBoundParameters.ContainsKey('ExcludePaths')) { | ||
foreach ($path in $ExcludePaths) { | ||
if ($_.FullName.StartsWith($path)) { | ||
return | ||
} | ||
} | ||
} | ||
$_ | ||
} | ||
|
||
$modules | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message | ||
$Scripts | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message | ||
} | ||
finally { | ||
Pop-Location | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters