コード例 #1
0
ファイル: VsHelper.cs プロジェクト: kouweizhong/sqlcecodegen
        public static IVsHierarchy ToHierarchy(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            string projectGuid = null;

            using (var projectReader = XmlReader.Create(project.FileName))
            {
                projectReader.MoveToContent();
                if (projectReader.NameTable != null)
                {
                    object nodeName = projectReader.NameTable.Add("ProjectGuid");
                    while (projectReader.Read())
                    {
                        if (Equals(projectReader.LocalName, nodeName))
                        {
                            projectGuid = projectReader.ReadElementContentAsString();
                            break;
                        }
                    }
                }
            }
            IServiceProvider serviceProvider =
                new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

            return(projectGuid != null?VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid)) : null);
        }
コード例 #2
0
        public static IVsHierarchy ToHierarchy(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            string projectGuid = null;

            // DTE does not expose the project GUID that exists at in the msbuild project file.
            // Cannot use MSBuild object model because it uses a static instance of the Engine, and using the Project will cause it to be unloaded from the engine when the GC collects the variable that we declare.
            using (var projectReader = XmlReader.Create(project.FileName))
            {
                projectReader.MoveToContent();
                var nodeName = projectReader.NameTable.Add("ProjectGuid");
                while (projectReader.Read())
                {
                    if (object.Equals(projectReader.LocalName, nodeName))
                    {
                        projectGuid = (string)projectReader.ReadElementContentAsString();
                        break;
                    }
                }
            }
            Debug.Assert(!string.IsNullOrEmpty(projectGuid));
            var serviceProvider = new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

            return(VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid)));
        }
コード例 #3
0
        private IVsHierarchy ToHierarchy(EnvDTE.Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            string projectGuid = null;

            if (File.Exists(project.FileName))
            {
                // DTE does not expose the project GUID that exists at in the msbuild project file.
                // Cannot use MSBuild object model because it uses a static instance of the Engine,
                // and using the Project will cause it to be unloaded from the engine when the
                // GC collects the variable that we declare.
                using (XmlReader projectReader = XmlReader.Create(project.FileName))
                {
                    projectReader.MoveToContent();
                    object nodeName = projectReader.NameTable.Add("ProjectGuid");
                    while (projectReader.Read())
                    {
                        if (Object.Equals(projectReader.LocalName, nodeName))
                        {
                            projectGuid = projectReader.ReadElementContentAsString();
                            break;
                        }
                    }
                }

                return(VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid)));
            }

            return(null);
        }
コード例 #4
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);
        }
コード例 #5
0
        /// <summary>
        /// Recursively search if this project reference guid is in cycle.
        /// </summary>
        private bool IsReferenceInCycle(Guid projectGuid)
        {
            // TODO: This has got to be wrong, it doesn't work w/ other project types.
            IVsHierarchy hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, projectGuid);

            IReferenceContainerProvider provider = hierarchy.GetProject().GetCommonProject() as IReferenceContainerProvider;

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

                Utilities.CheckNotNull(referenceContainer, "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);
        }
コード例 #6
0
        /// <summary>
        /// Defines whether this node is valid node for painting the refererence icon.
        /// </summary>
        /// <returns></returns>
        protected override bool CanShowDefaultIcon()
        {
            if (this.referencedProjectGuid == Guid.Empty || this.ProjectMgr == null || this.ProjectMgr.IsClosed || this.isNodeValid)
            {
                return(false);
            }

            IVsHierarchy hierarchy = null;

            hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, this.referencedProjectGuid);

            if (hierarchy == null)
            {
                return(false);
            }
            ThreadHelper.ThrowIfNotOnUIThread();

            //If the Project is unloaded return false
            if (this.ReferencedProjectObject == null)
            {
                return(false);
            }

            return(!String.IsNullOrEmpty(this.referencedProjectFullPath) && File.Exists(this.referencedProjectFullPath));
        }
コード例 #7
0
        /// <summary>
        /// Gets a Project type string for a specified project instance guid
        /// </summary>
        /// <param name="projectGuid">Project instance guid.</param>
        /// <returns>The project type string</returns>
        private string GetProjectType(Guid projectGuid)
        {
            IVsHierarchy hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, projectGuid);
            object       projectType;

            ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_TypeName, out projectType));
            return(projectType as string);
        }
