Click here to Skip to main content
15,894,460 members
Articles / .NET

Versioning Controlled Build

Rate me:
Please Sign up or sign in to vote.
4.90/5 (237 votes)
8 Dec 2013CPOL35 min read 2.2M   20.6K   779  
A Visual Studio add-in and command-line utility that automates versioning of .NET and VC++ projects
/*
 * Filename:    SolutionExplorerSelector.cs
 * Product:     Versioning Controlled Build
 * Solution:    BuildAutoIncrement
 * Project:     AddinImplementation
 * Description: Class that selects projects in Solution Explorer window of 
 *              Visual Studio environment.
 * Copyright:   Julijan �ribar, 2004-2013
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author(s) be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
using EnvDTE;

#if !FX1_1
using EnvDTE80;
using DTE = EnvDTE80.DTE2;
#endif

using System;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;

namespace BuildAutoIncrement {
	/// <summary>
    ///     Helper object used to select items via Visual Studio IDE.
    ///     Used primarily for checkout of items under SSC.
    /// </summary>
	public sealed class SolutionExplorerSelector {

        private SolutionExplorerSelector() {
            m_selectedItemsCount = 0;
        }

        /// <summary>
        ///   Creates <c>SolutionExplorerCheckoutHelper</c> object with 
        ///   attached VS IDE object.
        /// </summary>
        /// <param name="environment"></param>
        public SolutionExplorerSelector(DTE environment) : this() {
            UIHierarchy solutionExplorer = (UIHierarchy)environment.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Object;
            m_rootItem = solutionExplorer.UIHierarchyItems.Item(1);
        }

        /// <summary>
        ///   Selects item in the Solution Explorer that corresponds to the 
        ///   file containing version information.
        /// </summary>
        /// <param name="projectName">
        ///   Name of the project in which item must be searched for.
        /// </param>
        /// <param name="filenames">
        ///   Array of filenames to select. If array is empty or <c>null</c> then
        ///   the root (i.e. node corresponding to the project is selected.
        /// </param>
        public void SelectItem(ProjectInfo projectInfo, string[] filenames) {
            Debug.Assert(m_rootItem != null);
            Debug.Assert(projectInfo.UIPath.Length > 0);
            string[] pathToProjectRoot = projectInfo.UIPath.Split('\\');
            UIHierarchyItem projectRoot = m_rootItem;
            foreach (string pathComponent in pathToProjectRoot) {
                projectRoot = projectRoot.UIHierarchyItems.Item(pathComponent);
                Debug.Assert(projectRoot != null);
            }
            if (filenames == null || filenames.Length == 0) {
                SelectUIHierarchyItem(projectRoot);
                return;
            }
            foreach (string filename in filenames) {
                RecurseProjectTree(projectRoot, filename);
            }
        }

        /// <summary>
        ///   Recurses project tree in Solution explorer, searching for the 
        ///   item for which path is provided. When found, item is selected.
        /// </summary>
        /// <param name="projectInfo">
        ///   <c>ProjectInfo</c> of the project for which AssemblyInfo file has 
        ///   to be selected.
        /// </param>
        /// <param name="parentNode">
        ///   Parent node in the Solution Explorer from which search is 
        ///   started.
        /// </param>
        /// <param name="filename2select">
        ///   Full name of the file to select.
        /// </param>
        /// <returns>
        ///   Returns <c>true</c> if file has been found and selected; used to
        ///   break recursion if file has been found.
        /// </returns>
        private UIHierarchyItem RecurseProjectTree(UIHierarchyItem parentNode, string filename2select) {
            Debug.Assert(parentNode != null);
            foreach (UIHierarchyItem child in parentNode.UIHierarchyItems) {
                ProjectItem projectItem = child.Object as ProjectItem;
                if (projectItem != null) {
                    string fullPath = ProjectItemInfo.GetItemFullPath(projectItem);
                    if (string.Compare(fullPath, filename2select, true) == 0) {
                        SelectUIHierarchyItem(child);
                        return child;
                    }
                    UIHierarchyItem item = RecurseProjectTree(child, filename2select);
                    if (item != null)
                        return item;
                }
            }
            return null;
        }

        /// <summary>
        ///   Selects an item in SolutionExplorer windows.
        /// </summary>
        /// <param name="item">
        ///   Item to select.
        /// </param>
        private void SelectUIHierarchyItem(UIHierarchyItem item) {
            m_selectedItemsCount++;
            if (m_selectedItemsCount > 1)
                item.Select(vsUISelectionType.vsUISelectionTypeToggle);
            else
                item.Select(vsUISelectionType.vsUISelectionTypeSelect);
            Debug.Assert(item.IsSelected);
        }

        private UIHierarchyItem m_rootItem = null;

        private int             m_selectedItemsCount = 0;

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Croatia Croatia
Graduated at the Faculty of Electrical Engineering and Computing, University of Zagreb (Croatia) and received M.Sc. degree in electronics. For several years he was research and lecturing assistant in the fields of solid state electronics and electronic circuits, published several scientific and professional papers, as well as a book "Physics of Semiconductor Devices - Solved Problems with Theory" (in Croatian).
During that work he gained interest in C++ programming language and have co-written "C++ Demystified" (in Croatian), 1st edition published in 1997, 2nd in 2001, 3rd in 2010, 4th in 2014.
After book publication, completely switched to software development, programming mostly in C++ and in C#.
In 2016 coauthored the book "Python for Curious" (in Croatian).

Comments and Discussions