// Resolve actions to update all packages in all projects. private IEnumerable <Resolver.PackageAction> ResolveActionsForUpdateAll(IVsPackageManager activePackageManager) { var resolver = new ActionResolver() { Logger = this, AllowPrereleaseVersions = IncludePrerelease, DependencyVersion = activePackageManager.DependencyVersion }; var allPackages = SelectedNode.GetPackages(String.Empty, IncludePrerelease); foreach (Project project in _solutionManager.GetProjects()) { IProjectManager projectManager = activePackageManager.GetProjectManager(project); foreach (var package in allPackages) { // Update if an older version package is installed in the project var localPackge = projectManager.LocalRepository.FindPackage(package.Id); if (localPackge != null && localPackge.Version < package.Version) { resolver.AddOperation(PackageAction.Install, package, projectManager); } } } var actions = resolver.ResolveActions(); return(actions); }
public void UninstallPackage(Project project, string packageId, bool removeDependencies) { if (project == null) { throw new ArgumentNullException("project"); } if (String.IsNullOrEmpty(packageId)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId")); } IVsPackageManager packageManager = _packageManagerFactory.CreatePackageManager(_packageRepository, useFallbackForDependencies: false); IProjectManager projectManager = packageManager.GetProjectManager(project); EventHandler <PackageOperationEventArgs> packageReferenceRemovingHandler = (sender, e) => { _scriptExecutor.Execute( e.InstallPath, PowerShellScripts.Uninstall, e.Package, project, projectManager.GetTargetFrameworkForPackage(packageId), NullLogger.Instance); }; try { projectManager.PackageReferenceRemoving += packageReferenceRemovingHandler; // Locate the package to uninstall IPackage package = packageManager.LocatePackageToUninstall( projectManager, packageId, version: null); // resolve actions var resolver = new ActionResolver() { RemoveDependencies = removeDependencies }; resolver.AddOperation(PackageAction.Uninstall, package, projectManager); var actions = resolver.ResolveActions(); // execute actions var actionExecutor = new ActionExecutor(); actionExecutor.Execute(actions); } finally { projectManager.PackageReferenceRemoving -= packageReferenceRemovingHandler; } }
protected override bool ExecuteCore(PackageItem item) { IVsPackageManager activePackageManager = GetActivePackageManager(); Debug.Assert(activePackageManager != null); using (activePackageManager.SourceRepository.StartOperation(OperationName, item.Id, item.Version)) { ShowProgressWindow(); // Resolve operations var resolver = new ActionResolver() { Logger = this, DependencyVersion = activePackageManager.DependencyVersion, IgnoreDependencies = false, AllowPrereleaseVersions = IncludePrerelease }; var projectManager = activePackageManager.GetProjectManager(_project); projectManager.Logger = this; resolver.AddOperation(PackageAction.Install, item.PackageIdentity, projectManager); var actions = resolver.ResolveActions(); // show license agreeement bool acceptLicense = ShowLicenseAgreement(actions); if (!acceptLicense) { return(false); } // execute operations try { RegisterPackageOperationEvents(activePackageManager, projectManager); var actionExecutor = new ActionExecutor() { Logger = this }; actionExecutor.Execute(actions); } finally { UnregisterPackageOperationEvents(activePackageManager, projectManager); } return(true); } }
protected override void ProcessRecordCore() { if (!SolutionManager.IsSolutionOpen) { // terminating ErrorHandler.ThrowSolutionNotOpenTerminatingError(); } // Locate the package to uninstall IPackage package = PackageManager.LocatePackageToUninstall( ProjectManager, Id, Version); // resolve actions var resolver = new ActionResolver() { Logger = this, ForceRemove = Force.IsPresent, RemoveDependencies = RemoveDependencies.IsPresent }; resolver.AddOperation( PackageAction.Uninstall, package, ProjectManager); var actions = resolver.ResolveActions(); if (WhatIf) { foreach (var operation in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, operation); } return; } // execute actions var actionExecutor = new ActionExecutor() { Logger = this }; actionExecutor.Execute(actions); }
private void ReinstallAllPackages(IEnumerable <IProjectManager> projectManagers) { var packages = PackageManager.LocalRepository.GetPackages().ToList(); var solutionLevelPackages = packages.Where(p => !PackageManager.IsProjectLevel(p)); var resolver = new ActionResolver() { ForceRemove = true }; // reinstall solution level packages foreach (var package in solutionLevelPackages) { resolver.AddOperation(PackageAction.Uninstall, package, new NullProjectManager(PackageManager)); var packageFromSource = PackageManager.SourceRepository.FindPackage( package.Id, package.Version); resolver.AddOperation(PackageAction.Install, packageFromSource, new NullProjectManager(PackageManager)); } // Reinstall packages in projects foreach (var projectManager in projectManagers) { ReinstallAllPackagesInProject(projectManager, resolver); } var actions = resolver.ResolveActions(); if (WhatIf) { foreach (var action in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, action); } return; } var executor = new ActionExecutor() { Logger = this, PackageOperationEventListener = this, CatchProjectOperationException = true }; executor.Execute(actions); }
private bool UninstallSolutionPackage(IPackage package) { CheckDependentPackages(package, LocalRepository, targetFramework: null); bool?result = AskRemoveDependency( package, new[] { LocalRepository }, new FrameworkName[] { null }); if (result == null) { // user presses Cancel return(false); } ShowProgressWindow(); try { RegisterPackageOperationEvents(PackageManager, null); // resolve actions var resolver = new ActionResolver() { Logger = this, ForceRemove = false, RemoveDependencies = (bool)result }; resolver.AddOperation( PackageAction.Uninstall, package, new NullProjectManager(PackageManager)); var actions = resolver.ResolveActions(); // execute actions var actionExecutor = new ActionExecutor() { Logger = this }; actionExecutor.Execute(actions); } finally { UnregisterPackageOperationEvents(PackageManager, null); } return(true); }
/* * protected override void ExecuteCommand(IProjectManager projectManager, PackageItem item, IVsPackageManager activePackageManager, IList<PackageOperation> operations) * { * activePackageManager.UpdatePackages( * projectManager, * new[] { item.PackageIdentity }, * operations, * updateDependencies: true, * allowPrereleaseVersions: IncludePrerelease, * logger: this); * } */ private IEnumerable <Resolver.PackageAction> ResolveActionsForUpdateAll(IVsPackageManager activePackageManager, IProjectManager projectManager) { var resolver = new ActionResolver() { Logger = this, AllowPrereleaseVersions = IncludePrerelease, DependencyVersion = activePackageManager.DependencyVersion }; var allPackages = SelectedNode.GetPackages(String.Empty, IncludePrerelease); foreach (var package in allPackages) { resolver.AddOperation(PackageAction.Install, package, projectManager); } var actions = resolver.ResolveActions(); return(actions); }
private void InstallPackage(IVsPackageManager packageManager) { if (packageManager == null) { return; } // Locate the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, Id, Version, IncludePrerelease.IsPresent); // Resolve actions var resolver = new ActionResolver() { Logger = this, IgnoreDependencies = IgnoreDependencies, DependencyVersion = packageManager.DependencyVersion, AllowPrereleaseVersions = IncludePrerelease.IsPresent }; resolver.AddOperation(PackageAction.Install, package, ProjectManager); var actions = resolver.ResolveActions(); if (WhatIf) { foreach (var action in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, action); } return; } var executor = new ActionExecutor() { Logger = this }; executor.Execute(actions); }
protected void UninstallPackageFromProject(Project project, PackageItem item, bool removeDependencies) { IProjectManager projectManager = null; try { projectManager = PackageManager.GetProjectManager(project); // make sure the package is installed in this project before proceeding if (projectManager.LocalRepository.Exists(item.PackageIdentity)) { RegisterPackageOperationEvents(PackageManager, projectManager); // Locate the package to uninstall IPackage package = PackageManager.LocatePackageToUninstall( projectManager, item.Id, version: null); // resolve operations var resolver = new ActionResolver() { Logger = this, ForceRemove = false, RemoveDependencies = removeDependencies }; resolver.AddOperation(PackageAction.Uninstall, package, projectManager); var actions = resolver.ResolveActions(); // execute operations var actionExecutor = new ActionExecutor() { Logger = this }; actionExecutor.Execute(actions); } } finally { if (projectManager != null) { UnregisterPackageOperationEvents(PackageManager, projectManager); } } }
private void Uninstall(string id, IPackageManager packageManager, IProjectManager projectManager) { // Locate the package to uninstall IPackage package = packageManager.LocatePackageToUninstall( projectManager, id, version: null); // resolve actions var resolver = new ActionResolver(); resolver.AddOperation(PackageAction.Uninstall, package, projectManager); var actions = resolver.ResolveActions(); // execute actions var actionExecutor = new ActionExecutor(); actionExecutor.Execute(actions); }
private void InstallPackage(string id, MockVsPackageManager2 packageManager) { var projectManager = packageManager.GetProjectManager(packageManager.SolutionManager.GetProject("default")); // Resolve the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, id, version: null, allowPrereleaseVersions: false); // Resolve operations var resolver = new ActionResolver(); resolver.AddOperation(NuGet.PackageAction.Install, package, projectManager); var actions = resolver.ResolveActions(); var executor = new ActionExecutor(); executor.Execute(actions); }
static void Install( string id, IProjectManager projectManager) { var resolver = new ActionResolver(); // Resolve the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( projectManager.PackageManager.SourceRepository, projectManager.PackageManager.LocalRepository, id, null, resolver.AllowPrereleaseVersions); // Resolve actions resolver.AddOperation(PackageAction.Install, package, projectManager); var actions = resolver.ResolveActions(); var actionExecutor = new ActionExecutor(); actionExecutor.Execute(actions); }
public Task RestoreMissingPackages() { TaskScheduler uiScheduler; try { uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); } catch (InvalidOperationException) { // this exception occurs during unit tests uiScheduler = TaskScheduler.Default; } Task task = Task.Factory.StartNew(() => { IVsPackageManager packageManager = _packageManagerFactory.CreatePackageManagerWithAllPackageSources(); IPackageRepository localRepository = packageManager.LocalRepository; var projectReferences = GetAllPackageReferences(packageManager); foreach (var reference in projectReferences) { if (!localRepository.Exists(reference.Id, reference.Version)) { // Resolve the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, reference.Id, reference.Version, allowPrereleaseVersions: true); // Resolve actions var resolver = new ActionResolver() { Logger = packageManager.Logger, DependencyVersion = packageManager.DependencyVersion, IgnoreDependencies = true, AllowPrereleaseVersions = true }; resolver.AddOperation(PackageAction.Install, package, new NullProjectManager(packageManager)); var actions = resolver.ResolveActions(); // Execute actions var actionExecutor = new ActionExecutor() { Logger = packageManager.Logger }; actionExecutor.Execute(actions); } } }); task.ContinueWith(originalTask => { if (originalTask.IsFaulted) { ExceptionHelper.WriteToActivityLog(originalTask.Exception); } else { // we don't allow canceling Debug.Assert(!originalTask.IsCanceled); // after we're done with restoring packages, do the check again CheckForMissingPackages(); } }, uiScheduler); return(task); }
/// <summary> /// Returns true if package install is needed. /// Package install is not needed if /// - AllowMultipleVersions is false; /// - there is an existing package, and its version is newer than or equal to the /// package to be installed. /// </summary> /// <param name="packageManager">The pacakge manager.</param> /// <param name="packageId">The id of the package to install.</param> /// <param name="version">The version of the package to install.</param> /// <returns>True if package install is neede; otherwise, false.</returns> private bool PackageInstallNeeded( IPackageManager packageManager, string packageId, SemanticVersion version) { if (AllowMultipleVersions) { return true; } var installedPackage = packageManager.LocalRepository.FindPackage(packageId); if (installedPackage == null) { return true; } if (version == null) { // need to query the source repository to get the version to be installed. IPackage package = packageManager.SourceRepository.FindPackage( packageId, version, NullConstraintProvider.Instance, allowPrereleaseVersions: Prerelease, allowUnlisted: false); if (package == null) { return false; } version = package.Version; } if (installedPackage.Version >= version) { // If the installed pacakge has newer version, no install is needed. return false; } // install is needed. In this case, uninstall the existing pacakge. var resolver = new ActionResolver() { Logger = packageManager.Logger, RemoveDependencies = true, ForceRemove = false }; var projectManager = new NullProjectManager(packageManager); foreach (var package in packageManager.LocalRepository.GetPackages()) { projectManager.LocalRepository.AddPackage(package); } resolver.AddOperation( PackageAction.Uninstall, installedPackage, projectManager); var projectActions = resolver.ResolveActions(); // because the projectManager's LocalRepository is not a PackageReferenceRepository, // the packages in the packages folder are not referenced. Thus, the resolved actions // are all PackageProjectActions. We need to create packages folder actions // from those PackageProjectActions. var solutionActions = new List<Resolver.PackageSolutionAction>(); foreach (var action in projectActions) { var projectAction = action as PackageProjectAction; if (projectAction == null) { continue; } var solutioAction = projectAction.ActionType == PackageActionType.Install ? PackageActionType.AddToPackagesFolder : PackageActionType.DeleteFromPackagesFolder; solutionActions.Add(new PackageSolutionAction( solutioAction, projectAction.Package, packageManager)); } var userOperationExecutor = new ActionExecutor() { Logger = packageManager.Logger }; userOperationExecutor.Execute(solutionActions); return true; }
private void InstallPackage( IFileSystem fileSystem, string packageId, SemanticVersion version) { if (version == null) { NoCache = true; } var packageManager = CreatePackageManager(fileSystem, AllowMultipleVersions); if (!PackageInstallNeeded(packageManager, packageId, version)) { Console.WriteLine(LocalizedResourceManager.GetString("InstallCommandPackageAlreadyExists"), packageId); return; } if (version == null) { var latestVersion = GetLastestPackageVersion( packageManager.SourceRepository, packageId, allowPrereleaseVersions: Prerelease); if (latestVersion != null) { version = latestVersion.Version; } } using (packageManager.SourceRepository.StartOperation( RepositoryOperationNames.Install, packageId, version == null ? null : version.ToString())) { var resolver = new ActionResolver() { Logger = Console, AllowPrereleaseVersions = Prerelease }; // Resolve the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, packageId, version, Prerelease); // Resolve operations. Note that we only care about AddToPackagesFolder actions resolver.AddOperation( PackageAction.Install, package, new NullProjectManager(packageManager)); var actions = resolver.ResolveActions() .Where(action => action.ActionType == PackageActionType.AddToPackagesFolder); if (actions.Any()) { var executor = new ActionExecutor() { Logger = Console }; executor.Execute(actions); } else if (packageManager.LocalRepository.Exists(package)) { // If the package wasn't installed by our set of operations, notify the user. Console.Log( MessageLevel.Info, NuGet.Resources.NuGetResources.Log_PackageAlreadyInstalled, package.GetFullName()); } } }
internal void InstallPackage(IPackageRepository repository, Project project, string packageId, SemanticVersion version, bool ignoreDependencies, bool skipAssemblyReferences) { if (project == null) { throw new ArgumentNullException("project"); } if (repository == null) { throw new ArgumentNullException("repository"); } using (_vsCommonOperations.SaveSolutionExplorerNodeStates(_solutionManager)) { IVsPackageManager packageManager = _packageManagerFactory.CreatePackageManager(repository, useFallbackForDependencies: false); IProjectManager projectManager = packageManager.GetProjectManager(project); EventHandler <PackageOperationEventArgs> installedHandler = (sender, e) => { _scriptExecutor.ExecuteInitScript( e.InstallPath, e.Package, NullLogger.Instance); }; EventHandler <PackageOperationEventArgs> addedHandler = (sender, e) => { _scriptExecutor.ExecuteScript( e.InstallPath, PowerShellScripts.Install, e.Package, project, project.GetTargetFrameworkName(), NullLogger.Instance); }; bool oldBindingRedirectValue = packageManager.BindingRedirectEnabled; try { projectManager.PackageReferenceAdded += addedHandler; packageManager.PackageInstalled += installedHandler; // if skipping assembly references, disable binding redirects too. packageManager.BindingRedirectEnabled = !skipAssemblyReferences; // locate the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, packageId, version, allowPrereleaseVersions: true); if (skipAssemblyReferences) { package = new SkipAssemblyReferencesPackage(package); } // resolve actions var resolver = new ActionResolver() { Logger = NullLogger.Instance, DependencyVersion = packageManager.DependencyVersion, IgnoreDependencies = ignoreDependencies, AllowPrereleaseVersions = true }; resolver.AddOperation(PackageAction.Install, package, projectManager); var actions = resolver.ResolveActions(); // execute actions var actionExecutor = new ActionExecutor(); actionExecutor.Execute(actions); } finally { packageManager.BindingRedirectEnabled = oldBindingRedirectValue; projectManager.PackageReferenceAdded -= addedHandler; packageManager.PackageInstalled -= installedHandler; } } }
protected override void ProcessRecordCore() { if (!SolutionManager.IsSolutionOpen) { // terminating ErrorHandler.ThrowSolutionNotOpenTerminatingError(); } // Locate the package to uninstall IPackage package = PackageManager.LocatePackageToUninstall( ProjectManager, Id, Version); #if VS14 var nugetAwareProject = ProjectManager == null ? null : ProjectManager.Project as INuGetPackageManager; if (nugetAwareProject != null) { var args = new Dictionary <string, object>(); args["WhatIf"] = WhatIf; args["SourceRepository"] = PackageManager.SourceRepository; args["SharedRepository"] = PackageManager.LocalRepository; using (var cts = new CancellationTokenSource()) { var task = nugetAwareProject.UninstallPackageAsync( new NuGetPackageMoniker { Id = package.Id, Version = package.Version.ToString() }, args, logger: null, progress: null, cancellationToken: cts.Token); task.Wait(); return; } } #endif // resolve actions var resolver = new ActionResolver() { Logger = this, ForceRemove = Force.IsPresent, RemoveDependencies = RemoveDependencies.IsPresent }; resolver.AddOperation( PackageAction.Uninstall, package, ProjectManager); var actions = resolver.ResolveActions(); if (WhatIf) { foreach (var operation in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, operation); } return; } // execute actions var actionExecutor = new ActionExecutor() { Logger = this }; actionExecutor.Execute(actions); }
private void InstallPackage(IVsPackageManager packageManager) { if (packageManager == null) { return; } // Locate the package to install IPackage package = PackageRepositoryHelper.ResolvePackage( packageManager.SourceRepository, packageManager.LocalRepository, Id, Version, IncludePrerelease.IsPresent); #if VS14 var nugetAwareProject = ProjectManager.Project as INuGetPackageManager; if (nugetAwareProject != null) { var args = new Dictionary <string, object>(); args["DependencyVersion"] = DependencyVersion; args["IgnoreDependencies"] = IgnoreDependencies; args["WhatIf"] = WhatIf; using (var cts = new CancellationTokenSource()) { var packageSupportedFrameworks = package.GetSupportedFrameworks(); var projectFrameworks = nugetAwareProject.GetSupportedFrameworksAsync(cts.Token).Result; args["Frameworks"] = projectFrameworks.Where( projectFramework => NuGet.VersionUtility.IsCompatible( projectFramework, packageSupportedFrameworks)).ToArray(); var task = nugetAwareProject.InstallPackageAsync( new NuGetPackageMoniker { Id = package.Id, Version = package.Version.ToString() }, args, logger: null, progress: null, cancellationToken: cts.Token); task.Wait(); return; } } #endif // Resolve actions var resolver = new ActionResolver() { Logger = this, IgnoreDependencies = IgnoreDependencies, DependencyVersion = packageManager.DependencyVersion, AllowPrereleaseVersions = IncludePrerelease.IsPresent }; resolver.AddOperation(PackageAction.Install, package, ProjectManager); var actions = resolver.ResolveActions(); if (WhatIf) { foreach (var action in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, action); } return; } var executor = new ActionExecutor() { Logger = this }; executor.Execute(actions); }
private void ReinstallOnePackage(string id, IEnumerable <IProjectManager> projectManagers) { List <Resolver.PackageAction> actions = new List <Resolver.PackageAction>(); var projectNameSpecified = !String.IsNullOrEmpty(ProjectName); var oldPackage = projectNameSpecified ? UpdateUtility.FindPackageToUpdate( id, version: null, packageManager: PackageManager, projectManager: projectManagers.First()) : UpdateUtility.FindPackageToUpdate( id, version: null, packageManager: PackageManager, projectManagers: projectManagers, logger: this); if (oldPackage.Item2 == null) { // we're reinstalling a solution level package Log(MessageLevel.Info, VsResources.ReinstallSolutionPackage, oldPackage.Item1); if (PackageManager.SourceRepository.Exists(oldPackage.Item1)) { var resolver = new ActionResolver() { ForceRemove = true }; resolver.AddOperation(PackageAction.Uninstall, oldPackage.Item1, new NullProjectManager(PackageManager)); var packageFromSource = PackageManager.SourceRepository.FindPackage( oldPackage.Item1.Id, oldPackage.Item1.Version); resolver.AddOperation(PackageAction.Install, packageFromSource, new NullProjectManager(PackageManager)); actions.AddRange(resolver.ResolveActions()); } else { Log( MessageLevel.Warning, VsResources.PackageRestoreSkipForSolution, oldPackage.Item1.GetFullName()); } } else { var reinstallInfo = new ReinstallInfo(Enumerable.Empty <VirtualProjectManager>()); var resolver = new ActionResolver() { ForceRemove = true }; foreach (var projectManager in projectManagers) { ReinstallPackage(id, projectManager, reinstallInfo, resolver); } actions.AddRange(resolver.ResolveActions()); } if (WhatIf) { foreach (var action in actions) { Log(MessageLevel.Info, Resources.Log_OperationWhatIf, action); } return; } var executor = new ActionExecutor() { Logger = this, PackageOperationEventListener = this, CatchProjectOperationException = true }; executor.Execute(actions); }