コード例 #8
0
        /// <summary>
        /// Defines whether this node is valid node for painting the refererence icon.
        /// </summary>
        /// <returns></returns>
        public /*protected, but public for FSharp.Project.dll*/ override bool CanShowDefaultIcon()
        {
            if (this.referencedProjectGuid == Guid.Empty || this.ProjectMgr == null || this.ProjectMgr.IsClosed || this.isNodeValid)
            {
                return(false);
            }

            // For multitargeting, we need to find the target framework moniker for the
            // reference we are adding, and display a warning icon if the reference targets a higher
            // framework then what we have.

            IVsSolution  vsSolution = this.ProjectMgr.GetService(typeof(SVsSolution)) as IVsSolution;
            IVsHierarchy projHier   = null;

            var hr = vsSolution.GetProjectOfGuid(this.ReferencedProjectGuid, out projHier);

            //Debug.Assert(hr == VSConstants.S_OK, "GetProjectOfGuid was not able to locate a project from a project reference");
            if (hr == VSConstants.S_OK)
            {
                object otargetFrameworkMoniker = null;
                hr = projHier.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID4.VSHPROPID_TargetFrameworkMoniker, out otargetFrameworkMoniker);
                if (!ErrorHandler.Succeeded(hr))
                {
                    // Safety check
                    return(false);
                }

                if (hr == VSConstants.S_OK)
                {
                    var thisFrameworkName = new System.Runtime.Versioning.FrameworkName(GetTargetFrameworkMoniker());
                    var frameworkName     = new System.Runtime.Versioning.FrameworkName((string)otargetFrameworkMoniker);
                    if (thisFrameworkName.Version < frameworkName.Version)
                    {
                        return(false);
                    }
                }
            }

            IVsHierarchy hierarchy = null;

            hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, this.referencedProjectGuid);

            if (hierarchy == null)
            {
                return(false);
            }

            //If the Project is unloaded return false
            if (this.ReferencedProject == null)
            {
                return(false);
            }

            return(!String.IsNullOrEmpty(this.referencedProjectFullPath) && FSSafe.File.SafeExists(this.referencedProjectFullPath));
        }
コード例 #9
0
        private IVsHierarchy GetReferencedHierarchy()
        {
            IVsHierarchy hierarchy = null;

            if (this.referencedProjectGuid == Guid.Empty || this.projectMgr == null || this.projectMgr.IsClosed)
            {
                return(hierarchy);
            }

            return(VsShellUtilities.GetHierarchy(this.projectMgr.Site, this.referencedProjectGuid));
        }
コード例 #10
0
        /// <summary>
        /// Returns target framework for the given project Guid
        /// </summary>
        private static System.Runtime.Versioning.FrameworkName GetProjectTargetFrameworkName(System.IServiceProvider serviceProvider, Guid referencedProjectGuid)
        {
            var    hierarchy = VsShellUtilities.GetHierarchy(serviceProvider, referencedProjectGuid);
            object otherTargetFrameworkMonikerObj;

            hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID4.VSHPROPID_TargetFrameworkMoniker, out otherTargetFrameworkMonikerObj);

            string targetFrameworkMoniker = (string)otherTargetFrameworkMonikerObj;

            return(new System.Runtime.Versioning.FrameworkName(targetFrameworkMoniker));
        }
コード例 #11
0
        private bool IsReferenceInCycle(Guid projectGuid)
        {
            // use same logic as C#:
            //   vsproject\langbuild\langref.cpp
            //   BOOL CLanguageReferences::IsCircularReference(CLangReference *pclref, BOOL fCalculateDependencies)
            int                      isCircular = 0;
            IVsHierarchy             otherHier  = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, projectGuid);
            IVsSolutionBuildManager2 vsSBM      = this.ProjectMgr.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;

            vsSBM.CalculateProjectDependencies();
            vsSBM.QueryProjectDependency(otherHier, this.ProjectMgr.InteropSafeIVsHierarchy, out isCircular);
            return(isCircular != 0);
        }
