Exemplo n.º 1
0
        /// <summary>
        /// Enumerates through the items under the given project item.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <param name="name">The name of the project.</param>
        /// <param name="projectCallback">Called on each project enumerated through.</param>
        /// <param name="projectItemCallback">Called on each project item enumerated through.</param>
        /// <param name="projectContext">Project-specific context information.</param>
        /// <param name="fileContext">File-specific context information.</param>
        /// <returns>If an object is returned, enumeration should end.</returns>
        private static object EnumerateProjectItem(
            EnvDTE.ProjectItem item,
            string name,
            ProjectInvoker projectCallback,
            ProjectItemInvoker projectItemCallback,
            object projectContext,
            object fileContext)
        {
            Param.AssertNotNull(item, "item");
            Param.AssertNotNull(name, "name");
            Param.Ignore(projectCallback);
            Param.Ignore(projectItemCallback);
            Param.Ignore(projectContext);
            Param.Ignore(fileContext);

            try
            {
                switch (item.Kind)
                {
                    case EnvDTE.Constants.vsProjectItemKindSubProject:
                        if (item.SubProject != null)
                        {
                            object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;

                    case EnvDTE.Constants.vsProjectItemKindPhysicalFolder:
                    case EnvDTE.Constants.vsProjectItemKindVirtualFolder:
                        if (item.ProjectItems != null)
                        {
                            object temp = EnumerateProjectItems(item.ProjectItems, name, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;

                    default:
                        if (item.Properties != null)
                        {
                            if (projectItemCallback != null)
                            {
                                // Only add the item if the build action is set to compile.
                                if (CheckProjectItemBuildAction(item))
                                {
                                    string filePath = GetProjectItemPath(item);
                                    if (filePath != null && filePath.Length > 0)
                                    {
                                        object temp = projectItemCallback(item, filePath, ref projectContext, ref fileContext);
                                        if (temp != null)
                                        {
                                            return temp;
                                        }
                                    }
                                }
                            }
                        }

                        // Check whether it has any sub-files.
                        if (item.ProjectItems != null)
                        {
                            object temp = EnumerateProjectItems(item.ProjectItems, name, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        // If there is a sub-project, get the sub-project files.
                        if (item.SubProject != null)
                        {
                            object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;
                }
            }
            catch (COMException)
            {
            }

            return null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Enumerates through the items in the given project items list.
        /// </summary>
        /// <param name="items">The items to add files from.</param>
        /// <param name="name">The name of the project.</param>
        /// <param name="projectCallback">Called on each project enumerated through.</param>
        /// <param name="projectItemCallback">Called on each project item enumerated through.</param>
        /// <param name="projectContext">Project-specific context information.</param>
        /// <param name="fileContext">File-specific context information.</param>
        /// <returns>If an object is returned, enumeration should end.</returns>
        private static object EnumerateProjectItems(
            ProjectItems items,
            string name,
            ProjectInvoker projectCallback,
            ProjectItemInvoker projectItemCallback,
            object projectContext,
            object fileContext)
        {
            Param.AssertNotNull(items, "items");
            Param.AssertNotNull(name, "name");
            Param.Ignore(projectCallback);
            Param.Ignore(projectItemCallback);
            Param.Ignore(projectContext);
            Param.Ignore(fileContext);

            // Loop through each item in the project.
            foreach (ProjectItem item in items)
            {
                object temp = EnumerateProjectItem(item, name, projectCallback, projectItemCallback, projectContext, fileContext);
                if (temp != null)
                {
                    return temp;
                }
            }

            return null;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Enumerates through the given project.
        /// </summary>
        /// <param name="project">The project to enumerate.</param>
        /// <param name="projectCallback">Called on each project enumerated through.</param>
        /// <param name="projectItemCallback">Called on each project item enumerated through.</param>
        /// <param name="projectContext">Project-specific context information.</param>
        /// <param name="fileContext">File-specific context information.</param>
        /// <returns>If an object is returned, enumeration should end.</returns>
        private static object EnumerateProject(
            Project project,
            ProjectInvoker projectCallback,
            ProjectItemInvoker projectItemCallback,
            object projectContext,
            object fileContext)
        {
            Param.AssertNotNull(project, "project");
            Param.Ignore(projectCallback);
            Param.Ignore(projectItemCallback);
            Param.Ignore(projectContext);
            Param.Ignore(fileContext);

            EnvDTE.DTE applicationObject = GetDTE();

            if (project.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
            {
                // Figure out the path to the solution.
                string solutionPath = Path.GetDirectoryName(applicationObject.Solution.FullName);

                // Invoke the callback method on the project if necessary.
                if (projectCallback != null)
                {
                    // These types of projects are all located at the root of the solution, so we need
                    // to tack on the project name in order to differentiate between them.
                    string projectId = Path.Combine(applicationObject.Solution.FullName, project.Name);

                    object temp = projectCallback(
                        project,
                        projectId.GetHashCode(),
                        solutionPath,
                        ref projectContext,
                        ref fileContext);

                    if (temp != null)
                    {
                        return temp;
                    }
                }

                if (project.ProjectItems != null)
                {
                    // Enumerate the items under this solution project.
                    object temp = EnumerateSolutionProjectItems(
                        applicationObject.Solution,
                        project.ProjectItems,
                        solutionPath,
                        projectCallback,
                        projectItemCallback,
                        projectContext,
                        fileContext);

                    if (temp != null)
                    {
                        return temp;
                    }
                }
            }
            else
            {
                // Invoke the callback method on the project if necessary.
                if (projectCallback != null)
                {
                    string projectPath = GetProjectPath(project);
                    if (projectPath != null)
                    {
                        object temp = projectCallback(project, projectPath.GetHashCode(), projectPath, ref projectContext, ref fileContext);
                        if (temp != null)
                        {
                            return temp;
                        }
                    }
                }

                if (project.ProjectItems != null)
                {
                    // Enumerate the items under this project.
                    object temp = EnumerateProjectItems(
                        project.ProjectItems,
                        project.Name,
                        projectCallback,
                        projectItemCallback,
                        projectContext,
                        fileContext);

                    if (temp != null)
                    {
                        return temp;
                    }
                }
            }

            return null;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enumerates through the items in the given solution project.
        /// </summary>
        /// <param name="solution">The solution contianing the project.</param>
        /// <param name="items">The list of items in the project.</param>
        /// <param name="solutionPath">The path to the solution.</param>
        /// <param name="projectCallback">Called on each project enumerated through.</param>
        /// <param name="projectItemCallback">Called on each project item enumerated through.</param>
        /// <param name="projectContext">Project-specific context information.</param>
        /// <param name="fileContext">File-specific context information.</param>
        /// <returns>If an object is returned, enumeration should end.</returns>
        private static object EnumerateSolutionProjectItems(
            Solution solution,
            ProjectItems items,
            string solutionPath,
            ProjectInvoker projectCallback,
            ProjectItemInvoker projectItemCallback,
            object projectContext,
            object fileContext)
        {
            Param.AssertNotNull(solution, "solution");
            Param.AssertNotNull(items, "items");
            Param.AssertValidString(solutionPath, "solutionPath");
            Param.Ignore(projectCallback);
            Param.Ignore(projectItemCallback);
            Param.Ignore(projectContext);
            Param.Ignore(fileContext);

            // Loop through each item in the solution project.
            foreach (ProjectItem item in items)
            {
                try
                {
                    switch (item.Kind)
                    {
                        case EnvDTE.Constants.vsProjectItemKindSubProject:
                            if (item.SubProject != null)
                            {
                                object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                                if (temp != null)
                                {
                                    return temp;
                                }
                            }

                            break;

                        case EnvDTE.Constants.vsProjectItemKindPhysicalFolder:
                        case EnvDTE.Constants.vsProjectItemKindVirtualFolder:
                            if (item.ProjectItems != null)
                            {
                                object temp = EnumerateSolutionProjectItems(
                                    solution, item.ProjectItems, solutionPath, projectCallback, projectItemCallback, projectContext, fileContext);
                                if (temp != null)
                                {
                                    return temp;
                                }
                            }

                            break;

                        default:
                            // Call the callback for this item if necessary.
                            if (projectItemCallback != null && item.Name != null && item.Name.Length > 0)
                            {
                                string filePath = Path.Combine(solutionPath, item.Name);
                                object temp = projectItemCallback(item, filePath, ref projectContext, ref fileContext);
                                if (temp != null)
                                {
                                    return temp;
                                }
                            }

                            // If this is a physical file, check whether it has any sub-files.
                            if (item.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile ||
                                item.Kind == EnvDTE.Constants.vsProjectItemKindSolutionItems)
                            {
                                if (item.ProjectItems != null && item.ProjectItems.Count > 0)
                                {
                                    object temp = EnumerateSolutionProjectItems(
                                        solution, item.ProjectItems, solutionPath, projectCallback, projectItemCallback, projectContext, fileContext);
                                    if (temp != null)
                                    {
                                        return temp;
                                    }
                                }
                            }

                            // Get the files from the subproject if there is one.
                            if (item.SubProject != null)
                            {
                                object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                                if (temp != null)
                                {
                                    return temp;
                                }
                            }

                            break;
                    }
                }
                catch (COMException)
                {
                }
            }

            return null;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Enumerates through the items under the given project item.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <param name="name">The name of the project.</param>
        /// <param name="projectCallback">Called on each project enumerated through.</param>
        /// <param name="projectItemCallback">Called on each project item enumerated through.</param>
        /// <param name="projectContext">Project-specific context information.</param>
        /// <param name="fileContext">File-specific context information.</param>
        /// <returns>If an object is returned, enumeration should end.</returns>
        private static object EnumerateProjectItem(
            ProjectItem item,
            string name,
            ProjectInvoker projectCallback,
            ProjectItemInvoker projectItemCallback,
            object projectContext,
            object fileContext)
        {
            Param.AssertNotNull(item, "item");
            Param.AssertNotNull(name, "name");
            Param.Ignore(projectCallback);
            Param.Ignore(projectItemCallback);
            Param.Ignore(projectContext);
            Param.Ignore(fileContext);

            try
            {
                switch (item.Kind)
                {
                    case Constants.vsProjectItemKindSubProject:
                        if (item.SubProject != null)
                        {
                            object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;

                    case Constants.vsProjectItemKindPhysicalFolder:
                    case Constants.vsProjectItemKindVirtualFolder:
                        if (item.ProjectItems != null)
                        {
                            object temp = EnumerateProjectItems(item.ProjectItems, name, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;

                    default:
                        if (item.Properties != null)
                        {
                            if (projectItemCallback != null)
                            {
                                // Only add the item if the build action is set to compile (0 = None, 1 = Compile etc)
                                if (CheckProjectItemBuildAction(item))
                                {
                                    string filePath = GetProjectItemPath(item);
                                    if (!string.IsNullOrEmpty(filePath))
                                    {
                                        if (CheckProjectItemIsIncluded(item))
                                        {
                                            object callbackResult = projectItemCallback(item, filePath, AnalysisType.Project, ref projectContext, ref fileContext);
                                            if (callbackResult != null)
                                            {
                                                return callbackResult;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // Check whether it has any sub-files.
                        if (item.ProjectItems != null)
                        {
                            object temp = EnumerateProjectItems(item.ProjectItems, name, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        // If there is a sub-project, get the sub-project files.
                        if (item.SubProject != null)
                        {
                            object temp = EnumerateProject(item.SubProject, projectCallback, projectItemCallback, projectContext, fileContext);
                            if (temp != null)
                            {
                                return temp;
                            }
                        }

                        break;
                }
            }
            catch (COMException)
            {
            }
            catch (NotImplementedException)
            {
                // item.SubProject throws this for Installshield projects
            }

            return null;
        }