-
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
f7e01f1
commit 4662ea8
Showing
10 changed files
with
382 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright © 2023 Chocolatey Software, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
[cmdletBinding()] | ||
Param( | ||
[Parameter()] | ||
[String] | ||
$ModuleName, | ||
|
||
[Parameter()] | ||
[String] | ||
$RequiredVersion | ||
) | ||
|
||
if (Get-Module -ListAvailable -Name $ModuleName) { | ||
Write-Host "The $ModuleName PowerShell Module is already installed." | ||
} | ||
else { | ||
Write-Host "Install Module $ModuleName..." | ||
Install-Module -Name $ModuleName -RequiredVersion $RequiredVersion -Force | ||
} |
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
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,123 @@ | ||
// Copyright © 2023 Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
BuildParameters.Tasks.PSScriptAnalyzerTask = Task("Run-PSScriptAnalyzer") | ||
.WithCriteria(() => BuildParameters.ShouldRunPSScriptAnalyzer, "Skipping because PSScriptAnalyzer is not enabled") | ||
.WithCriteria(() => BuildParameters.ShouldRunAnalyze, "Skipping because running analysis tasks is not enabled") | ||
.Does(() => RequirePSModule("PSScriptAnalyzer", "1.21.0", () => | ||
RequirePSModule("ConvertToSARIF", "1.0.0", () => { | ||
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; | ||
} | ||
|
||
var outputFolder = MakeAbsolute(BuildParameters.Paths.Directories.PSScriptAnalyzerResults).FullPath; | ||
EnsureDirectoryExists(outputFolder); | ||
|
||
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") | ||
.WithModule("ConvertToSARIF") | ||
.WithModule("Microsoft.PowerShell.Management") | ||
.WithModule("Microsoft.PowerShell.Utility") | ||
.SetFormatOutput(false) | ||
.SetLogOutput(true) | ||
.OutputToAppConsole(true) | ||
.WithArguments(args => { | ||
args.AppendQuoted("AnalyzePath", PSScriptAnalyzerSetting.AnalysisPath.ToString()) | ||
.AppendQuoted("SettingsPath", PSScriptAnalyzerSetting.SettingsPath.ToString()) | ||
.AppendQuoted("OutputPath", outputFolder) | ||
.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 pwshSettings = new PowershellSettings() | ||
.WithModule("PSScriptAnalyzer") | ||
.WithModule("ConvertToSARIF") | ||
.WithModule("Microsoft.PowerShell.Management") | ||
.WithModule("Microsoft.PowerShell.Utility") | ||
.SetFormatOutput(false) | ||
.SetLogOutput(true) | ||
.OutputToAppConsole(true) | ||
.WithArguments(args => { | ||
args.AppendQuoted("AnalyzePath", BuildParameters.RootDirectoryPath.ToString()) | ||
.AppendQuoted("SettingsPath", settingsFile.ToString()) | ||
.AppendQuoted("OutputPath", outputFolder) | ||
.AppendArray("ExcludePaths", ToolSettings.PSScriptAnalyzerExcludePaths); | ||
}); | ||
|
||
pwshSettings.ExceptionOnScriptError = false; | ||
|
||
var resultCollection = StartPowershellFile(MakeAbsolute(powerShellAnalysisScript), pwshSettings); | ||
var returnCode = int.Parse(resultCollection[0].BaseObject.ToString()); | ||
|
||
Information("Result: {0}", returnCode); | ||
|
||
// NOTE: Ideally, we would have this throw an exception, however, during testing, invoking PSScriptAnalyzer | ||
// sometimes caused random "The term 'Get-Command' is not recognized as the name of a cmdlet, function, script | ||
// file, or operable program." errors, which meant that we can't rely on this returnCode. For now, we | ||
// only want PSScriptAnalyzer to warn on violations, so we don't need to worry about this just now. | ||
//if (returnCode != 0) | ||
//{ | ||
// throw new ApplicationException("Script failed to execute"); | ||
//} | ||
} | ||
}) | ||
) | ||
); | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.