コード例 #12
0
        /// <summary>
        /// When building a wixproj in VS, the configuration of referenced projects cannot be determined
        /// by MSBuild or from within an MSBuild task. So we'll get them from the VS project system here.
        /// </summary>
        /// <param name="project">The project where the properties are being defined; also the project
        /// whose references are being examined.</param>
        internal static void DefineProjectReferenceConfigurations(WixProjectNode project)
        {
            StringBuilder configList = new StringBuilder();

            IVsSolutionBuildManager solutionBuildManager =
                WixHelperMethods.GetService <IVsSolutionBuildManager, SVsSolutionBuildManager>(project.Site);

            List <WixProjectReferenceNode> referenceNodes = new List <WixProjectReferenceNode>();

            project.FindNodesOfType(referenceNodes);

            foreach (WixProjectReferenceNode referenceNode in referenceNodes)
            {
                IVsHierarchy hierarchy = VsShellUtilities.GetHierarchy(referenceNode.ProjectMgr.Site, referenceNode.ReferencedProjectGuid);

                string          configuration   = null;
                IVsProjectCfg2  projectCfg2     = null;
                IVsProjectCfg[] projectCfgArray = new IVsProjectCfg[1];

                int hr = solutionBuildManager.FindActiveProjectCfg(IntPtr.Zero, IntPtr.Zero, hierarchy, projectCfgArray);
                ErrorHandler.ThrowOnFailure(hr);

                projectCfg2 = projectCfgArray[0] as IVsProjectCfg2;

                if (projectCfg2 != null)
                {
                    hr = projectCfg2.get_DisplayName(out configuration);
                    if (hr != 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }
                }

                if (configuration != null)
                {
                    if (configList.Length > 0)
                    {
                        configList.Append(';');
                    }

                    configList.Append(referenceNode.ReferencedProjectName);
                    configList.Append('=');
                    configList.Append(configuration);
                }
            }

            if (configList.Length > 0)
            {
                project.BuildProject.SetGlobalProperty("VSProjectConfigurations", configList.ToString());
            }
        }
コード例 #13
0
        public IVsHierarchy ToHierarchy(EnvDTE.Project project)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            System.IServiceProvider serviceProvider =
                new ServiceProvider(project.DTE as
                                    Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
            Guid guid = GetProjectGuid(serviceProvider, project);

            if (guid == Guid.Empty)
            {
                return(null);
            }
            return(VsShellUtilities.GetHierarchy(serviceProvider, guid));
        }
コード例 #14
0
        private static IVsProject _dteProjectToVsProject(EnvDTE.Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            string projectGuid = null;

            // DTE does not expose the project GUID that exists at in the msbuild project file.
            // Cannot use MSBuild object model because it uses a static instance of the Engine,
            // and using the Project will cause it to be unloaded from the engine when the
            // GC collects the variable that we declare.
            using (XmlReader projectReader = XmlReader.Create(project.FileName))
            {
                projectReader.MoveToContent();
                object nodeName = projectReader.NameTable.Add("ProjectGuid");
                while (projectReader.Read())
                {
                    if (Object.Equals(projectReader.LocalName, nodeName))
                    {
                        projectGuid = (string)projectReader.ReadElementContentAsString();
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(projectGuid))
            {
                throw new Exception("Unable to find ProjectGuid element in the project file");
            }

            Microsoft.VisualStudio.OLE.Interop.IServiceProvider dteServiceProvider =
                (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)project.DTE;
            IServiceProvider serviceProvider = new ServiceProvider(dteServiceProvider);
            IVsHierarchy     vsHierarchy     = VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid));

            IVsProject vsProject = (IVsProject)vsHierarchy;

            if (vsProject == null)
            {
                throw new ArgumentException("Project is not a VS project.");
            }

            return(vsProject);
        }
コード例 #15
0
        /// <summary>
        /// Checks whether a reference added to a given project would introduce a circular dependency.
        /// </summary>
        protected virtual bool IsReferenceInCycle(Guid projectGuid)
        {
            IVsHierarchy referencedHierarchy = VsShellUtilities.GetHierarchy(this.ProjectManager.Site, projectGuid);

            var solutionBuildManager = this.ProjectManager.Site.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;

            if (solutionBuildManager == null)
            {
                throw new InvalidOperationException("Cannot find the IVsSolutionBuildManager2 service.");
            }

            int circular;

            Marshal.ThrowExceptionForHR(solutionBuildManager.CalculateProjectDependencies());
            Marshal.ThrowExceptionForHR(solutionBuildManager.QueryProjectDependency(referencedHierarchy, this.ProjectManager.InteropSafeIVsHierarchy, out circular));

            return(circular != 0);
        }
