public override int OnAfterRenameProject(IVsHierarchy hierarchy) { if (hierarchy == null) { return(VSConstants.E_INVALIDARG); } try { List <ProjectReferenceNode> projectReferences = this.GetProjectReferencesContainingThisProject(hierarchy); // Collect data that is needed to initialize the new project reference node. string projectRef; ErrorHandler.ThrowOnFailure(this.Solution.GetProjrefOfProject(hierarchy, out projectRef)); object nameAsObject; ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_Name, out nameAsObject)); string projectName = (string)nameAsObject; string projectPath = String.Empty; IVsProject3 project = hierarchy as IVsProject3; if (project != null) { ErrorHandler.ThrowOnFailure(project.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectPath)); projectPath = Path.GetDirectoryName(projectPath); } // Remove and re add the node. foreach (ProjectReferenceNode projectReference in projectReferences) { ProjectNode projectMgr = projectReference.ProjectManager; projectReference.Remove(false); IReferenceContainer refContainer = projectMgr.GetReferenceContainer(); if (refContainer == null) { continue; } VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project; selectorData.bstrTitle = projectName; selectorData.bstrFile = projectPath; selectorData.bstrProjRef = projectRef; refContainer.AddReferenceFromSelectorData(selectorData); } } catch (COMException e) { return(e.ErrorCode); } return(VSConstants.S_OK); }
public override int OnAfterRenameProject(IVsHierarchy hierarchy) { if (hierarchy == null) { return(VSConstants.E_INVALIDARG); } try { List <ProjectReferenceNode> projectReferences = this.GetProjectReferencesContainingThisProject(hierarchy); // Collect data that is needed to initialize the new project reference node. string projectRef; ErrorHandler.ThrowOnFailure(this.Solution.GetProjrefOfProject(hierarchy, out projectRef)); object nameAsObject; ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_Name, out nameAsObject)); string projectName = (string)nameAsObject; string projectPath = String.Empty; if (hierarchy is IVsProject3) { IVsProject3 project = (IVsProject3)hierarchy; ErrorHandler.ThrowOnFailure(project.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectPath)); projectPath = Path.GetDirectoryName(projectPath); } // Remove and re add the node. foreach (ProjectReferenceNode projectReference in projectReferences) { ProjectNode projectMgr = projectReference.ProjectMgr; projectReference.Remove(false); ProjectReferenceNode newNode = new ProjectReferenceNode(projectMgr, projectName, projectPath, projectRef); newNode.AddReference(); } } catch (COMException e) { Trace.WriteLine("Exception :" + e.Message); return(e.ErrorCode); } return(VSConstants.S_OK); }
/// <summary> /// Notifies a sink that the [bindable] property specified by dispID has changed. /// If dispID is DISPID_UNKNOWN, then multiple properties have changed together. /// The client (owner of the sink) should then retrieve the current value of each property of interest from the object that generated the notification. /// In our case we will care about the VSLangProj80.VsProjPropId.VBPROJPROPID_FileName and update the changes in the parent project file. /// </summary> /// <param name="dispid">Dispatch identifier of the property that is about to change or DISPID_UNKNOWN if multiple properties are about to change.</param> public virtual void OnChanged(int dispid) { if (dispid == (int)VSLangProj80.VsProjPropId.VBPROJPROPID_FileName) { // Get the filename of the nested project. Inetead of asking the label on the nested we ask the filename, since the label might not yet been set. IVsProject3 nestedProject = this.nestedHierarchy as IVsProject3; if (nestedProject != null) { string document; nestedProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out document); this.RenameNestedProjectInParentProject(Path.GetFileNameWithoutExtension(document)); // We need to redraw the caption since for some reason, by intervining to the OnChanged event the Caption is not updated. this.ReDraw(UIHierarchyElement.Caption); } } }
private void TestImplicitNestedProjectReload(IServiceProvider sp, ProjectNode project, int dialogAnswer) { // Save everything. IVsSolution solutionService = (IVsSolution)sp.GetService(typeof(IVsSolution)); solutionService.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, project, 0); IVsProject3 nestedProject = Utilities.GetNestedHierarchy(project, "ANestedProject") as IVsProject3; if (nestedProject == null) { throw new InvalidOperationException("The nested project has not been loaded corectly"); } string nestedProjectFileName = null; nestedProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out nestedProjectFileName); if (nestedProjectFileName == null) { throw new InvalidOperationException("The nested project file name could not been retrieved corectly"); } string resourceText = Utilities.GetResourceStringFromTheProjectAssembly("QueryReloadNestedProject"); // Create the messageBoxListener Thread. This will bring up the reload of the nested project file. // In this scenario we will answer dialogAnswer. Also we rely on the exact messagebox text here. string message = String.Format(System.Globalization.CultureInfo.CurrentCulture, resourceText, nestedProjectFileName); DialogBoxPurger purger = new DialogBoxPurger(dialogAnswer, message); bool result = false; try { purger.Start(); this.AddReferenceExternallyToTheProjectFile(nestedProjectFileName); } finally { result = purger.WaitForDialogThreadToTerminate(); } if (!result) { throw new InvalidOperationException("The messagebox for relaoding the nested project file has never popped up"); } // Check to see if the nested project is there. EnvDTE.Project projectDTE = Utilities.GetAutomationObject(project); EnvDTE.ProjectItem item = projectDTE.ProjectItems.Item("ANestedProject"); Assert.IsNotNull(item, "The nested project has not been loaded correctly."); EnvDTE.Project nestedAutomationProject = item.SubProject; // Now check to see if we can find the added reference VSLangProj.VSProject automationProject = nestedAutomationProject.Object as VSLangProj.VSProject; if (nestedAutomationProject == null) { throw new InvalidOperationException("The nested project is not a vs language project"); } // Get references collection VSLangProj.References references = automationProject.References; IEnumerator enumerator = references.GetEnumerator(); bool found = false; while (enumerator.MoveNext()) { VSLangProj.Reference reference = enumerator.Current as VSLangProj.Reference; if (reference.Name == BuildEngineRef) { found = true; } } if (dialogAnswer == NativeMethods.IDYES) { Assert.IsTrue(found, "The nested project file has not been reloaded correctly"); } else { Assert.IsFalse(found, "The nested project file has been reloaded but was asked not to do that."); } }