-
Notifications
You must be signed in to change notification settings - Fork 117
Add Analyze step for script analyzer. #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0c7158c
a0fbe4d
0ae4cd4
b2efc52
2f2dab0
b6ed918
daa1cbe
58c4fff
888ce86
37a7e14
2145282
9d95056
743e289
248bfe5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,13 @@ Please follow the scripting style of this file when adding new script. | |
General notes | ||
#> | ||
function Invoke-Plaster { | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingWriteHost', '', Scope='Function')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidShouldContinueWithoutForce', '', Scope='Function', Target='CopyFileWithConflictDetection')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function', Target='CopyFileWithConflictDetection')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function', Target='ProcessFile')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function', Target='ProcessModifyFile')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function', Target='ProcessNewModuleManifest')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText', '', Scope='Function', Target='ProcessParameter')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function', Target='ProcessRequireModule')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidShouldContinueWithoutForce', '', Scope='Function', Target='ProcessFile')] | ||
[CmdletBinding(SupportsShouldProcess=$true)] | ||
|
@@ -709,7 +712,8 @@ function Invoke-Plaster { | |
# Copy over empty directories - if any. | ||
$gciParams.Remove('File') | ||
$gciParams['Directory'] = $true | ||
$dirs = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams | Where {$_.GetFileSystemInfos().Length -eq 0}) | ||
$dirs = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams | | ||
Where-Object {$_.GetFileSystemInfos().Length -eq 0}) | ||
foreach ($dir in $dirs) { | ||
$dirSrcPath = $dir.FullName | ||
$relPath = $dirSrcPath.Substring($srcRelRootPathLength) | ||
|
@@ -1112,13 +1116,9 @@ function Invoke-Plaster { | |
} | ||
} | ||
|
||
<# | ||
██ ██ ███████ ██ ██████ ███████ ██████ ███████ | ||
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ | ||
███████ █████ ██ ██████ █████ ██████ ███████ | ||
██ ██ ██ ██ ██ ██ ██ ██ ██ | ||
██ ██ ███████ ███████ ██ ███████ ██ ██ ███████ | ||
#> | ||
############################################################################### | ||
# Helper functions | ||
############################################################################### | ||
|
||
function InitializePredefinedVariables([string]$TemplatePath, [string]$DestPath) { | ||
# Always set these variables, even if the command has been run with -WhatIf | ||
|
@@ -1212,7 +1212,7 @@ function WriteContentWithEncoding([string]$path, [string[]]$content, [string]$en | |
'utf8' { $noBomEncoding = New-Object System.Text.UTF8Encoding($false) } | ||
} | ||
|
||
if ($content -eq $null) { | ||
if ($null -eq $content) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I try to be consistent with this but forget from time to time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, it's generally a lot more consistent that what I normally manage :) |
||
$content = [string]::Empty | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ Task Clean -requiredVariables ReleaseDir { | |
} | ||
} | ||
|
||
Task Build -depends BuildImpl, Sign, PostBuild { | ||
Task Build -depends BuildImpl, Analyze, Sign, PostBuild { | ||
} | ||
|
||
Task BuildImpl -depends Init, Clean, PreBuild -requiredVariables SrcRootDir, OutDir { | ||
|
@@ -134,6 +134,41 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { | |
} | ||
} | ||
|
||
Task Analyze -depends BuildImpl -requiredVariables ScriptAnalysisAction, OutDir { | ||
if ((Get-Host).Name -in $SkipScriptAnalysisHost) { | ||
$ScriptAnalysisAction = 'Skip' | ||
} | ||
|
||
if ($ScriptAnalysisAction -eq 'Skip') { | ||
"Script analysis is not enabled. Skipping Analyze task." | ||
return | ||
} | ||
|
||
$analysisResult = Invoke-ScriptAnalyzer -Path $OutDir -Recurse -Verbose:$VerbosePreference | ||
$analysisResult | Format-Table | ||
switch ($ScriptAnalysisAction) { | ||
'Error' { | ||
Assert -conditionToCheck ( | ||
($analysisResult | Where-Object Severity -eq 'Error').Count -eq 0 | ||
) -failureMessage 'One or more Script Analyzer errors were found. Build cannot continue!' | ||
} | ||
'Warning' { | ||
Assert -conditionToCheck ( | ||
($analysisResult | Where-Object { | ||
$_.Severity -eq 'Warning' -or $_.Severity -eq 'Error' | ||
}).Count -eq 0) -failureMessage 'One or more Script Analyzer warnings were found. Build cannot continue!' | ||
} | ||
'None' { | ||
return | ||
} | ||
default { | ||
Assert -conditionToCheck ( | ||
$analysisResult.Count -eq 0 | ||
) -failureMessage 'One or more Script Analyzer issues were found. Build cannot continue!' | ||
} | ||
} | ||
} | ||
|
||
Task GenerateMarkdown -depends Build, PreBuildHelp -requiredVariables DocsRootDir, ModuleName, OutDir { | ||
if ($null -eq $DefaultLocale) { | ||
$DefaultLocale = 'en-US' | ||
|
@@ -194,7 +229,7 @@ Task InstallImpl -depends BuildHelp, PreInstall -requiredVariables OutDir { | |
Copy-Item -Path $OutDir\* -Destination $InstallPath -Verbose:$VerbosePreference -Recurse -Force | ||
} | ||
|
||
Task Test -depends Build -requiredVariables TestRootDir, ModuleName { | ||
Task Test -depends Analyze -requiredVariables TestRootDir, ModuleName { | ||
Import-Module Pester | ||
|
||
try { | ||
|
@@ -375,6 +410,7 @@ function PromptUserForCredentialAndStorePassword { | |
} | ||
|
||
function AddSetting { | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a matter of preference, I would put this outside the function. But that might just be the C# dev in me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did try that, putting it outside the function and at the top of the file, but it doesn't seem to like it at all. (Error is: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW is this cropping up because we are analyzing the build script? If so, perhaps we should only analyze scripts under the src dir? Sorry if this is a duplicate comment. I responded today by email (on my phone) while I was out and about with my wife. Those email responses don't seem to get added to the review. Oh well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, this file is under src. Duh. Guess I need to just call it a night and get some sleep. :-) |
||
param( | ||
[Parameter(Mandatory)] | ||
[string]$Key, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
#> | ||
function Test-PlasterManifest { | ||
[CmdletBinding()] | ||
[OutputType([System.Xml.XmlDocument])] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice add. Thanks. |
||
param( | ||
# Specifies a path to a plasterManifest.xml file. | ||
[Parameter(Position=0, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad this particular PSSA bug was recently fixed. Well, I haven't had a chance to test the fix (not sure it is even shipping yet) but I saw the PR go by that purports to fix it. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which bug/fix? I did notice that the suppression rule for
PSAvoidUsingWriteHost
didn't actually manage to suppress all the usages, which was a bit odd. Is that what you meant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I wasn't very clear. This is the PR on PSSA I was referring to: PowerShell/PSScriptAnalyzer#625 I was running into this issue in Invoke-Plaster because I had several helper functions within
begin
that use $PSCmdlet.ShouldProcess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I am testing with script analyzer 1.8.0 and it appears to resolve that issue without requiring suppression anymore, so those PSShouldProcess suppression statements can go at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as we are using 1.8.0 in the AppVeyor build, we should be able to remove these. Let me take a crack at it.