|
| 1 | +param( |
| 2 | + [Parameter(Mandatory=$true)][string] $InputPath, # Full path to directory where Symbols.NuGet packages to be checked are stored |
| 3 | + [Parameter(Mandatory=$true)][string] $ExtractPath, # Full path to directory where the packages will be extracted during validation |
| 4 | + [Parameter(Mandatory=$true)][string] $SourceLinkToolPath, # Full path to directory where dotnet SourceLink CLI was installed |
| 5 | + [Parameter(Mandatory=$true)][string] $GHRepoName, # GitHub name of the repo including the Org. E.g., dotnet/arcade |
| 6 | + [Parameter(Mandatory=$true)][string] $GHCommit # GitHub commit SHA used to build the packages |
| 7 | +) |
| 8 | + |
| 9 | +# Cache/HashMap (File -> Exist flag) used to consult whether a file exist |
| 10 | +# in the repository at a specific commit point. This is populated by inserting |
| 11 | +# all files present in the repo at a specific commit point. |
| 12 | +$global:RepoFiles = @{} |
| 13 | + |
| 14 | +$ValidatePackage = { |
| 15 | + param( |
| 16 | + [string] $PackagePath # Full path to a Symbols.NuGet package |
| 17 | + ) |
| 18 | + |
| 19 | + # Ensure input file exist |
| 20 | + if (!(Test-Path $PackagePath)) { |
| 21 | + throw "Input file does not exist: $PackagePath" |
| 22 | + } |
| 23 | + |
| 24 | + # Extensions for which we'll look for SourceLink information |
| 25 | + # For now we'll only care about Portable & Embedded PDBs |
| 26 | + $RelevantExtensions = @(".dll", ".exe", ".pdb") |
| 27 | + |
| 28 | + Write-Host -NoNewLine "Validating" ([System.IO.Path]::GetFileName($PackagePath)) "... " |
| 29 | + |
| 30 | + $PackageId = [System.IO.Path]::GetFileNameWithoutExtension($PackagePath) |
| 31 | + $ExtractPath = Join-Path -Path $using:ExtractPath -ChildPath $PackageId |
| 32 | + $FailedFiles = 0 |
| 33 | + |
| 34 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 35 | + |
| 36 | + [System.IO.Directory]::CreateDirectory($ExtractPath); |
| 37 | + |
| 38 | + $zip = [System.IO.Compression.ZipFile]::OpenRead($PackagePath) |
| 39 | + |
| 40 | + $zip.Entries | |
| 41 | + Where-Object {$RelevantExtensions -contains [System.IO.Path]::GetExtension($_.Name)} | |
| 42 | + ForEach-Object { |
| 43 | + $FileName = $_.FullName |
| 44 | + $Extension = [System.IO.Path]::GetExtension($_.Name) |
| 45 | + $FakeName = -Join((New-Guid), $Extension) |
| 46 | + $TargetFile = Join-Path -Path $ExtractPath -ChildPath $FakeName |
| 47 | + |
| 48 | + # We ignore resource DLLs |
| 49 | + if ($FileName.EndsWith(".resources.dll")) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $TargetFile, $true) |
| 54 | + |
| 55 | + $ValidateFile = { |
| 56 | + param( |
| 57 | + [string] $FullPath, # Full path to the module that has to be checked |
| 58 | + [string] $RealPath, |
| 59 | + [ref] $FailedFiles |
| 60 | + ) |
| 61 | + |
| 62 | + # Makes easier to reference `sourcelink cli` |
| 63 | + Push-Location $using:SourceLinkToolPath |
| 64 | + |
| 65 | + $SourceLinkInfos = .\sourcelink.exe print-urls $FullPath | Out-String |
| 66 | + |
| 67 | + if ($LASTEXITCODE -eq 0 -and -not ([string]::IsNullOrEmpty($SourceLinkInfos))) { |
| 68 | + $NumFailedLinks = 0 |
| 69 | + |
| 70 | + # We only care about Http addresses |
| 71 | + $Matches = (Select-String '(http[s]?)(:\/\/)([^\s,]+)' -Input $SourceLinkInfos -AllMatches).Matches |
| 72 | + |
| 73 | + if ($Matches.Count -ne 0) { |
| 74 | + $Matches.Value | |
| 75 | + ForEach-Object { |
| 76 | + $Link = $_ |
| 77 | + $CommitUrl = -Join("https://raw.githubusercontent.com/", $using:GHRepoName, "/", $using:GHCommit, "/") |
| 78 | + $FilePath = $Link.Replace($CommitUrl, "") |
| 79 | + $Status = 200 |
| 80 | + $Cache = $using:RepoFiles |
| 81 | + |
| 82 | + if ( !($Cache.ContainsKey($FilePath)) ) { |
| 83 | + try { |
| 84 | + $Uri = $Link -as [System.URI] |
| 85 | + |
| 86 | + # Only GitHub links are valid |
| 87 | + if ($Uri.AbsoluteURI -ne $null -and $Uri.Host -match "github") { |
| 88 | + $Status = (Invoke-WebRequest -Uri $Link -UseBasicParsing -Method HEAD -TimeoutSec 5).StatusCode |
| 89 | + } |
| 90 | + else { |
| 91 | + $Status = 0 |
| 92 | + } |
| 93 | + } |
| 94 | + catch { |
| 95 | + $Status = 0 |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if ($Status -ne 200) { |
| 100 | + if ($NumFailedLinks -eq 0) { |
| 101 | + if ($FailedFiles.Value -eq 0) { |
| 102 | + Write-Host |
| 103 | + } |
| 104 | + |
| 105 | + Write-Host "`tFile $RealPath has broken links:" |
| 106 | + } |
| 107 | + |
| 108 | + Write-Host "`t`tFailed to retrieve $Link" |
| 109 | + |
| 110 | + $NumFailedLinks++ |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if ($NumFailedLinks -ne 0) { |
| 116 | + $FailedFiles.value++ |
| 117 | + $global:LASTEXITCODE = 1 |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Pop-Location |
| 122 | + } |
| 123 | + |
| 124 | + &$ValidateFile $TargetFile $FileName ([ref]$FailedFiles) |
| 125 | + } |
| 126 | + |
| 127 | + $zip.Dispose() |
| 128 | + |
| 129 | + if ($FailedFiles -eq 0) { |
| 130 | + Write-Host "Passed." |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function ValidateSourceLinkLinks { |
| 135 | + if (!($GHRepoName -Match "^[^\s\/]+/[^\s\/]+$")) { |
| 136 | + Write-Host "GHRepoName should be in the format <org>/<repo>" |
| 137 | + $global:LASTEXITCODE = 1 |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + if (!($GHCommit -Match "^[0-9a-fA-F]{40}$")) { |
| 142 | + Write-Host "GHCommit should be a 40 chars hexadecimal string" |
| 143 | + $global:LASTEXITCODE = 1 |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + $RepoTreeURL = -Join("https://api.github.com/repos/", $GHRepoName, "/git/trees/", $GHCommit, "?recursive=1") |
| 148 | + $CodeExtensions = @(".cs", ".vb", ".fs", ".fsi", ".fsx", ".fsscript") |
| 149 | + |
| 150 | + try { |
| 151 | + # Retrieve the list of files in the repo at that particular commit point and store them in the RepoFiles hash |
| 152 | + $Data = Invoke-WebRequest $RepoTreeURL | ConvertFrom-Json | Select-Object -ExpandProperty tree |
| 153 | + |
| 154 | + foreach ($file in $Data) { |
| 155 | + $Extension = [System.IO.Path]::GetExtension($file.path) |
| 156 | + |
| 157 | + if ($CodeExtensions.Contains($Extension)) { |
| 158 | + $RepoFiles[$file.path] = 1 |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + catch { |
| 163 | + Write-Host "Problems downloading the list of files from the repo. Url used: $RepoTreeURL" |
| 164 | + $global:LASTEXITCODE = 1 |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + if (Test-Path $ExtractPath) { |
| 169 | + Remove-Item $ExtractPath -Force -Recurse -ErrorAction SilentlyContinue |
| 170 | + } |
| 171 | + |
| 172 | + # Process each NuGet package in parallel |
| 173 | + $Jobs = @() |
| 174 | + Get-ChildItem "$InputPath\*.symbols.nupkg" | |
| 175 | + ForEach-Object { |
| 176 | + $Jobs += Start-Job -ScriptBlock $ValidatePackage -ArgumentList $_.FullName |
| 177 | + } |
| 178 | + |
| 179 | + foreach ($Job in $Jobs) { |
| 180 | + Wait-Job -Id $Job.Id | Receive-Job |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +Measure-Command { ValidateSourceLinkLinks } |
0 commit comments