diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index e85d43d6ce22..3d0d4570318f 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -3,7 +3,10 @@ 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 + . $PSScriptRoot\..\utils.ps1 enum RuleNames { @@ -25,10 +28,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 +51,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..c8a0fbdc561e 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1 @@ -3,7 +3,10 @@ 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 + . $PSScriptRoot\..\utils.ps1 enum RuleNames { @@ -348,10 +351,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 +366,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 +400,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 +774,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..17b66d7eb9a7 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 #> $SYNOPSIS_HEADING = "## SYNOPSIS" @@ -196,7 +195,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 @@ -505,23 +504,3 @@ function Get-ScriptAnalyzerResult { return $results } - -<# - .SYNOPSIS - Retry import-module -#> -function Redo-ImportModule { - param ( - [string]$CommandName - ) - $modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" - Get-Item $modulePath | Import-Module -Global - $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue - if ($null -eq $GetCommand) { - return $false - } - else{ - Write-Debug "Succeed by retrying import-module" - return $true - } -} \ No newline at end of file