Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. $packagesPath = (Join-Path -Path $WorkingDirectory -ChildPath "packages");
  2. if (Test-Path -Path $packagesPath) {
  3. Write-Verbose -Message "Fixing packages folder structure...";
  4. $restoredVersionedPackagePaths = @(Get-ChildItem -Path $packagesPath -Directory |
  5. Select-Object -ExpandProperty "FullName" |
  6. ForEach-Object {
  7. $path = $_;
  8. $versionedMatch = [regex]::Match($path, ".*?\\(?<path>packages\\(?<id>.*?)\.(?<version>\d+(?:\.\d+)+))");
  9. if (!$versionedMatch.Success) {
  10. return;
  11. }
  12. New-Object -TypeName "psobject" -Property $([ordered]@{
  13. Path = $path;
  14. Version = [version]$versionedMatch.Groups["version"].Value;
  15. Id = $versionedMatch.Groups["id"].Value;
  16. NupkgPath = (Get-ChildItem -Path $path -File -Filter "*.nupkg" | Select-Object -ExpandProperty "FullName" -First 1)
  17. Directories = (Get-ChildItem -Path $path -Directory | Select-Object -ExpandProperty "FullName")
  18. }) | Write-Output;
  19. });
  20. foreach ($restoredVersionedPackagePath in $restoredVersionedPackagePaths) {
  21. $modernPackageId = $restoredVersionedPackagePath.Id.ToLowerInvariant();
  22. $modernPackagePath = Join-Path -Path $packagesPath -ChildPath $modernPackageId;
  23. $modernVersion = $restoredVersionedPackagePath.Version.Major.ToString();
  24. $modernVersion += if ($restoredVersionedPackagePath.Version.Minor -lt 0) {
  25. ".0"
  26. } else {
  27. ".$($restoredVersionedPackagePath.Version.Minor.ToString())";
  28. }
  29. $modernVersion += if ($restoredVersionedPackagePath.Version.Build -lt 0) {
  30. ".0"
  31. } else {
  32. ".$($restoredVersionedPackagePath.Version.Build.ToString())";
  33. }
  34. if (!(Test-Path -Path $modernPackagePath)) {
  35. New-Item -ItemType "Directory" -Path $modernPackagePath -Force | Out-Null;
  36. }
  37. $modernVersionedPackagePath = Join-Path -Path $modernPackagePath -ChildPath $modernVersion;
  38. if (!(Test-Path -Path $modernVersionedPackagePath)) {
  39. New-Item -ItemType "Directory" -Path $modernVersionedPackagePath -Force | Out-Null;
  40. } else {
  41. continue;
  42. }
  43. $currentDirectories = @(Get-ChildItem -Path $modernVersionedPackagePath -Directory | Select-Object -ExpandProperty "FullName");
  44. if ($currentDirectories) {
  45. $currentDirectories | ForEach-Object { Remove-Item -Path $_ -Recurse -Force | Out-Null; };
  46. }
  47. foreach ($directory in $restoredVersionedPackagePath.Directories) {
  48. $directoryName = Split-Path -Path $directory -Leaf;
  49. $exec = Join-Path -Path ([System.Environment]::SystemDirectory) -ChildPath "xcopy.exe";
  50. $targetDirectory = Join-Path -Path $modernVersionedPackagePath -ChildPath $directoryName;
  51. $execResult = (& cmd.exe /s /c "`"$exec`" `"$directory`" `"$targetDirectory`" /E /I 2>&1");
  52. if ($execResult) {
  53. foreach ($line in @($execResult)) {
  54. Write-Verbose -Message $line;
  55. }
  56. }
  57. }
  58. $modernPackageHash = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($(Get-FileHash -Path $restoredVersionedPackagePath.NupkgPath -Algorithm "SHA512").Hash));
  59. $modernPackageHashPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.$modernVersion.nupkg.sha512";
  60. if (!(Test-Path -Path $modernPackageHashPath)) {
  61. Set-Content -Path $modernPackageHashPath -Value $modernPackageHash -Encoding UTF8 -Force;
  62. }
  63. $modernNupkgPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.$modernVersion.nupkg";
  64. if (!(Test-Path -Path $modernNupkgPath)) {
  65. Copy-Item -Path $restoredVersionedPackagePath.NupkgPath -Destination $modernNupkgPath -Force;
  66. }
  67. $modernMetaPath = Join-Path -Path $modernVersionedPackagePath -ChildPath ".nupkg.metadata";
  68. if (!(Test-Path -Path $modernMetaPath)) {
  69. Set-Content -Path $modernMetaPath -Value $($(New-Object -TypeName "psobject" -Property @{ version = 1; contentHash = $modernPackageHash }) | ConvertTo-Json -Depth 9) -Encoding UTF8 -Force;
  70. }
  71. $modernNuspecPath = Join-Path -Path $modernVersionedPackagePath -ChildPath "$modernPackageId.nuspec";
  72. if (!(Test-Path -Path $modernNuspecPath)) {
  73. if ( -not("System.IO.Compression.ZipFile" -as [Type]) ) {
  74. [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null;
  75. }
  76. $nupkgZip = [System.IO.Compression.ZipFile]::OpenRead($modernNupkgPath);
  77. $nupkgZipNuspecEntry = $nupkgZip.Entries |
  78. Where-Object { $_.FullName -like "*.nuspec" } |
  79. Select-Object -First 1;
  80. [System.IO.Compression.ZipFileExtensions]::ExtractToFile($nupkgZipNuspecEntry, $(Join-Path -Path $modernVersionedPackagePath -ChildPath $nupkgZipNuspecEntry.Name.ToLowerInvariant()), $true);
  81. $nupkgZip.Dispose();
  82. }
  83. }
  84. Write-Verbose -Message "Done fixing packages folder structure.";
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement