Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@

<Target Name="StaticAnalysisExample" Condition="'$(RunStaticAnalysis)' == 'true'" DependsOnTargets="Build" AfterTargets="StaticAnalysisHelp">
<Message Importance="high" Text="Running static analysis for PowerShell examples..." />
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;. $(RepoTools)/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 -MarkdownPaths $(RepoArtifacts)/FilesChanged.txt -RulePaths $(RepoTools)/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/*.psm1 -AnalyzeScriptsInFile -OutputScriptsInFile &quot;"/>
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;. $(RepoTools)/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1 -MarkdownPaths $(RepoArtifacts)/FilesChanged.txt -RulePaths $(RepoTools)/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/*.psm1 &quot;"/>
</Target>

<Target Name="StaticAnalysis" DependsOnTargets="StaticAnalysisBreakingChange;StaticAnalysisDependency;StaticAnalysisSignature;StaticAnalysisHelp;StaticAnalysisExample">
Expand Down
52 changes: 34 additions & 18 deletions tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<#
.SYNOPSIS
The script to find examples in ".md" and analyze the examples by custom rules.
.PARAMETER MarkdownPaths
Markdown searching paths. Empty for current path. Supports wildcard.
.PARAMETER ScriptPath
PowerShell script searching path.
.PARAMETER RulePaths
PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard.
.PARAMETER CodeMapPath
Code map path bound with the PowerShell script.
.PARAMETER Recurse
To search markdowns recursively in the folders.
.PARAMETER IncludeDefaultRules
To analyze default rules provided by PSScriptAnalyzer.
.PARAMETER OutputFolder
Folder path storing output files.
.PARAMETER SkipAnalyzing
To skip analyzing step. Only extracting example codes from markdowns to the temp script.
.PARAMETER CleanScripts
To clean the temp script.
.NOTES
File Name: Measure-MarkdownOrScript.ps1
#>
Expand All @@ -9,20 +27,21 @@

[CmdletBinding(DefaultParameterSetName = "Markdown")]
param (
[Parameter(Mandatory, HelpMessage = "Markdown searching paths. Empty for current path. Supports wildcard.", ParameterSetName = "Markdown")]
[Parameter(Mandatory, ParameterSetName = "Markdown")]
[AllowEmptyString()]
[string[]]$MarkdownPaths,
[Parameter(Mandatory, HelpMessage = "PowerShell scripts searching paths. Empty for current path. Supports wildcard.", ParameterSetName = "Script")]
[Parameter(Mandatory, ParameterSetName = "Script")]
[AllowEmptyString()]
[string[]]$ScriptPaths,
[Parameter(HelpMessage = "PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard.")]
[string[]]$ScriptPath,
[string[]]$RulePaths,
[Parameter(Mandatory, ParameterSetName = "Script")]
[string]$CodeMapPath,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code map should be transparent to users so it should be optional.
As a user, I only want to know the error is on which line. I don't care where the code map file is.

[Parameter(ParameterSetName = "Markdown")]
[switch]$Recurse,
[switch]$IncludeDefaultRules,
[string]$OutputFolder = "$PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleAnalysis",
[switch]$AnalyzeScriptsInFile,
[switch]$OutputScriptsInFile,
[Parameter(ParameterSetName = "Markdown")]
[switch]$SkipAnalyzing,
[switch]$CleanScripts
)

Expand All @@ -35,12 +54,10 @@ $totalLine = 1
# Find examples in "help\*.md", output ".ps1"
if ($PSCmdlet.ParameterSetName -eq "Markdown") {
# Clean caches, remove files in "output" folder
if ($OutputScriptsInFile.IsPresent) {
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction SilentlyContinue
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction SilentlyContinue
Remove-Item $PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv -ErrorAction SilentlyContinue
Remove-Item $OutputFolder -ErrorAction SilentlyContinue
}
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction SilentlyContinue
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction SilentlyContinue
Remove-Item $PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv -ErrorAction SilentlyContinue
Remove-Item $OutputFolder -ErrorAction SilentlyContinue
$null = New-Item -ItemType Directory -Path $OutputFolder -ErrorAction SilentlyContinue
$null = New-Item -ItemType File $OutputFolder\TempScript.ps1
# When the input $MarkdownPaths is the path of txt file
Expand All @@ -66,7 +83,6 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
}
$cmdlet = $_.BaseName
$result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName `
-OutputScriptsInFile:$OutputScriptsInFile.IsPresent `
-OutputFolder $OutputFolder `
-TotalLine $totalLine
$analysisResultsTable += $result.Errors
Expand All @@ -75,19 +91,19 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
}
}
$codeMap| Export-Csv "$OutputFolder\TempCodeMap.csv" -NoTypeInformation
if ($AnalyzeScriptsInFile.IsPresent) {
$ScriptPaths = "$OutputFolder\TempScript.ps1"
if (!$SkipAnalyzing.IsPresent) {
$ScriptPath = "$OutputFolder\TempScript.ps1"
$CodeMapPath = "$OutputFolder\TempCodeMap.csv"
}
}

# Analyze scripts
if ($PSCmdlet.ParameterSetName -eq "Script" -or $AnalyzeScriptsInFile.IsPresent) {
if ($PSCmdlet.ParameterSetName -eq "Script" -or !$SkipAnalyzing.IsPresent) {
# Read code map from file
$codeMap = Import-Csv $CodeMapPath
# Read and analyze ".ps1" in \ScriptsByExample
Write-Output "Analyzing file ..."
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent $codeMap -ErrorAction Continue
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPath) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent $codeMap -ErrorAction Continue

# Summarize analysis results, output in Result.csv
if($analysisResultsTable){
Expand All @@ -97,5 +113,5 @@ if ($PSCmdlet.ParameterSetName -eq "Script" -or $AnalyzeScriptsInFile.IsPresent)

# Clean caches
if ($CleanScripts.IsPresent) {
Remove-Item $ScriptPaths -Exclude *.csv -Recurse -ErrorAction Continue
Remove-Item $ScriptPath -Exclude *.csv -Recurse -ErrorAction Continue
}
3 changes: 1 addition & 2 deletions tools/StaticAnalysis/ExampleAnalyzer/utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ function Measure-SectionMissingAndOutputScript {
[string]$Module,
[string]$Cmdlet,
[string]$MarkdownPath,
[switch]$OutputScriptsInFile,
[string]$OutputFolder,
[int]$TotalLine
)
Expand Down Expand Up @@ -360,7 +359,7 @@ function Measure-SectionMissingAndOutputScript {


# Output example codes to "TempScript.ps1"
if ($OutputScriptsInFile.IsPresent -and $missingExampleCode -eq 0) {
if ($missingExampleCode -eq 0) {
$cmdletExamplesScriptPath = "$OutputFolder\TempScript.ps1"
$line = $exampleCodes.Count
if($line -ne 0){
Expand Down