-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CI Example Analyzer] Support analysis for any PowerShell script #19191
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,13 @@ | |
.PARAMETER MarkdownPaths | ||
Markdown searching paths. Empty for current path. Supports wildcard. | ||
.PARAMETER ScriptPath | ||
PowerShell script searching path. | ||
PowerShell script searching paths. Empty for current path. Supports wildcard. | ||
.PARAMETER RulePaths | ||
PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard. | ||
.PARAMETER CodeMapPath | ||
Code map path bound with the PowerShell script. | ||
.PARAMETER Recurse | ||
.PARAMETER MarkdownRecurse | ||
To search markdowns recursively in the folders. | ||
.PARAMETER ScriptRecurse | ||
To search scripts recursively in the folders. | ||
.PARAMETER IncludeDefaultRules | ||
To analyze default rules provided by PSScriptAnalyzer. | ||
.PARAMETER OutputFolder | ||
|
@@ -32,12 +32,12 @@ param ( | |
[string[]]$MarkdownPaths, | ||
[Parameter(Mandatory, ParameterSetName = "Script")] | ||
[AllowEmptyString()] | ||
[string[]]$ScriptPath, | ||
[string[]]$ScriptPaths, | ||
[string[]]$RulePaths, | ||
[Parameter(Mandatory, ParameterSetName = "Script")] | ||
[string]$CodeMapPath, | ||
[Parameter(ParameterSetName = "Markdown")] | ||
[switch]$Recurse, | ||
[switch]$MarkdownRecurse, | ||
[Parameter(ParameterSetName = "Script")] | ||
[switch]$ScriptRecurse, | ||
[switch]$IncludeDefaultRules, | ||
[string]$OutputFolder = "$PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleAnalysis", | ||
[Parameter(ParameterSetName = "Markdown")] | ||
|
@@ -51,24 +51,26 @@ $analysisResultsTable = @() | |
$codeMap = @() | ||
$totalLine = 1 | ||
|
||
# Clean caches, remove files in "output" folder | ||
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction SilentlyContinue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please introduce a constant for "TempScript.ps1" and "TempCodeMap.csv" |
||
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction SilentlyContinue | ||
Remove-Item $PSScriptRoot\..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv -ErrorAction SilentlyContinue | ||
Remove-Item $OutputFolder -ErrorAction SilentlyContinue | ||
# Create output folder and temp script | ||
$null = New-Item -ItemType Directory -Path $OutputFolder -ErrorAction SilentlyContinue | ||
$null = New-Item -ItemType File $OutputFolder\TempScript.ps1 | ||
|
||
# Find examples in "help\*.md", output ".ps1" | ||
if ($PSCmdlet.ParameterSetName -eq "Markdown") { | ||
# Clean caches, remove files in "output" folder | ||
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 | ||
# When the input $MarkdownPaths is the path of txt file contained markdown paths | ||
if ($MarkdownPaths -cmatch ".*\.txt") { | ||
$MarkdownPath = Get-Content $MarkdownPaths | ||
} | ||
# When the input $MarkdownPaths is the path of a folder | ||
else{ | ||
$MarkdownPath = $MarkdownPaths | ||
} | ||
foreach($_ in Get-ChildItem $MarkdownPath -Recurse:$Recurse.IsPresent){ | ||
foreach($_ in Get-ChildItem $MarkdownPath -Recurse:$MarkdownRecurse.IsPresent){ | ||
# Filter the .md of overview in "\help\" | ||
if ((Get-Item -Path $_.FullName).Directory.Name -eq "help" -and $_.FullName -cmatch ".*\.md" -and $_.BaseName -cmatch "^[A-Z][a-z]+-([A-Z][a-z0-9]*)+$") { | ||
if((Get-Item -Path $_.FullName).Directory.Parent.Name -eq "netcoreapp3.1"){ | ||
|
@@ -91,19 +93,18 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") { | |
} | ||
} | ||
$codeMap| Export-Csv "$OutputFolder\TempCodeMap.csv" -NoTypeInformation | ||
if (!$SkipAnalyzing.IsPresent) { | ||
$ScriptPath = "$OutputFolder\TempScript.ps1" | ||
$CodeMapPath = "$OutputFolder\TempCodeMap.csv" | ||
} | ||
} | ||
|
||
# Analyze scripts | ||
if ($PSCmdlet.ParameterSetName -eq "Script" -or !$SkipAnalyzing.IsPresent) { | ||
# Read code map from file | ||
$codeMap = Import-Csv $CodeMapPath | ||
$TempScriptPath = "$OutputFolder\TempScript.ps1" | ||
if ($PSCmdlet.ParameterSetName -eq "Script"){ | ||
$codeMap = Set-ScriptsIntoSingleScript -ScriptPaths $ScriptPaths -Recurse:$ScriptRecurse.IsPresent -OutputFolder $OutputFolder | ||
$codeMap| Export-Csv "$OutputFolder\TempCodeMap.csv" -NoTypeInformation | ||
} | ||
# Read and analyze ".ps1" in \ScriptsByExample | ||
Write-Output "Analyzing file ..." | ||
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPath) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent $codeMap -ErrorAction Continue | ||
$analysisResultsTable += Get-ScriptAnalyzerResult -ScriptPath $TempScriptPath -RulePaths $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -CodeMap $codeMap -ErrorAction Continue | ||
|
||
# Summarize analysis results, output in Result.csv | ||
if($analysisResultsTable){ | ||
|
@@ -113,5 +114,6 @@ if ($PSCmdlet.ParameterSetName -eq "Script" -or !$SkipAnalyzing.IsPresent) { | |
|
||
# Clean caches | ||
if ($CleanScripts.IsPresent) { | ||
Remove-Item $ScriptPath -Exclude *.csv -Recurse -ErrorAction Continue | ||
Remove-Item $OutputFolder\TempScript.ps1 -ErrorAction Continue | ||
Remove-Item $OutputFolder\TempCodeMap.csv -ErrorAction Continue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use one single
-Recurse
parameter for both parameter sets?