From 2f2dab094344fffe8a9ef1a3ccae3579eaa9faad Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sat, 8 Oct 2016 16:54:55 +0100 Subject: [PATCH 01/10] Resolve most scriptanalyzer warnings --- src/InvokePlaster.ps1 | 10 +++++++--- src/Templates/NewModule/test/Module.T.ps1 | 1 + src/TestPlasterManifest.ps1 | 1 + test/ModuleManifest.Tests.ps1 | 1 + test/Shared.ps1 | 4 ++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/InvokePlaster.ps1 b/src/InvokePlaster.ps1 index 468615e..70670fd 100644 --- a/src/InvokePlaster.ps1 +++ b/src/InvokePlaster.ps1 @@ -1,4 +1,4 @@ -<# +<# NOTE TO DEVELOPERS: All text displayed to the user except for Write-Debug (or $PSCmdlet.WriteDebug()) text must be added to the string tables in: @@ -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) @@ -1212,7 +1216,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) { $content = [string]::Empty } diff --git a/src/Templates/NewModule/test/Module.T.ps1 b/src/Templates/NewModule/test/Module.T.ps1 index bdb45d9..9ebf705 100644 --- a/src/Templates/NewModule/test/Module.T.ps1 +++ b/src/Templates/NewModule/test/Module.T.ps1 @@ -1,3 +1,4 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SuppressImportModule')] $SuppressImportModule = $true . $PSScriptRoot\Shared.ps1 diff --git a/src/TestPlasterManifest.ps1 b/src/TestPlasterManifest.ps1 index 75c1143..c555b83 100644 --- a/src/TestPlasterManifest.ps1 +++ b/src/TestPlasterManifest.ps1 @@ -26,6 +26,7 @@ #> function Test-PlasterManifest { [CmdletBinding()] + [OutputType([System.Xml.XmlDocument])] param( # Specifies a path to a plasterManifest.xml file. [Parameter(Position=0, diff --git a/test/ModuleManifest.Tests.ps1 b/test/ModuleManifest.Tests.ps1 index bdb45d9..9ebf705 100644 --- a/test/ModuleManifest.Tests.ps1 +++ b/test/ModuleManifest.Tests.ps1 @@ -1,3 +1,4 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SuppressImportModule')] $SuppressImportModule = $true . $PSScriptRoot\Shared.ps1 diff --git a/test/Shared.ps1 b/test/Shared.ps1 index 4ef9d68..11ef88c 100644 --- a/test/Shared.ps1 +++ b/test/Shared.ps1 @@ -1,7 +1,11 @@ $ModuleManifestName = 'Plaster.psd1' $ModuleManifestPath = "$PSScriptRoot\..\src\$ModuleManifestName" $TemplateDir = "$PSScriptRoot\TemplateRootTemp" + +[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='OutDir')] $OutDir = "$PSScriptRoot\Out" + +[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='PlasterManifestPath')] $PlasterManifestPath = "$TemplateDir\plasterManifest.xml" if (!$SuppressImportModule) { From b6ed918f9cc343cdbe9e6e65747d87515590b9d3 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sat, 8 Oct 2016 21:02:31 +0100 Subject: [PATCH 02/10] Add Analyze task to run PSScriptAnalyzer --- build.psake.ps1 | 15 +++++++++++++++ build.settings.ps1 | 16 +++++++++++++++- src/Templates/NewModule/build.psake.ps1 | 15 +++++++++++++++ src/Templates/NewModule/build.settings.ps1 | 16 +++++++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/build.psake.ps1 b/build.psake.ps1 index 3813d0e..d2e756c 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -134,6 +134,21 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } +Task Analyze -depends Build { + if ((Get-Host).Name -in $SkipCodeAnalysisHost) { + $SkipCodeAnalysis = $true + } + + if ($SkipCodeAnalysis) { + "Script analysis is not enabled. Skipping Analyze task." + return + } + + $analysisResult = Invoke-ScriptAnalyzer -Path $OutDir -Recurse -Verbose:$VerbosePreference + $analysisResult | Format-Table + Assert -conditionToCheck ($analysisResult.Count -eq 0) -failureMessage 'One or more Script Analyzer errors/warnings were found. Build cannot continue!' +} + Task GenerateMarkdown -depends Build, PreBuildHelp -requiredVariables DocsRootDir, ModuleName, OutDir { if ($null -eq $DefaultLocale) { $DefaultLocale = 'en-US' diff --git a/build.settings.ps1 b/build.settings.ps1 index 647b388..d7fe1eb 100644 --- a/build.settings.ps1 +++ b/build.settings.ps1 @@ -55,7 +55,21 @@ Properties { [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CertSubjectName')] $CertSubjectName = $null - + # -------------------- Script Analysis properties ------------------------------ + + # The script analysis task step will run, unless your host is in the array defined below. + # This allows you to control whether code analysis is executed, for hosts where script + # analysis is included in the product. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysisHost')] + $SkipCodeAnalysisHost = @( + 'Visual Studio Code Host', + 'My Custom Host with scriptanalyzer support' + ) + + # Alternatively, you can set the following variable to control script analysis. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] + $SkipCodeAnalysis = $null + # -------------------- Publishing properties ------------------------------ # Your NuGet API key for the PSGallery. Leave it as $null and the first time you publish, diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index 3813d0e..d2e756c 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -134,6 +134,21 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } +Task Analyze -depends Build { + if ((Get-Host).Name -in $SkipCodeAnalysisHost) { + $SkipCodeAnalysis = $true + } + + if ($SkipCodeAnalysis) { + "Script analysis is not enabled. Skipping Analyze task." + return + } + + $analysisResult = Invoke-ScriptAnalyzer -Path $OutDir -Recurse -Verbose:$VerbosePreference + $analysisResult | Format-Table + Assert -conditionToCheck ($analysisResult.Count -eq 0) -failureMessage 'One or more Script Analyzer errors/warnings were found. Build cannot continue!' +} + Task GenerateMarkdown -depends Build, PreBuildHelp -requiredVariables DocsRootDir, ModuleName, OutDir { if ($null -eq $DefaultLocale) { $DefaultLocale = 'en-US' diff --git a/src/Templates/NewModule/build.settings.ps1 b/src/Templates/NewModule/build.settings.ps1 index 647b388..d7fe1eb 100644 --- a/src/Templates/NewModule/build.settings.ps1 +++ b/src/Templates/NewModule/build.settings.ps1 @@ -55,7 +55,21 @@ Properties { [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CertSubjectName')] $CertSubjectName = $null - + # -------------------- Script Analysis properties ------------------------------ + + # The script analysis task step will run, unless your host is in the array defined below. + # This allows you to control whether code analysis is executed, for hosts where script + # analysis is included in the product. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysisHost')] + $SkipCodeAnalysisHost = @( + 'Visual Studio Code Host', + 'My Custom Host with scriptanalyzer support' + ) + + # Alternatively, you can set the following variable to control script analysis. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] + $SkipCodeAnalysis = $null + # -------------------- Publishing properties ------------------------------ # Your NuGet API key for the PSGallery. Leave it as $null and the first time you publish, From daa1cbe1065bdd5f2e715d20d43418265cb15ab1 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sat, 8 Oct 2016 21:24:49 +0100 Subject: [PATCH 03/10] Deal with further PSScriptAnalyzer warnings --- build.psake.ps1 | 1 + src/Templates/NewModule/build.psake.ps1 | 1 + test/Shared.ps1 | 1 + 3 files changed, 3 insertions(+) diff --git a/build.psake.ps1 b/build.psake.ps1 index d2e756c..c6f5ddb 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -390,6 +390,7 @@ function PromptUserForCredentialAndStorePassword { } function AddSetting { + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function')] param( [Parameter(Mandatory)] [string]$Key, diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index d2e756c..c6f5ddb 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -390,6 +390,7 @@ function PromptUserForCredentialAndStorePassword { } function AddSetting { + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function')] param( [Parameter(Mandatory)] [string]$Key, diff --git a/test/Shared.ps1 b/test/Shared.ps1 index 11ef88c..0579056 100644 --- a/test/Shared.ps1 +++ b/test/Shared.ps1 @@ -15,6 +15,7 @@ if (!$SuppressImportModule) { } function CleanDir { + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function')] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] From 58c4fffcec50c77fd0b6ef9af5b3e9f877c87001 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sat, 8 Oct 2016 22:06:32 +0100 Subject: [PATCH 04/10] Improve analyze task control --- build.psake.ps1 | 24 ++++++++++++++++++++-- build.settings.ps1 | 5 +++++ src/Templates/NewModule/build.psake.ps1 | 24 ++++++++++++++++++++-- src/Templates/NewModule/build.settings.ps1 | 5 +++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/build.psake.ps1 b/build.psake.ps1 index c6f5ddb..c4c6f7a 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -134,7 +134,7 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } -Task Analyze -depends Build { +Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { if ((Get-Host).Name -in $SkipCodeAnalysisHost) { $SkipCodeAnalysis = $true } @@ -146,7 +146,27 @@ Task Analyze -depends Build { $analysisResult = Invoke-ScriptAnalyzer -Path $OutDir -Recurse -Verbose:$VerbosePreference $analysisResult | Format-Table - Assert -conditionToCheck ($analysisResult.Count -eq 0) -failureMessage 'One or more Script Analyzer errors/warnings were found. Build cannot continue!' + switch ($CodeAnalysisStop) { + '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 errors were found. Build cannot continue!' + } + 'None' { + break; + } + 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 { diff --git a/build.settings.ps1 b/build.settings.ps1 index d7fe1eb..c879e36 100644 --- a/build.settings.ps1 +++ b/build.settings.ps1 @@ -69,6 +69,11 @@ Properties { # Alternatively, you can set the following variable to control script analysis. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] $SkipCodeAnalysis = $null + + # To control the failure of the build with specific script analyzer rule severities, + # the CodeAnalysisStop variable can be used. The supported values for this variable are + # 'Warning', 'Error', 'All' or 'None'. Invalid input will stop on all rules. + $CodeAnalysisStop = 'Error' # -------------------- Publishing properties ------------------------------ diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index c6f5ddb..c4c6f7a 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -134,7 +134,7 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } -Task Analyze -depends Build { +Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { if ((Get-Host).Name -in $SkipCodeAnalysisHost) { $SkipCodeAnalysis = $true } @@ -146,7 +146,27 @@ Task Analyze -depends Build { $analysisResult = Invoke-ScriptAnalyzer -Path $OutDir -Recurse -Verbose:$VerbosePreference $analysisResult | Format-Table - Assert -conditionToCheck ($analysisResult.Count -eq 0) -failureMessage 'One or more Script Analyzer errors/warnings were found. Build cannot continue!' + switch ($CodeAnalysisStop) { + '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 errors were found. Build cannot continue!' + } + 'None' { + break; + } + 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 { diff --git a/src/Templates/NewModule/build.settings.ps1 b/src/Templates/NewModule/build.settings.ps1 index d7fe1eb..c879e36 100644 --- a/src/Templates/NewModule/build.settings.ps1 +++ b/src/Templates/NewModule/build.settings.ps1 @@ -69,6 +69,11 @@ Properties { # Alternatively, you can set the following variable to control script analysis. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] $SkipCodeAnalysis = $null + + # To control the failure of the build with specific script analyzer rule severities, + # the CodeAnalysisStop variable can be used. The supported values for this variable are + # 'Warning', 'Error', 'All' or 'None'. Invalid input will stop on all rules. + $CodeAnalysisStop = 'Error' # -------------------- Publishing properties ------------------------------ From 888ce8632d2dbbfc88a5a77cf2993cbac0342e1c Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sat, 8 Oct 2016 22:54:03 +0100 Subject: [PATCH 05/10] Merge analyze task skip and control variables --- build.psake.ps1 | 8 ++++---- build.settings.ps1 | 8 +++----- src/Templates/NewModule/build.psake.ps1 | 8 ++++---- src/Templates/NewModule/build.settings.ps1 | 8 +++----- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/build.psake.ps1 b/build.psake.ps1 index c4c6f7a..ed8633c 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -136,10 +136,10 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { if ((Get-Host).Name -in $SkipCodeAnalysisHost) { - $SkipCodeAnalysis = $true + $CodeAnalysisStop = 'Skip' } - if ($SkipCodeAnalysis) { + if ($CodeAnalysisStop -eq 'Skip') { "Script analysis is not enabled. Skipping Analyze task." return } @@ -156,10 +156,10 @@ Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { Assert -conditionToCheck ( ($analysisResult | Where-Object { $_.Severity -eq 'Warning' -or $_.Severity -eq 'Error' - }).Count -eq 0) -failureMessage 'One or more Script Analyzer errors were found. Build cannot continue!' + }).Count -eq 0) -failureMessage 'One or more Script Analyzer warnings were found. Build cannot continue!' } 'None' { - break; + return } default { Assert -conditionToCheck ( diff --git a/build.settings.ps1 b/build.settings.ps1 index c879e36..8710740 100644 --- a/build.settings.ps1 +++ b/build.settings.ps1 @@ -66,13 +66,11 @@ Properties { 'My Custom Host with scriptanalyzer support' ) - # Alternatively, you can set the following variable to control script analysis. - [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] - $SkipCodeAnalysis = $null - # To control the failure of the build with specific script analyzer rule severities, # the CodeAnalysisStop variable can be used. The supported values for this variable are - # 'Warning', 'Error', 'All' or 'None'. Invalid input will stop on all rules. + # 'Warning', 'Error', 'All', 'None' or 'Skip'. Invalid input will stop on all rules. + # 'Skip' will skip over the code analysis step all together. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CodeAnalysisStop')] $CodeAnalysisStop = 'Error' # -------------------- Publishing properties ------------------------------ diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index c4c6f7a..ed8633c 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -136,10 +136,10 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { if ((Get-Host).Name -in $SkipCodeAnalysisHost) { - $SkipCodeAnalysis = $true + $CodeAnalysisStop = 'Skip' } - if ($SkipCodeAnalysis) { + if ($CodeAnalysisStop -eq 'Skip') { "Script analysis is not enabled. Skipping Analyze task." return } @@ -156,10 +156,10 @@ Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { Assert -conditionToCheck ( ($analysisResult | Where-Object { $_.Severity -eq 'Warning' -or $_.Severity -eq 'Error' - }).Count -eq 0) -failureMessage 'One or more Script Analyzer errors were found. Build cannot continue!' + }).Count -eq 0) -failureMessage 'One or more Script Analyzer warnings were found. Build cannot continue!' } 'None' { - break; + return } default { Assert -conditionToCheck ( diff --git a/src/Templates/NewModule/build.settings.ps1 b/src/Templates/NewModule/build.settings.ps1 index c879e36..8710740 100644 --- a/src/Templates/NewModule/build.settings.ps1 +++ b/src/Templates/NewModule/build.settings.ps1 @@ -66,13 +66,11 @@ Properties { 'My Custom Host with scriptanalyzer support' ) - # Alternatively, you can set the following variable to control script analysis. - [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysis')] - $SkipCodeAnalysis = $null - # To control the failure of the build with specific script analyzer rule severities, # the CodeAnalysisStop variable can be used. The supported values for this variable are - # 'Warning', 'Error', 'All' or 'None'. Invalid input will stop on all rules. + # 'Warning', 'Error', 'All', 'None' or 'Skip'. Invalid input will stop on all rules. + # 'Skip' will skip over the code analysis step all together. + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CodeAnalysisStop')] $CodeAnalysisStop = 'Error' # -------------------- Publishing properties ------------------------------ From 37a7e1499198c467a182687d0c980929a87b639a Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sun, 9 Oct 2016 00:18:19 +0100 Subject: [PATCH 06/10] Rename script analysis variables --- build.psake.ps1 | 8 ++++---- build.settings.ps1 | 6 +++--- src/Templates/NewModule/build.psake.ps1 | 8 ++++---- src/Templates/NewModule/build.settings.ps1 | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/build.psake.ps1 b/build.psake.ps1 index ed8633c..3ce7957 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -135,18 +135,18 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { - if ((Get-Host).Name -in $SkipCodeAnalysisHost) { - $CodeAnalysisStop = 'Skip' + if ((Get-Host).Name -in $SkipScriptAnalysisHost) { + $ScriptAnalysisAction = 'Skip' } - if ($CodeAnalysisStop -eq '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 ($CodeAnalysisStop) { + switch ($ScriptAnalysisAction) { 'Error' { Assert -conditionToCheck ( ($analysisResult | Where-Object Severity -eq 'Error').Count -eq 0 diff --git a/build.settings.ps1 b/build.settings.ps1 index 8710740..62b60ec 100644 --- a/build.settings.ps1 +++ b/build.settings.ps1 @@ -61,7 +61,7 @@ Properties { # This allows you to control whether code analysis is executed, for hosts where script # analysis is included in the product. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysisHost')] - $SkipCodeAnalysisHost = @( + $SkipScriptAnalysisHost = @( 'Visual Studio Code Host', 'My Custom Host with scriptanalyzer support' ) @@ -71,8 +71,8 @@ Properties { # 'Warning', 'Error', 'All', 'None' or 'Skip'. Invalid input will stop on all rules. # 'Skip' will skip over the code analysis step all together. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CodeAnalysisStop')] - $CodeAnalysisStop = 'Error' - + $ScriptAnalysisAction = 'Error' + # -------------------- Publishing properties ------------------------------ # Your NuGet API key for the PSGallery. Leave it as $null and the first time you publish, diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index ed8633c..3ce7957 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -135,18 +135,18 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { - if ((Get-Host).Name -in $SkipCodeAnalysisHost) { - $CodeAnalysisStop = 'Skip' + if ((Get-Host).Name -in $SkipScriptAnalysisHost) { + $ScriptAnalysisAction = 'Skip' } - if ($CodeAnalysisStop -eq '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 ($CodeAnalysisStop) { + switch ($ScriptAnalysisAction) { 'Error' { Assert -conditionToCheck ( ($analysisResult | Where-Object Severity -eq 'Error').Count -eq 0 diff --git a/src/Templates/NewModule/build.settings.ps1 b/src/Templates/NewModule/build.settings.ps1 index 8710740..62b60ec 100644 --- a/src/Templates/NewModule/build.settings.ps1 +++ b/src/Templates/NewModule/build.settings.ps1 @@ -61,7 +61,7 @@ Properties { # This allows you to control whether code analysis is executed, for hosts where script # analysis is included in the product. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SkipCodeAnalysisHost')] - $SkipCodeAnalysisHost = @( + $SkipScriptAnalysisHost = @( 'Visual Studio Code Host', 'My Custom Host with scriptanalyzer support' ) @@ -71,8 +71,8 @@ Properties { # 'Warning', 'Error', 'All', 'None' or 'Skip'. Invalid input will stop on all rules. # 'Skip' will skip over the code analysis step all together. [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='CodeAnalysisStop')] - $CodeAnalysisStop = 'Error' - + $ScriptAnalysisAction = 'Error' + # -------------------- Publishing properties ------------------------------ # Your NuGet API key for the PSGallery. Leave it as $null and the first time you publish, From 21452826111f745114483155a4703ac5ea1a7531 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sun, 9 Oct 2016 00:22:32 +0100 Subject: [PATCH 07/10] Remove PSSA rule suppression from test files --- src/Templates/NewModule/test/Module.T.ps1 | 1 - test/ModuleManifest.Tests.ps1 | 1 - test/Shared.ps1 | 5 ----- 3 files changed, 7 deletions(-) diff --git a/src/Templates/NewModule/test/Module.T.ps1 b/src/Templates/NewModule/test/Module.T.ps1 index 9ebf705..bdb45d9 100644 --- a/src/Templates/NewModule/test/Module.T.ps1 +++ b/src/Templates/NewModule/test/Module.T.ps1 @@ -1,4 +1,3 @@ -[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SuppressImportModule')] $SuppressImportModule = $true . $PSScriptRoot\Shared.ps1 diff --git a/test/ModuleManifest.Tests.ps1 b/test/ModuleManifest.Tests.ps1 index 9ebf705..bdb45d9 100644 --- a/test/ModuleManifest.Tests.ps1 +++ b/test/ModuleManifest.Tests.ps1 @@ -1,4 +1,3 @@ -[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SuppressImportModule')] $SuppressImportModule = $true . $PSScriptRoot\Shared.ps1 diff --git a/test/Shared.ps1 b/test/Shared.ps1 index 0579056..4ef9d68 100644 --- a/test/Shared.ps1 +++ b/test/Shared.ps1 @@ -1,11 +1,7 @@ $ModuleManifestName = 'Plaster.psd1' $ModuleManifestPath = "$PSScriptRoot\..\src\$ModuleManifestName" $TemplateDir = "$PSScriptRoot\TemplateRootTemp" - -[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='OutDir')] $OutDir = "$PSScriptRoot\Out" - -[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='PlasterManifestPath')] $PlasterManifestPath = "$TemplateDir\plasterManifest.xml" if (!$SuppressImportModule) { @@ -15,7 +11,6 @@ if (!$SuppressImportModule) { } function CleanDir { - [System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope='Function')] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] From 9d950566c8674a8138e1cfde4c7f0a28455ecabd Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sun, 9 Oct 2016 00:48:57 +0100 Subject: [PATCH 08/10] Reorder build tasks, fix incorrect required variable --- build.psake.ps1 | 6 +++--- src/Templates/NewModule/build.psake.ps1 | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.psake.ps1 b/build.psake.ps1 index 3ce7957..970c1c3 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -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,7 +134,7 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } -Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { +Task Analyze -depends BuildImpl -requiredVariables ScriptAnalysisAction, OutDir { if ((Get-Host).Name -in $SkipScriptAnalysisHost) { $ScriptAnalysisAction = 'Skip' } @@ -229,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 { diff --git a/src/Templates/NewModule/build.psake.ps1 b/src/Templates/NewModule/build.psake.ps1 index 3ce7957..970c1c3 100644 --- a/src/Templates/NewModule/build.psake.ps1 +++ b/src/Templates/NewModule/build.psake.ps1 @@ -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,7 +134,7 @@ Task Sign -depends BuildImpl -requiredVariables SettingsPath, SignScripts { } } -Task Analyze -depends Build -requiredVariables CodeAnalysisStop, OutDir { +Task Analyze -depends BuildImpl -requiredVariables ScriptAnalysisAction, OutDir { if ((Get-Host).Name -in $SkipScriptAnalysisHost) { $ScriptAnalysisAction = 'Skip' } @@ -229,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 { From 743e289495d29fbef2570c5753ed9908e2ab1c14 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sun, 9 Oct 2016 11:30:11 +0100 Subject: [PATCH 09/10] Add PSScriptAnalyzer module to appveyor config --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 41831d3..0964252 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,6 +10,7 @@ install: Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null Install-Module Pester -MinimumVersion 3.4.0 -Scope CurrentUser -Force | Out-Null Install-Module psake -Scope CurrentUser -Force | Out-Null + Install-Module PSScriptAnalyzer -Scope CurrentUser -Force | Out-Null build_script: - ps: | From 248bfe5050b3952abdd24e661f754b73379df0e0 Mon Sep 17 00:00:00 2001 From: Dave Green Date: Sun, 9 Oct 2016 18:43:56 +0100 Subject: [PATCH 10/10] Remove extended character set symbols --- src/InvokePlaster.ps1 | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/InvokePlaster.ps1 b/src/InvokePlaster.ps1 index 70670fd..081470d 100644 --- a/src/InvokePlaster.ps1 +++ b/src/InvokePlaster.ps1 @@ -1,4 +1,4 @@ -<# +<# NOTE TO DEVELOPERS: All text displayed to the user except for Write-Debug (or $PSCmdlet.WriteDebug()) text must be added to the string tables in: @@ -712,7 +712,7 @@ function Invoke-Plaster { # Copy over empty directories - if any. $gciParams.Remove('File') $gciParams['Directory'] = $true - $dirs = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams | + $dirs = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams | Where-Object {$_.GetFileSystemInfos().Length -eq 0}) foreach ($dir in $dirs) { $dirSrcPath = $dir.FullName @@ -1116,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