示例#1
0
        /// <summary>
        /// Recursively search if this project reference guid is in cycle.
        /// </summary>
        private bool IsReferenceInCycle(Guid projectGuid)
        {
            IVsHierarchy hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, projectGuid);

            IReferenceContainerProvider provider = hierarchy as IReferenceContainerProvider;

            if (provider != null)
            {
                IReferenceContainer referenceContainer = provider.GetReferenceContainer();

                Debug.Assert(referenceContainer != null, "Could not found the References virtual node");

                foreach (ReferenceNode refNode in referenceContainer.EnumReferences())
                {
                    ProjectReferenceNode projRefNode = refNode as ProjectReferenceNode;
                    if (projRefNode != null)
                    {
                        if (projRefNode.ReferencedProjectGuid == this.ProjectMgr.ProjectIDGuid)
                        {
                            return(true);
                        }

                        if (this.IsReferenceInCycle(projRefNode.ReferencedProjectGuid))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        private ProjectReferenceNode GetProjectReferenceOnNodeForHierarchy(IList <ReferenceNode> references, IVsHierarchy inputHierarchy)
        {
            if (references == null)
            {
                return(null);
            }

            Guid projectGuid;

            inputHierarchy.GetGuidProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out projectGuid);

            string canonicalName;

            inputHierarchy.GetCanonicalName(VSConstants.VSITEMID_ROOT, out canonicalName);
            foreach (ReferenceNode refNode in references)
            {
                ProjectReferenceNode projRefNode = refNode as ProjectReferenceNode;
                if (projRefNode != null)
                {
                    if (projRefNode.ReferencedProjectGuid == projectGuid)
                    {
                        return(projRefNode);
                    }

                    // Try with canonical names, if the project that is removed is an unloaded project than the above criteria will not pass.
                    if (!String.IsNullOrEmpty(projRefNode.Url) && NativeMethods.IsSamePath(projRefNode.Url, canonicalName))
                    {
                        return(projRefNode);
                    }
                }
            }

            return(null);
        }
示例#3
0
        private List <ProjectReferenceNode> GetProjectReferencesContainingThisProject(IVsHierarchy inputHierarchy)
        {
            List <ProjectReferenceNode> projectReferences = new List <ProjectReferenceNode>();

            if (this.Solution == null || inputHierarchy == null)
            {
                return(projectReferences);
            }

            uint             flags            = (uint)(__VSENUMPROJFLAGS.EPF_ALLPROJECTS | __VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION);
            Guid             enumOnlyThisType = Guid.Empty;
            IEnumHierarchies enumHierarchies  = null;

            ErrorHandler.ThrowOnFailure(this.Solution.GetProjectEnum(flags, ref enumOnlyThisType, out enumHierarchies));
            Debug.Assert(enumHierarchies != null, "Could not get list of hierarchies in solution");

            IVsHierarchy[] hierarchies = new IVsHierarchy[1];
            uint           fetched;
            int            returnValue = VSConstants.S_OK;

            do
            {
                returnValue = enumHierarchies.Next(1, hierarchies, out fetched);
                Debug.Assert(fetched <= 1, "We asked one project to be fetched VSCore gave more than one. We cannot handle that");
                if (returnValue == VSConstants.S_OK && fetched == 1)
                {
                    IVsHierarchy hierarchy = hierarchies[0];
                    Debug.Assert(hierarchy != null, "Could not retrieve a hierarchy");
                    IReferenceContainerProvider provider = hierarchy as IReferenceContainerProvider;
                    if (provider != null)
                    {
                        IReferenceContainer referenceContainer = provider.GetReferenceContainer();

                        if (referenceContainer != null)
                        {
                            ProjectReferenceNode projectReferenceNode = this.GetProjectReferenceOnNodeForHierarchy(referenceContainer.EnumReferences(), inputHierarchy);
                            if (projectReferenceNode != null)
                            {
                                projectReferences.Add(projectReferenceNode);
                            }
                        }
                    }
                }
            } while (returnValue == VSConstants.S_OK && fetched == 1);

            return(projectReferences);
        }
示例#4
0
        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);
        }
示例#5
0
        protected override bool IsAlreadyAdded(out ReferenceNode existingNode)
        {
            ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode;

            Debug.Assert(referencesFolder != null, "Could not find the References node");
            bool shouldCheckPath = !string.IsNullOrEmpty(this.Url);

            for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
            {
                ProjectReferenceNode projectReferenceNode = n as ProjectReferenceNode;
                if (null != projectReferenceNode)
                {
                    if (this.ReferencedProjectGuid.Equals(projectReferenceNode.ReferencedProjectGuid) ||
                        (shouldCheckPath && NativeMethods.IsSamePath(projectReferenceNode.Url, this.Url)))
                    {
                        existingNode = projectReferenceNode;
                        return(true);
                    }
                }
            }

            existingNode = null;
            return(false);
        }
示例#6
0
 public ProjectReferencesProperties(ProjectReferenceNode node)
     : base(node)
 {
 }
示例#7
0
 public ProjectReferencesProperties(ProjectReferenceNode node)
     : base(node)
 {
 }
        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;
        }