From 7518d04a3a12a7fb369179b0e77d4d89c3db9b0f Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <47745270+al-cheb@users.noreply.github.com> Date: Fri, 24 Dec 2021 10:58:19 +0300 Subject: [PATCH] [Windows] Stop SoftwareReport script if error (#4801) * Stop SoftwareReport script if error * Validate blank versions * Fix tables parsing --- .../SoftwareReport.Android.psm1 | 2 +- .../SoftwareReport/SoftwareReport.Common.psm1 | 25 +++++----- .../SoftwareReport.Generator.ps1 | 6 +++ .../SoftwareReport.Helpers.psm1 | 47 +++++++++++++++++++ 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Android.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Android.psm1 index 37baf4625..9a24791a4 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Android.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Android.psm1 @@ -122,7 +122,7 @@ function Get-AndroidPlatformVersions { function Get-AndroidCommandLineToolsVersion { $commandLineTools = Get-AndroidSDKManagerPath - (& $commandLineTools --version | Out-String).Trim() -match "(?^(\d+\.){1,}\d+$)" | Out-Null + (cmd /c "$commandLineTools --version 2>NUL" | Out-String).Trim() -match "(?^(\d+\.){1,}\d+$)" | Out-Null $commandLineToolsVersion = $Matches.Version return $commandLineToolsVersion } diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 index 77099297f..68b838640 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 @@ -26,28 +26,29 @@ function Get-RustVersion { } function Get-RustupVersion { - $version = [regex]::matches($(rustup --version), "\d+\.\d+\.\d+").Value - return $version + $rustupInfo = cmd /c "rustup --version 2>NUL" + $version = [regex]::matches($rustupInfo, "\d+\.\d+\.\d+").Value + return $version } function Get-RustCargoVersion { - $version = [regex]::matches($(cargo --version), "\d+\.\d+\.\d+").Value - return $version + $version = [regex]::matches($(cargo --version), "\d+\.\d+\.\d+").Value + return $version } function Get-RustdocVersion { - $version = [regex]::matches($(rustdoc --version), "\d+\.\d+\.\d+").Value - return $version + $version = [regex]::matches($(rustdoc --version), "\d+\.\d+\.\d+").Value + return $version } function Get-RustfmtVersion { - $version = [regex]::matches($(rustfmt --version), "\d+\.\d+\.\d+").Value - return $version + $version = [regex]::matches($(rustfmt --version), "\d+\.\d+\.\d+").Value + return $version } function Get-RustClippyVersion { - $version = [regex]::matches($(cargo clippy --version), "\d+\.\d+\.\d+").Value - return $version + $version = [regex]::matches($(cargo clippy --version), "\d+\.\d+\.\d+").Value + return $version } function Get-BindgenVersion { @@ -122,10 +123,8 @@ function Get-ChocoVersion { } function Get-VcpkgVersion { - ($(vcpkg version) | Out-String) -match "version (?\d+\.\d+\.\d+)" | Out-Null - $vcpkgVersion = $Matches.Version $commitId = git -C "C:\vcpkg" rev-parse --short HEAD - return "Vcpkg $vcpkgVersion (build from master \<$commitId>)" + return "Vcpkg (build from master \<$commitId>)" } function Get-NPMVersion { diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 index 7101fea62..0df5a6b0f 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 @@ -1,3 +1,8 @@ +$global:ErrorActionPreference = "Stop" +$global:ProgressPreference = "SilentlyContinue" +$ErrorView = "NormalView" +Set-StrictMode -Version Latest + Import-Module MarkdownPS Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Android.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Browsers.psm1") -DisableNameChecking @@ -294,4 +299,5 @@ if ($cachedImages) { $markdown += $cachedImages | New-MDTable } +Test-BlankElement -Markdown $markdown $markdown | Out-File -FilePath "C:\InstalledSoftware.md" diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Helpers.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Helpers.psm1 index 0c20144ba..b62fda7eb 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Helpers.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Helpers.psm1 @@ -114,3 +114,50 @@ function Get-PathWithLink { $link = Get-LinkTarget($inputPath) return "${inputPath}${link}" } + +function Test-BlankElement { + param( + [string] $Markdown + ) + + $splitByLines = $Markdown.Split("`n") + # Validate entry without version + $blankVersions = $splitByLines -match "^-" -notmatch "(OS|Image) Version|WSL|Vcpkg|\d\." | Out-String + + # Validate tables with blank rows + $blankRows = "" + for($i = 0; $i -lt $splitByLines.Length; $i++) { + $addRows= $false + $table = @() + if ($splitByLines[$i].StartsWith("#") -and $splitByLines[$i+1].StartsWith("|")) { + $table += $splitByLines[$i,($i+1),($i+2)] + $i += 3 + $current = $splitByLines[$i] + while ($current.StartsWith("|")) { + $isBlankRow = $current.Substring(1, $current.LastIndexOf("|") - 2).Split("|").Trim() -contains "" + if ($isBlankRow) { + $table += $current + $addRows = $true + } + $current = $splitByLines[++$i] + } + if ($addRows) { + $blankRows += $table | Out-String + } + } + } + + # Display report + $isReport = $false + if ($blankVersions) { + Write-Host "Software list with blank version:`n${blankVersions}" + $isReport = $true + } + if ($blankRows) { + Write-Host "Tables with blank rows:`n${blankRows}" + $isReport = $true + } + if ($isReport) { + exit 1 + } +}