Debian Codegraph @

external/linker/eng/common/sdl/extract-artifact-packages.ps1

raw
1 param(
2 [Parameter(Mandatory=$true)][string] $InputPath, # Full path to directory where artifact packages are stored
3 [Parameter(Mandatory=$true)][string] $ExtractPath # Full path to directory where the packages will be extracted
4 )
5
6 $ErrorActionPreference = 'Stop'
7 Set-StrictMode -Version 2.0
8
9 # `tools.ps1` checks $ci to perform some actions. Since the post-build
10 # scripts don't necessarily execute in the same agent that run the
11 # build.ps1/sh script this variable isn't automatically set.
12 $ci = $true
13 $disableConfigureToolsetImport = $true
14
15 function ExtractArtifacts {
16 if (!(Test-Path $InputPath)) {
17 Write-Host "Input Path does not exist: $InputPath"
18 ExitWithExitCode 0
19 }
20 $Jobs = @()
21 Get-ChildItem "$InputPath\*.nupkg" |
22 ForEach-Object {
23 $Jobs += Start-Job -ScriptBlock $ExtractPackage -ArgumentList $_.FullName
24 }
25
26 foreach ($Job in $Jobs) {
27 Wait-Job -Id $Job.Id | Receive-Job
28 }
29 }
30
31 try {
32 . $PSScriptRoot\..\tools.ps1
33
34 $ExtractPackage = {
35 param(
36 [string] $PackagePath # Full path to a NuGet package
37 )
38
39 if (!(Test-Path $PackagePath)) {
40 Write-PipelineTelemetryError -Category 'Build' -Message "Input file does not exist: $PackagePath"
41 ExitWithExitCode 1
42 }
43
44 $RelevantExtensions = @('.dll', '.exe', '.pdb')
45 Write-Host -NoNewLine 'Extracting ' ([System.IO.Path]::GetFileName($PackagePath)) '...'
46
47 $PackageId = [System.IO.Path]::GetFileNameWithoutExtension($PackagePath)
48 $ExtractPath = Join-Path -Path $using:ExtractPath -ChildPath $PackageId
49
50 Add-Type -AssemblyName System.IO.Compression.FileSystem
51
52 [System.IO.Directory]::CreateDirectory($ExtractPath);
53
54 try {
55 $zip = [System.IO.Compression.ZipFile]::OpenRead($PackagePath)
56
57 $zip.Entries |
58 Where-Object {$RelevantExtensions -contains [System.IO.Path]::GetExtension($_.Name)} |
59 ForEach-Object {
60 $TargetFile = Join-Path -Path $ExtractPath -ChildPath $_.Name
61
62 [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $TargetFile, $true)
63 }
64 }
65 catch {
66 Write-Host $_.ScriptStackTrace
67 Write-PipelineTelemetryError -Force -Category 'Sdl' -Message $_
68 ExitWithExitCode 1
69 }
70 finally {
71 $zip.Dispose()
72 }
73 }
74 Measure-Command { ExtractArtifacts }
75 }
76 catch {
77 Write-Host $_.ScriptStackTrace
78 Write-PipelineTelemetryError -Force -Category 'Sdl' -Message $_
79 ExitWithExitCode 1
80 }