Skip to content

Instantly share code, notes, and snippets.

@attilah
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attilah/539f5d7fe5f637bf9f0c to your computer and use it in GitHub Desktop.
Save attilah/539f5d7fe5f637bf9f0c to your computer and use it in GitHub Desktop.
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