Esempio n. 1
0
        private static async Task <bool> DeleteProjectItemsInBatchAsync(IVsHierarchy hierarchy, IEnumerable <string> filePaths, Action <string, LogLevel> logAction, CancellationToken cancellationToken)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsProjectBuildSystem bldSystem = hierarchy as IVsProjectBuildSystem;
            HashSet <ProjectItem> folders   = new HashSet <ProjectItem>();

            try
            {
                if (bldSystem != null)
                {
                    bldSystem.StartBatchEdit();
                }

                foreach (string filePath in filePaths)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    ProjectItem item = DTE.Solution.FindProjectItem(filePath);

                    if (item != null)
                    {
                        ProjectItem parentFolder = item.Collection.Parent as ProjectItem;
                        folders.Add(parentFolder);
                        item.Delete();
                        logAction.Invoke(string.Format(Resources.Text.LibraryDeletedFromProject, filePath.Replace('\\', '/')), LogLevel.Operation);
                    }
                }

                DeleteEmptyFolders(folders);
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(DeleteProjectItemsInBatchAsync), ex);
                return(false);
            }
            finally
            {
                if (bldSystem != null)
                {
                    bldSystem.EndBatchEdit();
                }
            }

            return(true);
        }
Esempio n. 2
0
        private static async Task <bool> AddProjectItemsInBatchAsync(IVsHierarchy vsHierarchy, List <string> filePaths, Action <string, LogLevel> logAction, CancellationToken cancellationToken)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsProjectBuildSystem bldSystem = vsHierarchy as IVsProjectBuildSystem;

            try
            {
                if (bldSystem != null)
                {
                    bldSystem.StartBatchEdit();
                }

                cancellationToken.ThrowIfCancellationRequested();

                var           vsProject = (IVsProject)vsHierarchy;
                VSADDRESULT[] result    = new VSADDRESULT[filePaths.Count()];

                vsProject.AddItem(VSConstants.VSITEMID_ROOT,
                                  VSADDITEMOPERATION.VSADDITEMOP_LINKTOFILE,
                                  string.Empty,
                                  (uint)filePaths.Count(),
                                  filePaths.ToArray(),
                                  IntPtr.Zero,
                                  result);

                foreach (string filePath in filePaths)
                {
                    logAction.Invoke(string.Format(Resources.Text.LibraryAddedToProject, filePath.Replace('\\', '/')), LogLevel.Operation);
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(AddProjectItemsInBatchAsync), ex);
                return(false);
            }
            finally
            {
                if (bldSystem != null)
                {
                    bldSystem.EndBatchEdit();
                }
            }

            return(true);
        }
Esempio n. 3
0
        public override void Execute(PackageOperation op)
        {
            // Try to get the project for this project manager
            Project project             = _packageManager.GetProject(this);
            IVsProjectBuildSystem build = null;

            if (project != null)
            {
                build = project.ToVsProjectBuildSystem();
            }

            try
            {
                if (build != null)
                {
                    // Start a batch edit so there is no background compilation until we're done
                    // processing project actions
                    build.StartBatchEdit();
                }

                base.Execute(op);
            }
            finally
            {
                if (build != null)
                {
                    // End the batch edit when we are done.
                    build.EndBatchEdit();
                }
            }

            var eventArgs = CreateOperation(op.Package);

            if (op.Action == PackageAction.Install)
            {
                _packageManager.PackageEvents.NotifyReferenceAdded(eventArgs);
            }
            else
            {
                _packageManager.PackageEvents.NotifyReferenceRemoved(eventArgs);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Runs action on the project manager and rollsback any package installs if it fails.
        /// </summary>
        private void RunProjectAction(IProjectManager projectManager, Action action)
        {
            if (projectManager == null)
            {
                return;
            }

            // Keep track of what was added and removed
            var packagesAdded   = new Stack <IPackage>();
            var packagesRemoved = new List <IPackage>();

            EventHandler <PackageOperationEventArgs> removeHandler = (sender, e) =>
            {
                packagesRemoved.Add(e.Package);
                _packageEvents.NotifyReferenceRemoved(e);
            };

            EventHandler <PackageOperationEventArgs> addingHandler = (sender, e) =>
            {
                packagesAdded.Push(e.Package);
                _packageEvents.NotifyReferenceAdded(e);

                // If this package doesn't exist at solution level (it might not be because of leveling)
                // then we need to install it.
                if (!LocalRepository.Exists(e.Package))
                {
                    ExecuteInstall(e.Package);
                }
            };


            // Try to get the project for this project manager
            Project project = GetProject(projectManager);

            IVsProjectBuildSystem build = null;

            if (project != null)
            {
                build = project.ToVsProjectBuildSystem();
            }

            // Add the handlers
            projectManager.PackageReferenceRemoved += removeHandler;
            projectManager.PackageReferenceAdding  += addingHandler;

            try
            {
                if (build != null)
                {
                    // Start a batch edit so there is no background compilation until we're done
                    // processing project actions
                    build.StartBatchEdit();
                }

                action();

                if (BindingRedirectEnabled && projectManager.Project.IsBindingRedirectSupported)
                {
                    // Only add binding redirects if install was successful
                    AddBindingRedirects(projectManager);
                }
            }
            catch
            {
                // We need to Remove the handlers here since we're going to attempt
                // a rollback and we don't want modify the collections while rolling back.
                projectManager.PackageReferenceRemoved -= removeHandler;
                projectManager.PackageReferenceAdded   -= addingHandler;

                // When things fail attempt a rollback
                RollbackProjectActions(projectManager, packagesAdded, packagesRemoved);

                // Rollback solution packages
                Uninstall(packagesAdded);

                // Clear removed packages so we don't try to remove them again (small optimization)
                packagesRemoved.Clear();
                throw;
            }
            finally
            {
                if (build != null)
                {
                    // End the batch edit when we are done.
                    build.EndBatchEdit();
                }

                // Remove the handlers
                projectManager.PackageReferenceRemoved -= removeHandler;
                projectManager.PackageReferenceAdding  -= addingHandler;

                // Remove any packages that would be removed as a result of updating a dependency or the package itself
                // We can execute the uninstall directly since we don't need to resolve dependencies again.
                Uninstall(packagesRemoved);
            }
        }