From 725b909d2bff5d381281d72425dafacde9c6a94a Mon Sep 17 00:00:00 2001 From: MoChilia Date: Tue, 26 Jul 2022 17:14:58 +0800 Subject: [PATCH 1/3] fix bug for import-module --- .../AnalyzeRules/CommandName.psm1 | 56 +++++++++---------- .../AnalyzeRules/ParameterNameAndValue.psm1 | 18 ++---- .../Measure-MarkdownOrScript.ps1 | 9 ++- .../StaticAnalysis/ExampleAnalyzer/utils.ps1 | 17 ++++-- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index e85d43d6ce22..328175689e9d 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -4,6 +4,8 @@ .NOTES File: CommandName.psm1 #> +Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global + . $PSScriptRoot\..\utils.ps1 enum RuleNames { @@ -25,10 +27,6 @@ function Measure-CommandName { [System.Management.Automation.Language.ScriptBlockAst] $ScriptBlockAst ) - begin{ - $modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" - Get-Item $modulePath | Import-Module -Global - } process { $Results = @() $global:CommandParameterPair = @() @@ -52,38 +50,34 @@ function Measure-CommandName { if ($CommandAst.InvocationOperator -eq "Unknown") { $CommandName = $CommandAst.CommandElements[0].Extent.Text $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue - $ActualName = $GetCommand.Name - if ($null -eq $GetCommand) { + if($null -eq $GetCommand){ # CommandName is not valid. - # Redo import-module - if(!(Redo-ImportModule $CommandName)){ - $global:CommandParameterPair += @{ - CommandName = $CommandName - ParameterName = "" - ModuleCmdletExNum = $ModuleCmdletExNum - } - return $true + $global:CommandParameterPair += @{ + CommandName = $CommandName + ParameterName = "" + ModuleCmdletExNum = $ModuleCmdletExNum } + return $true } - else { - if ($GetCommand.CommandType -eq "Alias") { - # CommandName is an alias. - $global:CommandParameterPair += @{ - CommandName = $CommandName - ParameterName = "" - ModuleCmdletExNum = $ModuleCmdletExNum - } - return $true + + $ActualName = $GetCommand.Name + if ($GetCommand.CommandType -eq "Alias") { + # CommandName is an alias. + $global:CommandParameterPair += @{ + CommandName = $CommandName + ParameterName = "" + ModuleCmdletExNum = $ModuleCmdletExNum } - if ($CommandName -cne $ActualName) { - # CommandName doesn't follow the Capitalization Conventions. - $global:CommandParameterPair += @{ - CommandName = "$CommandName#@#$ActualName" - ParameterName = "" - ModuleCmdletExNum = $ModuleCmdletExNum - } - return $true + return $true + } + if ($CommandName -cne $ActualName) { + # CommandName doesn't follow the Capitalization Conventions. + $global:CommandParameterPair += @{ + CommandName = "$CommandName#@#$ActualName" + ParameterName = "" + ModuleCmdletExNum = $ModuleCmdletExNum } + return $true } } } diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 index 3933d737b0f3..cbf0cbf1b282 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 @@ -4,6 +4,8 @@ .NOTES File: ParameterNameAndValue.psm1 #> +Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global + . $PSScriptRoot\..\utils.ps1 enum RuleNames { @@ -348,10 +350,6 @@ function Measure-ParameterNameAndValue { [System.Management.Automation.Language.ScriptBlockAst] $ScriptBlockAst ) - begin{ - $modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" - Get-Item $modulePath | Import-Module -Global - } process { $Results = @() $global:CommandParameterPair = @() @@ -367,7 +365,6 @@ function Measure-ParameterNameAndValue { [ScriptBlock]$Predicate = { param([System.Management.Automation.Language.Ast]$Ast) $global:Ast = $Ast - if ($Ast -is [System.Management.Automation.Language.AssignmentStatementAst]) { [System.Management.Automation.Language.AssignmentStatementAst]$AssignmentStatementAst = $Ast if($AssignmentStatementAst.Left -is [System.Management.Automation.Language.ConvertExpressionAst]){ @@ -402,13 +399,8 @@ function Measure-ParameterNameAndValue { $CommandName = $CommandAst.CommandElements[0].Extent.Text $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue - - # Skip parameters for invaild cmdlet - if ($null -eq $GetCommand) { - # Redo import-module - if(!(Redo-ImportModule $CommandName)){ - return $false - } + if($null -eq $GetCommand){ + return $false } # Get command from alias if ($GetCommand.CommandType -eq "Alias") { @@ -781,4 +773,4 @@ function Measure-ParameterNameAndValue { } } -Export-ModuleMember -Function Measure-* +Export-ModuleMember -Function Measure-* \ No newline at end of file diff --git a/tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 b/tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 index 35dc9701e866..e1c983a78bde 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 @@ -55,7 +55,12 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") { continue } Write-Output "Searching in file $($_.FullName) ..." - $module = (Get-Item -Path $_.FullName).Directory.Parent.Name + if((Get-Item -Path $_.FullName).Directory.Parent.Parent.Name -ne "src"){ + $module = (Get-Item -Path $_.FullName).Directory.Parent.Parent.Name + } + else{ + $module = (Get-Item -Path $_.FullName).Directory.Parent.Name + } $cmdlet = $_.BaseName $result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName ` -OutputScriptsInFile:$OutputScriptsInFile.IsPresent ` @@ -72,7 +77,7 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") { if ($PSCmdlet.ParameterSetName -eq "Script" -or $AnalyzeScriptsInFile.IsPresent) { # Read and analyze ".ps1" in \ScriptsByExample Write-Output "Analyzing file ..." - $analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction SilentlyContinue + $analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction Continue # Summarize analysis results, output in Result.csv if($analysisResultsTable){ diff --git a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 index fded751a10ae..c4a655d555b8 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 @@ -14,6 +14,7 @@ Get-ScriptAnalyzerResult Redo-ImportModule #> +$DebugPreference = 'Continue' $SYNOPSIS_HEADING = "## SYNOPSIS" $SYNTAX_HEADING = "## SYNTAX" @@ -196,7 +197,7 @@ function Get-RecordsNotInAllowList{ return $false } # Skip NeedDeleting in Storage - if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage.Management"){ + if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage"){ return $false } return $true @@ -512,12 +513,18 @@ function Get-ScriptAnalyzerResult { #> function Redo-ImportModule { param ( - [string]$CommandName + [string]$CommandName, + [string]$moduleName ) - $modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" + $modulePath = "$PSScriptRoot\..\..\..\artifacts\Debug\$moduleName\$moduleName.psd1" + if(!(Test-Path $modulePath)){ + Write-Debug "Cannot find path $modulePath." + return $false + } Get-Item $modulePath | Import-Module -Global - $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue - if ($null -eq $GetCommand) { + $GetCommandName = Get-Command -ListImported | Where-Object {$_.Name -eq $CommandName} + if ($null -eq $GetCommandName) { + Write-Debug "$CommandName is still invalid" return $false } else{ From 954747faaee84e0379222d637765b6df2f5dae58 Mon Sep 17 00:00:00 2001 From: MoChilia Date: Tue, 26 Jul 2022 17:16:33 +0800 Subject: [PATCH 2/3] delete retry import-module --- .../StaticAnalysis/ExampleAnalyzer/utils.ps1 | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 index c4a655d555b8..601624dc512e 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 @@ -12,7 +12,6 @@ Get-RecordsNotInAllowList Measure-SectionMissingAndOutputScript Get-ScriptAnalyzerResult - Redo-ImportModule #> $DebugPreference = 'Continue' @@ -506,29 +505,3 @@ function Get-ScriptAnalyzerResult { return $results } - -<# - .SYNOPSIS - Retry import-module -#> -function Redo-ImportModule { - param ( - [string]$CommandName, - [string]$moduleName - ) - $modulePath = "$PSScriptRoot\..\..\..\artifacts\Debug\$moduleName\$moduleName.psd1" - if(!(Test-Path $modulePath)){ - Write-Debug "Cannot find path $modulePath." - return $false - } - Get-Item $modulePath | Import-Module -Global - $GetCommandName = Get-Command -ListImported | Where-Object {$_.Name -eq $CommandName} - if ($null -eq $GetCommandName) { - Write-Debug "$CommandName is still invalid" - return $false - } - else{ - Write-Debug "Succeed by retrying import-module" - return $true - } -} \ No newline at end of file From 8ca7878dabcdb1e6e523dd1c27c01abae656b9dd Mon Sep 17 00:00:00 2001 From: MoChilia Date: Wed, 27 Jul 2022 13:11:00 +0800 Subject: [PATCH 3/3] remove debug --- .../StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 | 1 + .../ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 | 1 + tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index 328175689e9d..3d0d4570318f 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -3,6 +3,7 @@ Custom rule for command name. .NOTES File: CommandName.psm1 + Import-Module should be at the beginning of the rule to avoid thread conflict. #> Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 index cbf0cbf1b282..c8a0fbdc561e 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 @@ -3,6 +3,7 @@ Custom rule for parameter name and value. .NOTES File: ParameterNameAndValue.psm1 + Import-Module should be at the beginning of the rule to avoid thread conflict. #> Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global diff --git a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 index 601624dc512e..17b66d7eb9a7 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/utils.ps1 @@ -13,7 +13,6 @@ Measure-SectionMissingAndOutputScript Get-ScriptAnalyzerResult #> -$DebugPreference = 'Continue' $SYNOPSIS_HEADING = "## SYNOPSIS" $SYNTAX_HEADING = "## SYNTAX"