Instantly share code, notes, and snippets.

@attilah /EPR.ps1
Last active Aug 29, 2015

Embed
What would you like to do?
How to enable package restore from a script on a solution from StudioShell console
Add-Type -AssemblyName NugetConsole.Host.PowerShell
#
# These functions are from Nuget test sources, the key here is to get access to an IVsPackageSourceProvider instance.
#
function Get-Interface
{
Param(
$Object,
[type]$InterfaceType
)
[NuGetConsole.Host.PowerShell.Implementation.PSTypeWrapper]::GetInterface($Object, $InterfaceType)
}
function Get-VSService
{
Param(
[type]$ServiceType,
[type]$InterfaceType
)
$service = [Microsoft.VisualStudio.Shell.Package]::GetGlobalService($ServiceType)
if ($service -and $InterfaceType) {
$service = Get-Interface $service $InterfaceType
}
$service
}
function Get-VSComponentModel
{
Get-VSService ([Microsoft.VisualStudio.ComponentModelHost.SComponentModel]) ([Microsoft.VisualStudio.ComponentModelHost.IComponentModel])
}
function Enable-PackageRestore {
if (!$dte.Solution -or !$dte.Solution.IsOpen)
{
throw "No solution is available."
}
$componentService = Get-VSComponentModel
# change active package source to "All"
$packageSourceProvider = $componentService.GetService([NuGet.VisualStudio.IVsPackageSourceProvider])
$packageSourceProvider.ActivePackageSource = [NuGet.VisualStudio.AggregatePackageSource]::Instance
$packageRestoreManager = $componentService.GetService([NuGet.VisualStudio.IPackageRestoreManager])
$packageRestoreManager.EnableCurrentSolutionForRestore($false)
}
Enable-PackageRestore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment