Skip to content

Commit

Permalink
Restore modules from the NuGet package cache by using dotnet restore (P…
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored Feb 27, 2018
1 parent 548850d commit 76526c6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 225 deletions.
142 changes: 58 additions & 84 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
[string]$Destination,

[Parameter(Mandatory=$true)]
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Destination,
[string[]]$ModuleNames
)

[string]$SourceLocation="https://powershell.myget.org/F/powershellmodule/api/v2/",
$ModulesOnlyForCI = @("Pester")

[string]$RequiredVersion
)
if (!$Destination.EndsWith("Modules")) {
throw "Installing to an unexpected location"
}

$needRegister = $true
$RepositoryName = "mygetpsmodule"
$cache = dotnet nuget locals global-packages -l
if ($cache -match "info : global-packages: (.*)") {
$nugetCache = $matches[1]
}
else {
throw "Can't find nuget global cache"
}

# 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
$psGalleryProj = [xml](Get-Content -Raw $PSScriptRoot\src\Modules\PSGalleryModules.csproj)

# Get the existing registered PowerShellGet repositories
$psrepos = PowerShellGet\Get-PSRepository
foreach ($m in $psGalleryProj.Project.ItemGroup.PackageReference) {
$name = $m.Include
$version = $m.Version

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 ($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 }
}
}

if($needRegister)
{
$regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue
if($regVar)
{
PowerShellGet\UnRegister-PSRepository -Name $RepositoryName
}
log "Name='$Name', Version='$version', Destination='$Destination'"

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)
# Remove the build revision from the src (nuget drops it).
$srcVer = if ($version -match "(\d+.\d+.\d+).\d+") {
$matches[1]
} else {
$version
}

$regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName
if(-not $regVar)
{
throw ("'{0}' is not registered" -f $RepositoryName)
# Remove semantic version in the destination directory
$destVer = if ($version -match "(\d+.\d+.\d+)-.+") {
$matches[1]
} else {
$version
}
}

log ("Name='{0}', Destination='{1}', Repository='{2}'" -f ($Name -join ','), $Destination, $RepositoryName)
# Nuget seems to always use lowercase in the cache
$src = "$nugetCache/$($name.ToLower())/$srcVer"
$dest = "$Destination/$name/$destVer"

# do not output progress
$ProgressPreference = "SilentlyContinue"
$Name | ForEach-Object {

$command = @{
Name=$_
Path = $Destination
Repository =$RepositoryName
}
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'

if($RequiredVersion)
switch ($name)
{
$command.Add("RequiredVersion", $RequiredVersion)
}

# pull down the module
log "running save-module $_"
PowerShellGet\Save-Module @command -Force

# Remove PSGetModuleInfo.xml file
Find-Module -Name $_ -Repository $RepositoryName -IncludeDependencies | ForEach-Object {
Remove-Item -Path $Destination\$($_.Name)\*\PSGetModuleInfo.xml -Force
}
}
"Pester" {
$toolsDir = Join-Path -Path $src -ChildPath "tools"
Copy-Item -Path $toolsDir/* -Destination $dest -Recurse -Force
}

# Clean up
if($needRegister)
{
$regVar = PowerShellGet\Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue
if($regVar)
{
log "Unregistering PSRepository with name: $RepositoryName"
PowerShellGet\UnRegister-PSRepository -Name $RepositoryName
default {
Copy-Item -Exclude $dontCopy -Recurse $src/* $dest
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/building/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -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>
12 changes: 12 additions & 0 deletions src/Modules/PSGalleryModules.csproj
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 0 additions & 6 deletions test/PSReadLine/App.config

This file was deleted.

35 changes: 0 additions & 35 deletions test/PSReadLine/AssemblyInfo.cs

This file was deleted.

16 changes: 0 additions & 16 deletions test/PSReadLine/PSReadLine.tests.csproj

This file was deleted.

79 changes: 0 additions & 79 deletions test/PSReadLine/Program.cs

This file was deleted.

4 changes: 0 additions & 4 deletions test/PSReadLine/packages.config

This file was deleted.

0 comments on commit 76526c6

Please sign in to comment.