public static async Task <bool> DeleteFilesFromProjectAsync(Project project, IEnumerable <string> filePaths, Action <string, LogLevel> logAction, CancellationToken cancellationToken) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); int batchSize = 10; try { IVsHierarchy hierarchy = GetHierarchy(project); IVsProjectBuildSystem bldSystem = hierarchy as IVsProjectBuildSystem; List <string> filesToRemove = filePaths.ToList(); while (filesToRemove.Any()) { List <string> nextBatch = filesToRemove.Take(batchSize).ToList(); bool success = await DeleteProjectItemsInBatchAsync(hierarchy, nextBatch, logAction, cancellationToken); if (!success) { return(false); } await System.Threading.Tasks.Task.Yield(); int countToDelete = Math.Min(filesToRemove.Count(), batchSize); filesToRemove.RemoveRange(0, countToDelete); } return(true); } catch { return(false); } }
// This method is called by solution events to set the host // object for newly loaded or opened projects. public void ProjectSetHostObjects(IVsHierarchy hierarchy) { if (hierarchy != null && tools.Count > 0) { Guid projectType = Common.GetProjectType(hierarchy); IVsProjectBuildSystem pbuilder = hierarchy as IVsProjectBuildSystem; if (pbuilder != null) { foreach (Tool tool in tools) { foreach (Host host in tool.hosts) { if (host.projectType == Guid.Empty || projectType == Guid.Empty || host.projectType == projectType) { // set host objects foreach (TargetTask ttask in host.targetTasks) { Common.Trace(string.Format("Set host object for {3}: {0}-{1}-{2}", ttask.target, ttask.buildTask, host.projectType, Common.GetProjectName(hierarchy))); int result = pbuilder.SetHostObject(ttask.target, ttask.buildTask, tool.taskManager); if (result != VSConstants.S_OK) { Common.Trace("Host object setting failed with: " + result.ToString()); } } } } } } } }
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); }
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); }
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); } }
/// <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); } }
protected override void SetInnerProject(IntPtr innerIUnknown) { // // This was changed to support VS2012 - The FlavoredProject base class we were using // was throwing a COM exception, so we switched to use the newer FlavoredProjectBase class // which uses an IntPtr instead of a managed object for this class. The following method // converts the IUnknown IntPtr into a managed object from which we can convert into the // following interfaces. // object inner = Marshal.GetUniqueObjectForIUnknown(innerIUnknown); m_innerIVsProject = inner as IVsProject; m_innerIVsProjectBuildSystem = inner as IVsProjectBuildSystem; m_innerIVsHierarchy = inner as IVsHierarchy; m_innerIVsUIHierarchy = inner as IVsUIHierarchy; m_innerSingleFileGeneratorFactory = inner as IVsSingleFileGeneratorFactory; m_innerIVsBuildPropertyStorage = inner as IVsBuildPropertyStorage; m_innerProjectSpecialFiles = inner as IVsProjectSpecialFiles; Debug.Assert(m_innerIVsProject != null); Debug.Assert(m_innerIVsProjectBuildSystem != null); Debug.Assert(m_innerIVsHierarchy != null); Debug.Assert(m_innerIVsUIHierarchy != null); Debug.Assert(m_innerSingleFileGeneratorFactory != null); Debug.Assert(m_innerIVsBuildPropertyStorage != null); Debug.Assert(m_innerProjectSpecialFiles != null); if (this.serviceProvider == null) { this.serviceProvider = (System.IServiceProvider)this.m_package; } base.SetInnerProject(innerIUnknown); }