Created
February 17, 2019 15:19
-
-
Save mhudasch/65ed64c8f503dda2d64ec6c19a9bfec3 to your computer and use it in GitHub Desktop.
Fixes the nuget restore packages folder when restoring from both servers and directories at the same time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$packagesPath = (Join-Path -Path $WorkingDirectory -ChildPath "packages"); | |
if (Test-Path -Path $packagesPath) { | |
Write-Verbose -Message "Fixing packages folder structure..."; | |
$restoredVersionedPackagePaths = @(Get-ChildItem -Path $packagesPath -Directory | | |
Select-Object -ExpandProperty "FullName" | | |
ForEach-Object { | |
$path = $_; | |
$versionedMatch = [regex]::Match($path, ".*?\\(?<path>packages\\(?<id>.*?)\.(?<version>\d+(?:\.\d+)+))"); | |
if (!$versionedMatch.Success) { | |
return; | |
} | |
New-Object -TypeName "psobject" -Property $([ordered]@{ | |
Path = $path; | |
Version = [version]$versionedMatch.Groups["version"].Value; | |
Id = $versionedMatch.Groups["id"].Value; | |
NupkgPath = (Get-ChildItem -Path $path -File -Filter "*.nupkg" | Select-Object -ExpandProperty "FullName" -First 1) | |
Directories = (Get-ChildItem -Path $path -Directory | Select-Object -ExpandProperty "FullName") | |
}) | Write-Output; | |
}); | |
foreach ($restoredVersionedPackagePath in $restoredVersionedPackagePaths) { | |
$modernPackageId = $restoredVersionedPackagePath.Id.ToLowerInvariant(); | |
$modernPackagePath = Join-Path -Path $packagesPath -ChildPath $modernPackageId; | |
$modernVersion = $restoredVersionedPackagePath.Version.Major.ToString(); | |
$modernVersion += if ($restoredVersionedPackagePath.Version.Minor -lt 0) { | |
".0" | |
} else { | |
".$($restoredVersionedPackagePath.Version.Minor.ToString())"; | |
} | |
$modernVersion += if ($restoredVersionedPackagePath.Version.Build -lt 0) { | |
".0" | |
} else { | |
".$($restoredVersionedPackagePath.Version.Build.ToString())"; | |
} | |
if (!(Test-Path -Path $modernPackagePath)) { | |
New-Item -ItemType "Directory" -Path $modernPackagePath -Force | Out-Null; | |
} | |
$modernVersionedPackagePath = Join-Path -Path $modernPackagePath -ChildPath $modernVersion; | |
if (!(Test-Path -Path $modernVersionedPackagePath)) { | |
New-Item -ItemType "Directory" -Path $modernVersionedPackagePath -Force | Out-Null; | |
} else { | |
continue; | |
} | |
$currentDirectories = @(Get-ChildItem -Path $modernVersionedPackagePath -Directory | Select-Object -ExpandProperty "FullName"); | |
if ($currentDirectories) { | |
$currentDirectories | ForEach-Object { Remove-Item -Path $_ -Recurse -Force | Out-Null; }; | |
} | |
foreach ($directory in $restoredVersionedPackagePath.Directories) { | |
$directoryName = Split-Path -Path $directory -Leaf; | |
$exec = Join-Path -Path ([System.Environment]::SystemDirectory) -ChildPath "xcopy.exe"; | |
$targetDirectory = Join-Path -Path $modernVersionedPackagePath -ChildPath $directoryName; | |
$execResult = (& cmd.exe /s /c "`"$exec`" `"$directory`" `"$targetDirectory`" /E /I 2>&1"); | |
if ($execResult) { | |
foreach ($line in @($execResult)) { | |
Write-Verbose -Message $line; | |
} | |
} | |
} | |
$modernPackageHash = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($(Get-FileHash -Path $restoredVersionedPackagePath.NupkgPath -Algorithm "SHA512").Hash)); | |
$modernPackageHashPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.$modernVersion.nupkg.sha512"; | |
if (!(Test-Path -Path $modernPackageHashPath)) { | |
Set-Content -Path $modernPackageHashPath -Value $modernPackageHash -Encoding UTF8 -Force; | |
} | |
$modernNupkgPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.$modernVersion.nupkg"; | |
if (!(Test-Path -Path $modernNupkgPath)) { | |
Copy-Item -Path $restoredVersionedPackagePath.NupkgPath -Destination $modernNupkgPath -Force; | |
} | |
$modernMetaPath = Join-Path -Path $modernVersionedPackagePath -ChildPath ".nupkg.metadata"; | |
if (!(Test-Path -Path $modernMetaPath)) { | |
Set-Content -Path $modernMetaPath -Value $($(New-Object -TypeName "psobject" -Property @{ version = 1; contentHash = $modernPackageHash }) | ConvertTo-Json -Depth 9) -Encoding UTF8 -Force; | |
} | |
$modernNuspecPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.nuspec"; | |
if (!(Test-Path -Path $modernNuspecPath)) { | |
if ( -not("System.IO.Compression.ZipFile" -as [Type]) ) { | |
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null; | |
} | |
$nupkgZip = [System.IO.Compression.ZipFile]::OpenRead($modernNupkgPath); | |
$nupkgZipNuspecEntry = $nupkgZip.Entries | | |
Where-Object { $_.FullName -like "*.nuspec" } | | |
Select-Object -First 1; | |
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($nupkgZipNuspecEntry, $(Join-Path -Path $modernVersionedPackagePath -ChildPath $nupkgZipNuspecEntry.Name.ToLowerInvariant()), $true); | |
$nupkgZip.Dispose(); | |
} | |
} | |
Write-Verbose -Message "Done fixing packages folder structure."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment