try better script analysis publishing

This commit is contained in:
jhoneill
2019-12-08 01:35:13 +00:00
parent 469586e94a
commit 2bcb332ec7

View File

@@ -43,7 +43,7 @@ if (-not $SkipPreChecks) {
#Check files in the manifest are present
foreach ($file in $Settings.FileList) {
if (-not (Test-Path $file)) {
Write-host "##vso[task.logissue type=warning]File $file in the manifest file list is not present" -ForegroundColor yellow
"##vso[task.logissue type=warning]File $file in the manifest file list is not present"
}
}
@@ -63,44 +63,34 @@ if (-not $SkipPreChecks) {
(?(open)(?!)) # Fails if 'open' stack isn't empty
} # Functions closing '}'
"@, 57) # 57 = compile,multi-line ignore case and white space.
$reg2 = [Regex]::new(@"
^function\s*[-\w]+\s*{ # The function name and opening '{'
(
\#.*?[\r\n]+ # single line comment
| # or
\s*<\#.*?\#> # <#comment block#>
| # or
\s*\[.*?\] # [attribute tags]
)*
"@, 57) # 57 = compile, multi-line, ignore case and white space.
foreach ($file in (Get-Item .\Public\*.ps1)) {
$name = $file.name -replace(".ps1","")
if ($name -notmatch ("(\w+)-\w+")) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]$name in the public folder is not a verb-noun name"}
elseif ($Matches[1] -notin $approvedVerbs) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]$name in the public folder does not start with an approved verb"}
if ($name -notmatch ("(\w+)-\w+")) {"##vso[task.logissue type=Warning]$name in the public folder is not a verb-noun name"}
elseif ($Matches[1] -notin $approvedVerbs) {"##vso[task.logissue type=Warning]$name in the public folder does not start with an approved verb"}
if(-not ($Settings.FunctionsToExport -ceq $name)) {
Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]File $($file.name) in the public folder does not match an exported function in the manifest"
"##vso[task.logissue type=Warning]File $($file.name) in the public folder does not match an exported function in the manifest"
}
else {
$fileContent = Get-Content $file -Raw
$m = $reg.Matches($fileContent)
if ($m.Count -eq 0) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]Could not find $name function in $($file.name)"; continue}
elseif ($m.Count -ge 2) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]Multiple functions in $($item.name)"; Continue}
if ($m.Count -eq 0) {"##vso[task.logissue type=Warning]Could not find $name function in $($file.name)"; continue}
elseif ($m.Count -ge 2) {"##vso[task.logissue type=Warning]Multiple functions in $($item.name)"; Continue}
elseif ($m[0] -imatch "^\function\s" -and
$m[0] -cnotmatch "^\w+\s+$name") {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]function name does not match file name for $($file.name)"}
$m[0] -cnotmatch "^\w+\s+$name") {"##vso[task.logissue type=Warning]function name does not match file name for $($file.name)"}
#$m[0] runs form the f of function to its final } -find the section up to param, check for aliases & comment-based help
$m2 = [regex]::Match($m[0],"^.*?param",17) # 17 = multi-line, ignnore case
if (-not $m2.Success) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]function $name has no param() block"}
if (-not $m2.Success) {"##vso[task.logissue type=Warning]function $name has no param() block"}
else {
if ($m2.value -match "\[\s*Alias\(\s*.([\w-]+).\s*\)\s*\]") {
foreach ($a in ($Matches[1] -split '\s*,\s*')) {
$a = $a -replace "'","" -replace '"',''
if (-not ($Settings.AliasesToExport -eq $a)) {
Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]Function $name has alias $a which is not in the manifest"
"##vso[task.logissue type=Warning]Function $name has alias $a which is not in the manifest"
}
}
}
if ($m2.value -match "\.syopsis|\.Description|\.Example") {
Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]Function $name appears to have comment based help."
"##vso[task.logissue type=Warning]Function $name appears to have comment based help."
}
}
}
@@ -108,16 +98,14 @@ if (-not $SkipPreChecks) {
#Warn about functions which are exported but not found in public
$notFromPublic = $Settings.FunctionsToExport.where({-not (Test-Path ".\public\$_.ps1")})
If ($notFromPublic) {Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]Exported function(s) $($notFromPublic -join ', ') are not loaded from Public"}
If ($notFromPublic) {"##vso[task.logissue type=Warning]Exported function(s) $($notFromPublic -join ', ') are not loaded from Public"}
}
if ($PreCheckOnly) {return}
#region build, determine module path if necessary, create target directory if necessary, copy files based on manifest, build help
try {
Write-verbose -verbose -Message 'Module build started'
if (-not $ModulePath) {
if (-not $ModulePath) {
if ($IsLinux -or $IsMacOS) {$ModulePathSeparator = ':' }
else {$ModulePathSeparator = ';' }
@@ -186,7 +174,7 @@ if (-not $SkipPostChecks) {
}
$commands = Get-Command -Module $ModuleName -CommandType function,Cmdlet
$commands.where({$_.name -notmatch "(\w+)-\w+" -or $Matches[1] -notin $approvedVerbs}) | ForEach-Object {
Write-Host -ForegroundColor yellow "##vso[task.logissue type=Warning]$($_.name) does not meet the ApprovedVerb-Noun naming rules"
"##vso[task.logissue type=Warning]$($_.name) does not meet the ApprovedVerb-Noun naming rules"
}
$helpless = $commands | Get-Help | Where-Object {$_.Synopsis -match "^\s+$($_.name)\s+\["} | Select-Object -ExpandProperty name
foreach ($command in $helpless ) {
@@ -196,12 +184,12 @@ if (-not $SkipPostChecks) {
Install-Module -Name PSScriptAnalyzer -Force
}
Import-module -Name PSScriptAnalyzer
Write-Verbose -Verbose "Running script analyzer against '$ModulePath' "
"Running script analyzer against '{0}' " -f $ModulePath
$AnalyzerResults = Invoke-ScriptAnalyzer -Path $ModulePath -Recurse -ErrorAction SilentlyContinue
if ($AnalyzerResults) {
if (-not (Get-Module -Name ImportExcel -ListAvailable)) {
#ironically we use this to build import-excel Shouldn't need this there!
Write-Verbose -verbose 'Installing ImportExcel.'
'Installing ImportExcel.'
Install-Module -Name ImportExcel -Force
}
$chartDef = New-ExcelChartDefinition -ChartType 'BarClustered' -Column 2 -Title "Script analysis" -LegendBold
@@ -219,6 +207,7 @@ if (-not $SkipPostChecks) {
}
Remove-Item -Path $ExcelParams['Path'] -ErrorAction SilentlyContinue
$AnalyzerResults | Export-Excel @ExcelParams
"Try to uploadfile$($ExcelParams['Path'])"
"##vso[task.uploadfile]$($ExcelParams['Path'])"
}
}