Skip to content

Commit 3648280

Browse files
authored
[CI Example Analyzer] Redesign function syntax (Azure#19171)
* revise parameters * fix help messages
1 parent 7735306 commit 3648280

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266

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

272272
<Target Name="StaticAnalysis" DependsOnTargets="StaticAnalysisBreakingChange;StaticAnalysisDependency;StaticAnalysisSignature;StaticAnalysisHelp;StaticAnalysisExample">

tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<#
22
.SYNOPSIS
33
The script to find examples in ".md" and analyze the examples by custom rules.
4+
.PARAMETER MarkdownPaths
5+
Markdown searching paths. Empty for current path. Supports wildcard.
6+
.PARAMETER ScriptPath
7+
PowerShell script searching path.
8+
.PARAMETER RulePaths
9+
PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard.
10+
.PARAMETER CodeMapPath
11+
Code map path bound with the PowerShell script.
12+
.PARAMETER Recurse
13+
To search markdowns recursively in the folders.
14+
.PARAMETER IncludeDefaultRules
15+
To analyze default rules provided by PSScriptAnalyzer.
16+
.PARAMETER OutputFolder
17+
Folder path storing output files.
18+
.PARAMETER SkipAnalyzing
19+
To skip analyzing step. Only extracting example codes from markdowns to the temp script.
20+
.PARAMETER CleanScripts
21+
To clean the temp script.
422
.NOTES
523
File Name: Measure-MarkdownOrScript.ps1
624
#>
@@ -9,20 +27,21 @@
927

1028
[CmdletBinding(DefaultParameterSetName = "Markdown")]
1129
param (
12-
[Parameter(Mandatory, HelpMessage = "Markdown searching paths. Empty for current path. Supports wildcard.", ParameterSetName = "Markdown")]
30+
[Parameter(Mandatory, ParameterSetName = "Markdown")]
1331
[AllowEmptyString()]
1432
[string[]]$MarkdownPaths,
15-
[Parameter(Mandatory, HelpMessage = "PowerShell scripts searching paths. Empty for current path. Supports wildcard.", ParameterSetName = "Script")]
33+
[Parameter(Mandatory, ParameterSetName = "Script")]
1634
[AllowEmptyString()]
17-
[string[]]$ScriptPaths,
18-
[Parameter(HelpMessage = "PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard.")]
35+
[string[]]$ScriptPath,
1936
[string[]]$RulePaths,
37+
[Parameter(Mandatory, ParameterSetName = "Script")]
2038
[string]$CodeMapPath,
39+
[Parameter(ParameterSetName = "Markdown")]
2140
[switch]$Recurse,
2241
[switch]$IncludeDefaultRules,
2342
[string]$OutputFolder = "$PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleAnalysis",
24-
[switch]$AnalyzeScriptsInFile,
25-
[switch]$OutputScriptsInFile,
43+
[Parameter(ParameterSetName = "Markdown")]
44+
[switch]$SkipAnalyzing,
2645
[switch]$CleanScripts
2746
)
2847

@@ -35,12 +54,10 @@ $totalLine = 1
3554
# Find examples in "help\*.md", output ".ps1"
3655
if ($PSCmdlet.ParameterSetName -eq "Markdown") {
3756
# Clean caches, remove files in "output" folder
38-
if ($OutputScriptsInFile.IsPresent) {
39-
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction SilentlyContinue
40-
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction SilentlyContinue
41-
Remove-Item $PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv -ErrorAction SilentlyContinue
42-
Remove-Item $OutputFolder -ErrorAction SilentlyContinue
43-
}
57+
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction SilentlyContinue
58+
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction SilentlyContinue
59+
Remove-Item $PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv -ErrorAction SilentlyContinue
60+
Remove-Item $OutputFolder -ErrorAction SilentlyContinue
4461
$null = New-Item -ItemType Directory -Path $OutputFolder -ErrorAction SilentlyContinue
4562
$null = New-Item -ItemType File $OutputFolder\TempScript.ps1
4663
# When the input $MarkdownPaths is the path of txt file
@@ -66,7 +83,6 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
6683
}
6784
$cmdlet = $_.BaseName
6885
$result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName `
69-
-OutputScriptsInFile:$OutputScriptsInFile.IsPresent `
7086
-OutputFolder $OutputFolder `
7187
-TotalLine $totalLine
7288
$analysisResultsTable += $result.Errors
@@ -75,19 +91,19 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
7591
}
7692
}
7793
$codeMap| Export-Csv "$OutputFolder\TempCodeMap.csv" -NoTypeInformation
78-
if ($AnalyzeScriptsInFile.IsPresent) {
79-
$ScriptPaths = "$OutputFolder\TempScript.ps1"
94+
if (!$SkipAnalyzing.IsPresent) {
95+
$ScriptPath = "$OutputFolder\TempScript.ps1"
8096
$CodeMapPath = "$OutputFolder\TempCodeMap.csv"
8197
}
8298
}
8399

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

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

98114
# Clean caches
99115
if ($CleanScripts.IsPresent) {
100-
Remove-Item $ScriptPaths -Exclude *.csv -Recurse -ErrorAction Continue
116+
Remove-Item $ScriptPath -Exclude *.csv -Recurse -ErrorAction Continue
101117
}

tools/StaticAnalysis/ExampleAnalyzer/utils.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ function Measure-SectionMissingAndOutputScript {
234234
[string]$Module,
235235
[string]$Cmdlet,
236236
[string]$MarkdownPath,
237-
[switch]$OutputScriptsInFile,
238237
[string]$OutputFolder,
239238
[int]$TotalLine
240239
)
@@ -360,7 +359,7 @@ function Measure-SectionMissingAndOutputScript {
360359

361360

362361
# Output example codes to "TempScript.ps1"
363-
if ($OutputScriptsInFile.IsPresent -and $missingExampleCode -eq 0) {
362+
if ($missingExampleCode -eq 0) {
364363
$cmdletExamplesScriptPath = "$OutputFolder\TempScript.ps1"
365364
$line = $exampleCodes.Count
366365
if($line -ne 0){

0 commit comments

Comments
 (0)