Restore modules from the NuGet package cache by using dotnet restore (#6111)

This commit is contained in:
Dongbo Wang 2018-02-27 13:36:54 -08:00 committed by GitHub
parent 548850d249
commit 76526c6f1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 228 deletions

View file

@ -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
}
}
}
}

View file

@ -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.

View file

@ -5,5 +5,6 @@
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="powershell-core" value="https://powershell.myget.org/F/powershell-core/api/v3/index.json" />
<add key="PSGallery" value="https://www.powershellgallery.com/api/v2/" />
</packageSources>
</configuration>

View file

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<Import Project="..\..\PowerShell.Common.props" />
<ItemGroup>
<PackageReference Include="PackageManagement" Version="1.1.7.0" />
<PackageReference Include="PowerShellGet" Version="1.6.0" />
<PackageReference Include="Microsoft.PowerShell.Archive" Version="1.1.0.0" />
<PackageReference Include="Pester" Version="4.3.1" />
</ItemGroup>
</Project>

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View file

@ -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")]

View file

@ -1,16 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Test.Common.props"/>
<PropertyGroup>
<Description>PSReadLine basic tests</Description>
<AssemblyName>TestPSReadLine</AssemblyName>
<OutputType>Exe</OutputType>
<RuntimeIdentifiers>win7-x86;win7-x64;osx.10.12-x64;linux-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.PowerShell.PSReadLine\Microsoft.PowerShell.PSReadLine.csproj" />
</ItemGroup>
</Project>

View file

@ -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<string> {"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<EngineIntrinsics>().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});
}
}
}
}

View file

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.PowerShell.3.ReferenceAssemblies" version="1.0.0" targetFramework="net45" />
</packages>