コード例 #16
0
ファイル: VsHelper.cs プロジェクト: vijumn/open-wssf-2015
        /// <summary>
        /// Toes the hierarchy.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <returns></returns>
        public static IVsHierarchy ToHierarchy(EnvDTE.Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            // DTE does not expose the project GUID that exists at in the msbuild project file.
            Microsoft.Build.Evaluation.Project msproject = new Microsoft.Build.Evaluation.Project();
            msproject.FullPath = project.FileName;

            string guid = msproject.GetPropertyValue("ProjectGuid");

            IServiceProvider serviceProvider = new ServiceProvider(project.DTE as
                                                                   Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

            return(VsShellUtilities.GetHierarchy(serviceProvider, new Guid(guid)));
        }
コード例 #17
0
        /// <summary>
        /// Defines whether this node is valid node for painting the refererence icon.
        /// </summary>
        /// <returns></returns>
        protected override bool CanShowDefaultIcon()
        {
            if (this.referencedProjectGuid == Guid.Empty || this.ProjectMgr == null || this.ProjectMgr.IsClosed || this.isNodeValid)
            {
                return(false);
            }

            IVsHierarchy hierarchy = null;

            hierarchy = VsShellUtilities.GetHierarchy(this.ProjectMgr.Site, this.referencedProjectGuid);

            if (hierarchy == null)
            {
                return(false);
            }

            return(!String.IsNullOrEmpty(this.referencedProjectFullPath) && File.Exists(this.referencedProjectFullPath));
        }
コード例 #18
0
        /// <summary>
        /// Get the hierarchy corresponding to a Project
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public static IVsHierarchy _oldToHierarchy(EnvDTE.Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            string projectGuid = null;

            // DTE does not expose the project GUID that exists at in the msbuild project file.
            // Cannot use MSBuild object model because it uses a static instance of the Engine,
            // and using the Project will cause it to be unloaded from the engine when the
            // GC collects the variable that we declare.
            using (XmlReader projectReader = XmlReader.Create(project.FileName))
            {
                projectReader.MoveToContent();
                if (projectReader.NameTable != null)
                {
                    object nodeName = projectReader.NameTable.Add("ProjectGuid");
                    while (projectReader.Read())
                    {
                        if (Object.Equals(projectReader.LocalName, nodeName))
                        {
                            projectGuid = (String)projectReader.ReadElementContentAsString();
                            break;
                        }
                    }
                }
            }
            Debug.Assert(!String.IsNullOrEmpty(projectGuid));

            // ABM 20140904: With a null projectGuid, we should NEVER try call GetHierarchy.
            // TODO: Determine if this error should be fatal, or soft. Handle accordingly. At least log it.
            if (string.IsNullOrEmpty(projectGuid))
            {
                // TODO: Quickly added to indicate a problem in code flow. Better exception/error handling should be implemented.
                throw new Exception("Could not locate a ProjectGuid in this project's source file.");
            }

            IServiceProvider serviceProvider = new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

            return(VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid)));
        }
コード例 #19
0
        /// <summary>
        /// When building a project in VS, the configuration of referenced projects cannot be determined
        /// by MSBuild or from within an MSBuild task. So we'll get them from the VS project system here.
        /// </summary>
        /// <param name="project">The project where the properties are being defined; also the project
        /// whose references are being examined.</param>
        internal static void DefineProjectReferenceConfigurations(XProjectNode project)
        {
            StringBuilder configList = new StringBuilder();

            IVsSolutionBuildManager solutionBuildManager =
                XHelperMethods.GetService <IVsSolutionBuildManager, SVsSolutionBuildManager>(project.Site);

            List <ProjectReferenceNode> referenceNodes = new List <ProjectReferenceNode>();

            project.FindNodesOfType(referenceNodes);

            foreach (ProjectReferenceNode referenceNode in referenceNodes)
            {
                try
                {
                    IVsHierarchy hierarchy = VsShellUtilities.GetHierarchy(referenceNode.ProjectMgr.Site, referenceNode.ReferencedProjectGuid);

                    string          configuration   = null;
                    IVsProjectCfg2  projectCfg2     = null;
                    IVsProjectCfg[] projectCfgArray = new IVsProjectCfg[1];
                    ThreadHelper.ThrowIfNotOnUIThread();

                    // this can fail for some reason... this code was copied from Wix and probably isn't stable yet.
                    // this routine is called from InvokeMSBuild and we don't want that to fix because of
                    // some bug here, so this code is surrounded by try/catch until we figure this out
                    int hr = solutionBuildManager.FindActiveProjectCfg(IntPtr.Zero, IntPtr.Zero, hierarchy, projectCfgArray);
                    ErrorHandler.ThrowOnFailure(hr);

                    projectCfg2 = projectCfgArray[0] as IVsProjectCfg2;

                    if (projectCfg2 != null)
                    {
                        hr = projectCfg2.get_DisplayName(out configuration);
                        if (hr != 0)
                        {
                            Marshal.ThrowExceptionForHR(hr);
                        }
                    }

                    if (configuration != null)
                    {
                        if (configList.Length > 0)
                        {
                            configList.Append(';');
                        }

                        configList.Append(referenceNode.ReferencedProjectName);
                        configList.Append('=');
                        configList.Append(configuration);
                    }
                }
                catch (Exception)
                {
                    ;
                }
            }

            if (configList.Length > 0)
            {
                project.BuildProject.SetGlobalProperty("VSProjectConfigurations", configList.ToString());
            }
        }