|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +param( |
| 3 | + [Parameter(Mandatory = $true)] |
| 4 | + [string]$SourcePath, |
| 5 | + [Parameter(Mandatory = $true)] |
| 6 | + [string]$DestinationPath, |
| 7 | + [switch]$RemoveOriginal |
| 8 | +) |
| 9 | + |
| 10 | +$ErrorActionPreference = 'Stop' |
| 11 | + |
| 12 | +function Get-NuGetPackageInfo { |
| 13 | + param([string]$ExtractedPath) |
| 14 | + |
| 15 | + try { |
| 16 | + # Get the folder name as fallback (this is the package ID) |
| 17 | + $packageId = Split-Path $ExtractedPath -Leaf |
| 18 | + |
| 19 | + # Try to find the .nuspec file to get the version |
| 20 | + $nuspecFile = Get-ChildItem -Path $ExtractedPath -Filter "*.nuspec" -Recurse | Select-Object -First 1 |
| 21 | + |
| 22 | + # Get the package ID and version from the .nuspec |
| 23 | + [xml]$nuspec = Get-Content $nuspecFile.FullName |
| 24 | + $packageId = $nuspec.package.metadata.id |
| 25 | + $version = $nuspec.package.metadata.version |
| 26 | + |
| 27 | + # Return folder name based on whether version contains dash |
| 28 | + if ($version.Contains('-')) { |
| 29 | + return "$packageId-Preview" |
| 30 | + } else { |
| 31 | + return "$packageId-Stable" |
| 32 | + } |
| 33 | + } |
| 34 | + catch { |
| 35 | + # If parsing fails, fall through |
| 36 | + } |
| 37 | + |
| 38 | + return $null |
| 39 | +} |
| 40 | + |
| 41 | +# Ensure the destination directory exists |
| 42 | +if (-not (Test-Path $DestinationPath)) { |
| 43 | + New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null |
| 44 | + Write-Host "Created destination directory: $DestinationPath" |
| 45 | +} |
| 46 | + |
| 47 | +# Find all .nupkg files |
| 48 | +$nupkgs = Get-ChildItem $SourcePath |
| 49 | +if ($nupkgs.Count -eq 0) { |
| 50 | + Write-Host "No .nupkg files found in: $SourcePath" |
| 51 | + return |
| 52 | +} |
| 53 | +Write-Host "Found $($nupkgs.Count) .nupkg file(s) to extract" |
| 54 | + |
| 55 | +# Extract each .nupkg file |
| 56 | +foreach ($nupkg in $nupkgs) { |
| 57 | + $filename = $nupkg.Name.TrimEnd('.nupkg') |
| 58 | + Write-Host "Extracting '$nupkg' to default location: '$filename'..." |
| 59 | + |
| 60 | + # Extract to default location first |
| 61 | + $defaultDest = Join-Path $DestinationPath $filename |
| 62 | + Expand-Archive -Path $nupkg.FullName -DestinationPath $defaultDest -Force |
| 63 | + |
| 64 | + # Try to get package info from the nuspec to see if we should move |
| 65 | + $betterFolderName = Get-NuGetPackageInfo -ExtractedPath $defaultDest |
| 66 | + if ($betterFolderName -and $betterFolderName -ne $filename) { |
| 67 | + $finalDest = Join-Path $DestinationPath $betterFolderName |
| 68 | + Move-Item $defaultDest $finalDest |
| 69 | + Write-Host "Moved '$filename' to '$betterFolderName' based on nuspec info" |
| 70 | + } |
| 71 | + |
| 72 | + if ($RemoveOriginal) { |
| 73 | + Remove-Item $nupkg.FullName -Force |
| 74 | + Write-Host "Removed original file: $($nupkg.FullName)" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +Write-Host "Extraction completed successfully" |
0 commit comments