4
4
. PARAMETER MarkdownPaths
5
5
Markdown searching paths. Empty for current path. Supports wildcard.
6
6
. PARAMETER ScriptPath
7
- PowerShell script searching path.
7
+ PowerShell script searching paths. Empty for current path. Supports wildcard .
8
8
. PARAMETER RulePaths
9
9
PSScriptAnalyzer custom rules paths. Empty for current path. Supports wildcard.
10
- . PARAMETER CodeMapPath
11
- Code map path bound with the PowerShell script.
12
10
. PARAMETER Recurse
13
- To search markdowns recursively in the folders.
11
+ To search files recursively in the folders.
14
12
. PARAMETER IncludeDefaultRules
15
13
To analyze default rules provided by PSScriptAnalyzer.
16
14
. PARAMETER OutputFolder
17
15
Folder path storing output files.
18
16
. PARAMETER SkipAnalyzing
19
17
To skip analyzing step. Only extracting example codes from markdowns to the temp script.
20
- . PARAMETER CleanScripts
21
- To clean the temp script.
18
+ . PARAMETER NotCleanScripts
19
+ Do not clean the temp script.
22
20
. NOTES
23
21
File Name: Measure-MarkdownOrScript.ps1
24
22
#>
@@ -32,17 +30,14 @@ param (
32
30
[string []]$MarkdownPaths ,
33
31
[Parameter (Mandatory , ParameterSetName = " Script" )]
34
32
[AllowEmptyString ()]
35
- [string []]$ScriptPath ,
33
+ [string []]$ScriptPaths ,
36
34
[string []]$RulePaths ,
37
- [Parameter (Mandatory , ParameterSetName = " Script" )]
38
- [string ]$CodeMapPath ,
39
- [Parameter (ParameterSetName = " Markdown" )]
40
35
[switch ]$Recurse ,
41
36
[switch ]$IncludeDefaultRules ,
42
37
[string ]$OutputFolder = " $PSScriptRoot \..\..\..\artifacts\StaticAnalysisResults\ExampleAnalysis" ,
43
38
[Parameter (ParameterSetName = " Markdown" )]
44
39
[switch ]$SkipAnalyzing ,
45
- [switch ]$CleanScripts
40
+ [switch ]$NotCleanScripts
46
41
)
47
42
48
43
. $PSScriptRoot \utils.ps1
@@ -51,17 +46,24 @@ $analysisResultsTable = @()
51
46
$codeMap = @ ()
52
47
$totalLine = 1
53
48
49
+ $tempScript = " TempScript.ps1"
50
+ $tempScriptMap = " TempScript.Map.csv"
51
+ $TempScriptPath = " $OutputFolder \$tempScript "
52
+ $TempScriptMapPath = " $OutputFolder \$tempScriptMap "
53
+
54
+ # Clean caches, remove files in "output" folder
55
+ Remove-Item $TempScriptPath - ErrorAction SilentlyContinue
56
+ Remove-Item $TempScriptMapPath - ErrorAction SilentlyContinue
57
+ Remove-Item $PSScriptRoot \..\..\..\artifacts\StaticAnalysisResults\ExampleIssues.csv - ErrorAction SilentlyContinue
58
+ Remove-Item $OutputFolder - ErrorAction SilentlyContinue
59
+ # Create output folder and temp script
60
+ $null = New-Item - ItemType Directory - Path $OutputFolder - ErrorAction SilentlyContinue
61
+ $null = New-Item - ItemType File $TempScriptPath
62
+
54
63
# Find examples in "help\*.md", output ".ps1"
55
64
if ($PSCmdlet.ParameterSetName -eq " Markdown" ) {
56
- # Clean caches, remove files in "output" folder
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
61
- $null = New-Item - ItemType Directory - Path $OutputFolder - ErrorAction SilentlyContinue
62
- $null = New-Item - ItemType File $OutputFolder \TempScript.ps1
63
- # When the input $MarkdownPaths is the path of txt file
64
- if ($MarkdownPaths -cmatch " .*\.txt" ) {
65
+ # When the input $MarkdownPaths is the path of txt file contained markdown paths
66
+ if ((Test-Path $MarkdownPaths - PathType Leaf) -and $MarkdownPaths.EndsWith (" .txt" )) {
65
67
$MarkdownPath = Get-Content $MarkdownPaths
66
68
}
67
69
# When the input $MarkdownPaths is the path of a folder
@@ -83,27 +85,29 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
83
85
}
84
86
$cmdlet = $_.BaseName
85
87
$result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName `
86
- - OutputFolder $OutputFolder `
88
+ - TempScriptPath $TempScriptPath `
87
89
- TotalLine $totalLine
88
90
$analysisResultsTable += $result.Errors
89
91
$codeMap += $result.CodeMap
90
92
$totalLine = $result.TotalLine
91
93
}
92
94
}
93
- $codeMap | Export-Csv " $OutputFolder \TempCodeMap.csv" - NoTypeInformation
94
- if (! $SkipAnalyzing.IsPresent ) {
95
- $ScriptPath = " $OutputFolder \TempScript.ps1"
96
- $CodeMapPath = " $OutputFolder \TempCodeMap.csv"
95
+ if (! $NotCleanScripts.IsPresent ){
96
+ $codeMap | Export-Csv $TempScriptMapPath - NoTypeInformation
97
97
}
98
98
}
99
99
100
100
# Analyze scripts
101
101
if ($PSCmdlet.ParameterSetName -eq " Script" -or ! $SkipAnalyzing.IsPresent ) {
102
- # Read code map from file
103
- $codeMap = Import-Csv $CodeMapPath
102
+ if ($PSCmdlet.ParameterSetName -eq " Script" ){
103
+ $codeMap = Merge-Scripts - ScriptPaths $ScriptPaths - Recurse:$Recurse.IsPresent - TempScriptPath $TempScriptPath
104
+ if (! $NotCleanScripts.IsPresent ){
105
+ $codeMap | Export-Csv $TempScriptMapPath - NoTypeInformation
106
+ }
107
+ }
104
108
# Read and analyze ".ps1" in \ScriptsByExample
105
109
Write-Output " Analyzing file ..."
106
- $analysisResultsTable += Get-ScriptAnalyzerResult ( Get-Item - Path $ScriptPath ) $ RulePaths - IncludeDefaultRules:$IncludeDefaultRules.IsPresent $codeMap - ErrorAction Continue
110
+ $analysisResultsTable += Get-ScriptAnalyzerResult - ScriptPath $TempScriptPath - RulePaths $ RulePaths - IncludeDefaultRules:$IncludeDefaultRules.IsPresent - CodeMap $codeMap - ErrorAction Continue
107
111
108
112
# Summarize analysis results, output in Result.csv
109
113
if ($analysisResultsTable ){
@@ -112,6 +116,7 @@ if ($PSCmdlet.ParameterSetName -eq "Script" -or !$SkipAnalyzing.IsPresent) {
112
116
}
113
117
114
118
# Clean caches
115
- if ($CleanScripts.IsPresent ) {
116
- Remove-Item $ScriptPath - Exclude * .csv - Recurse - ErrorAction Continue
119
+ if (! $NotCleanScripts.IsPresent ) {
120
+ Remove-Item $TempScriptPath - ErrorAction Continue
121
+ Remove-Item $OutputFolder - ErrorAction SilentlyContinue
117
122
}
0 commit comments