From 76526c6f1df08c0a55fbae79a7148a4a58805626 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 27 Feb 2018 13:36:54 -0800 Subject: [PATCH] Restore modules from the NuGet package cache by using dotnet restore (#6111) --- build.psm1 | 148 ++++++++++-------------- docs/building/internals.md | 2 +- nuget.config | 1 + src/Modules/PSGalleryModules.csproj | 12 ++ test/PSReadLine/App.config | 6 - test/PSReadLine/AssemblyInfo.cs | 35 ------ test/PSReadLine/PSReadLine.tests.csproj | 16 --- test/PSReadLine/Program.cs | 79 ------------- test/PSReadLine/packages.config | 4 - 9 files changed, 75 insertions(+), 228 deletions(-) create mode 100644 src/Modules/PSGalleryModules.csproj delete mode 100644 test/PSReadLine/App.config delete mode 100644 test/PSReadLine/AssemblyInfo.cs delete mode 100644 test/PSReadLine/PSReadLine.tests.csproj delete mode 100644 test/PSReadLine/Program.cs delete mode 100644 test/PSReadLine/packages.config diff --git a/build.psm1 b/build.psm1 index 000b619775..7e0940776b 100644 --- a/build.psm1 +++ b/build.psm1 @@ -526,9 +526,7 @@ Fix steps: # handle Restore if ($Restore -or -not (Test-Path "$($Options.Top)/obj/project.assets.json")) { - log "Run dotnet restore" - - $srcProjectDirs = @($Options.Top, "$PSScriptRoot/src/TypeCatalogGen", "$PSScriptRoot/src/ResGen") + $srcProjectDirs = @($Options.Top, "$PSScriptRoot/src/TypeCatalogGen", "$PSScriptRoot/src/ResGen", "$PSScriptRoot/src/Modules/PSGalleryModules.csproj") $testProjectDirs = Get-ChildItem "$PSScriptRoot/test/*.csproj" -Recurse | ForEach-Object { [System.IO.Path]::GetDirectoryName($_) } $RestoreArguments = @("--verbosity") @@ -538,7 +536,10 @@ Fix steps: $RestoreArguments += "quiet" } - ($srcProjectDirs + $testProjectDirs) | ForEach-Object { Start-NativeExecution { dotnet restore $_ $RestoreArguments } } + ($srcProjectDirs + $testProjectDirs) | ForEach-Object { + log "Run dotnet restore $_ $RestoreArguments" + Start-NativeExecution { dotnet restore $_ $RestoreArguments } + } } # handle ResGen @@ -651,17 +652,11 @@ function Restore-PSModuleToBuild $CI ) - $ProgressPreference = "SilentlyContinue" log "Restore PowerShell modules to $publishPath" $modulesDir = Join-Path -Path $publishPath -ChildPath "Modules" - # Restore modules from powershellgallery feed - Restore-PSModule -Destination $modulesDir -Name @( - # PowerShellGet depends on PackageManagement module, so PackageManagement module will be installed with the PowerShellGet module. - 'PowerShellGet' - 'Microsoft.PowerShell.Archive' - ) -SourceLocation "https://www.powershellgallery.com/api/v2/" + Copy-PSGalleryModules -Destination $modulesDir if($CI.IsPresent) { @@ -676,7 +671,7 @@ function Restore-PSPester [ValidateNotNullOrEmpty()] [string] $Destination = ([IO.Path]::Combine((Split-Path (Get-PSOptions -DefaultToNew).Output), "Modules")) ) - Save-Module -Name Pester -Path $Destination -Repository PSGallery + Copy-PSGalleryModules -Destination $Destination -ModuleNames Pester } function Compress-TestContent { @@ -2309,101 +2304,80 @@ function Clear-PSRepo } # Install PowerShell modules such as PackageManagement, PowerShellGet -function Restore-PSModule +function Copy-PSGalleryModules { [CmdletBinding()] param( [Parameter(Mandatory=$true)] - [ValidateNotNullOrEmpty()] - [string[]]$Name, - - [Parameter(Mandatory=$true)] - [ValidateNotNullOrEmpty()] [string]$Destination, - [string]$SourceLocation="https://powershell.myget.org/F/powershellmodule/api/v2/", + [Parameter()] + [ValidateNotNullOrEmpty()] + [string[]]$ModuleNames + ) - [string]$RequiredVersion - ) + $ModulesOnlyForCI = @("Pester") - $needRegister = $true - $RepositoryName = "mygetpsmodule" - - # Check if the PackageManagement works in the base-oS or PowerShellCore - $null = Get-PackageProvider -Name NuGet -ForceBootstrap -Verbose:$VerbosePreference - $null = Get-PackageProvider -Name PowerShellGet -Verbose:$VerbosePreference - - # Get the existing registered PowerShellGet repositories - $psrepos = PowerShellGet\Get-PSRepository - - foreach ($repo in $psrepos) - { - if(($repo.SourceLocation -eq $SourceLocation) -or ($repo.SourceLocation.TrimEnd("/") -eq $SourceLocation.TrimEnd("/"))) - { - # found a registered repository that matches the source location - $needRegister = $false - $RepositoryName = $repo.Name - break - } + if (!$Destination.EndsWith("Modules")) { + throw "Installing to an unexpected location" } - if($needRegister) - { - $regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue - if($regVar) - { - PowerShellGet\UnRegister-PSRepository -Name $RepositoryName - } - - log "Registering PSRepository with name: $RepositoryName and sourcelocation: $SourceLocation" - PowerShellGet\Register-PSRepository -Name $RepositoryName -SourceLocation $SourceLocation -ErrorVariable ev -verbose - if($ev) - { - throw ("Failed to register repository '{0}'" -f $RepositoryName) - } - - $regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName - if(-not $regVar) - { - throw ("'{0}' is not registered" -f $RepositoryName) - } + $cache = dotnet nuget locals global-packages -l + if ($cache -match "info : global-packages: (.*)") { + $nugetCache = $matches[1] + } + else { + throw "Can't find nuget global cache" } - log ("Name='{0}', Destination='{1}', Repository='{2}'" -f ($Name -join ','), $Destination, $RepositoryName) + $psGalleryProj = [xml](Get-Content -Raw $PSScriptRoot\src\Modules\PSGalleryModules.csproj) - # do not output progress - $ProgressPreference = "SilentlyContinue" - $Name | ForEach-Object { + foreach ($m in $psGalleryProj.Project.ItemGroup.PackageReference) { + $name = $m.Include + $version = $m.Version - $command = @{ - Name=$_ - Path = $Destination - Repository =$RepositoryName - } - - if($RequiredVersion) - { - $command.Add("RequiredVersion", $RequiredVersion) + if ($null -ne $ModuleNames) { + # When '-ModuleNames' is specified, then we only copy those specified modules + if ($name -notin $ModuleNames) { continue } + } else { + # When '-ModuleNames' is NOT specified, copy all modules except the CI-only ones + if ($name -in $ModulesOnlyForCI) { continue } } - # pull down the module - log "running save-module $_" - PowerShellGet\Save-Module @command -Force + log "Name='$Name', Version='$version', Destination='$Destination'" - # Remove PSGetModuleInfo.xml file - Find-Module -Name $_ -Repository $RepositoryName -IncludeDependencies | ForEach-Object { - Remove-Item -Path $Destination\$($_.Name)\*\PSGetModuleInfo.xml -Force + # Remove the build revision from the src (nuget drops it). + $srcVer = if ($version -match "(\d+.\d+.\d+).\d+") { + $matches[1] + } else { + $version } - } - # Clean up - if($needRegister) - { - $regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue - if($regVar) + # Remove semantic version in the destination directory + $destVer = if ($version -match "(\d+.\d+.\d+)-.+") { + $matches[1] + } else { + $version + } + + # Nuget seems to always use lowercase in the cache + $src = "$nugetCache/$($name.ToLower())/$srcVer" + $dest = "$Destination/$name/$destVer" + + Remove-Item -Force -ErrorAction Ignore -Recurse "$Destination/$name" + New-Item -Path $dest -ItemType Directory -Force -ErrorAction Stop > $null + $dontCopy = '*.nupkg', '*.nupkg.sha512', '*.nuspec', 'System.Runtime.InteropServices.RuntimeInformation.dll' + + switch ($name) { - log "Unregistering PSRepository with name: $RepositoryName" - PowerShellGet\UnRegister-PSRepository -Name $RepositoryName + "Pester" { + $toolsDir = Join-Path -Path $src -ChildPath "tools" + Copy-Item -Path $toolsDir/* -Destination $dest -Recurse -Force + } + + default { + Copy-Item -Exclude $dontCopy -Recurse $src/* $dest + } } } } diff --git a/docs/building/internals.md b/docs/building/internals.md index e9b88df5b3..5779f0d2e5 100644 --- a/docs/building/internals.md +++ b/docs/building/internals.md @@ -16,7 +16,7 @@ We are calling `dotnet` tool build for `$Top` directory ### Dummy dependencies We use dummy dependencies between projects to leverage `dotnet` build functionality. -For example, `src\powershell-win-core\powershell-win-core.csproj` has dependency on `Microsoft.PowerShell.PSReadLine`, +For example, `src\powershell-win-core\powershell-win-core.csproj` has dependency on `Microsoft.PowerShell.Commands.Diagnostics.csproj`, but in reality, there is no build dependency. Dummy dependencies allows us to build just `$Top` folder, instead of building several folders. diff --git a/nuget.config b/nuget.config index ca674f6c21..74cb0168d2 100644 --- a/nuget.config +++ b/nuget.config @@ -5,5 +5,6 @@ + diff --git a/src/Modules/PSGalleryModules.csproj b/src/Modules/PSGalleryModules.csproj new file mode 100644 index 0000000000..4704631780 --- /dev/null +++ b/src/Modules/PSGalleryModules.csproj @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/PSReadLine/App.config b/test/PSReadLine/App.config deleted file mode 100644 index 8e15646352..0000000000 --- a/test/PSReadLine/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/test/PSReadLine/AssemblyInfo.cs b/test/PSReadLine/AssemblyInfo.cs deleted file mode 100644 index c5af7817c2..0000000000 --- a/test/PSReadLine/AssemblyInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("TestPSReadLine")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TestPSReadLine")] -[assembly: AssemblyCopyright("Copyright (c) 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("2892ed9f-6820-48a0-819b-0452c3b5401b")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/test/PSReadLine/PSReadLine.tests.csproj b/test/PSReadLine/PSReadLine.tests.csproj deleted file mode 100644 index b8e2dd0e2e..0000000000 --- a/test/PSReadLine/PSReadLine.tests.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - PSReadLine basic tests - TestPSReadLine - Exe - win7-x86;win7-x64;osx.10.12-x64;linux-x64 - - - - - - - diff --git a/test/PSReadLine/Program.cs b/test/PSReadLine/Program.cs deleted file mode 100644 index 532186898d..0000000000 --- a/test/PSReadLine/Program.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Management.Automation; -using System.Management.Automation.Runspaces; -using Microsoft.PowerShell; - -namespace TestPSReadLine -{ - class Program - { - static void CauseCrash(ConsoleKeyInfo? key = null, object arg = null) - { - throw new Exception("intentional crash for test purposes"); - } - - [STAThread] - static void Main() - { - //Box(new List {"abc", " def", "this is something coo"}); - - var iss = InitialSessionState.CreateDefault2(); - var rs = RunspaceFactory.CreateRunspace(iss); - rs.Open(); - Runspace.DefaultRunspace = rs; - - PSConsoleReadLine.SetOptions(new SetPSReadlineOption - { - EditMode = EditMode.Emacs, - HistoryNoDuplicates = true, - }); - PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+LeftArrow"}, PSConsoleReadLine.ShellBackwardWord, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+RightArrow"}, PSConsoleReadLine.ShellNextWord, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"F4"}, PSConsoleReadLine.HistorySearchBackward, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"F5"}, PSConsoleReadLine.HistorySearchForward, "", ""); - //PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+E"}, PSConsoleReadLine.EnableDemoMode, "", ""); - //PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+D"}, PSConsoleReadLine.DisableDemoMode, "", ""); - // PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+C"}, PSConsoleReadLine.CaptureScreen, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+P"}, PSConsoleReadLine.InvokePrompt, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+X"}, CauseCrash, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"F6"}, PSConsoleReadLine.PreviousLine, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"F7"}, PSConsoleReadLine.NextLine, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"F2"}, PSConsoleReadLine.ValidateAndAcceptLine, "", ""); - PSConsoleReadLine.SetKeyHandler(new[] {"Enter"}, PSConsoleReadLine.AcceptLine, "", ""); - - EngineIntrinsics executionContext; - using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace)) - { - executionContext = - ps.AddScript("$ExecutionContext").Invoke().FirstOrDefault(); - - // This is a workaround to ensure the command analysis cache has been created before - // we enter into ReadLine. It's a little slow and infrequently needed, so just - // uncomment host stops responding, run it once, then comment it out again. - //ps.Commands.Clear(); - //ps.AddCommand("Get-Command").Invoke(); - } - - while (true) - { - Console.Write("TestHostPS> "); - - var line = PSConsoleReadLine.ReadLine(null, executionContext); - Console.WriteLine(line); - line = line.Trim(); - if (line.Equals("exit")) - Environment.Exit(0); - if (line.Equals("cmd")) - PSConsoleReadLine.SetOptions(new SetPSReadlineOption {EditMode = EditMode.Windows}); - if (line.Equals("emacs")) - PSConsoleReadLine.SetOptions(new SetPSReadlineOption {EditMode = EditMode.Emacs}); - if (line.Equals("vi")) - PSConsoleReadLine.SetOptions(new SetPSReadlineOption {EditMode = EditMode.Vi}); - if (line.Equals("nodupes")) - PSConsoleReadLine.SetOptions(new SetPSReadlineOption {HistoryNoDuplicates = true}); - } - } - } -} diff --git a/test/PSReadLine/packages.config b/test/PSReadLine/packages.config deleted file mode 100644 index cae6c6db89..0000000000 --- a/test/PSReadLine/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file