Click here to Skip to main content
15,885,278 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:    ListExporterToPlainTextFile.cs
 * Product:     Versioning Controlled Build
 * Solution:    BuildAutoIncrement
 * Project:     Shared
 * Description: Exports projects list to a plain text file.
 * Copyright:   Julijan �ribar, 2004-2010
 * 
 * 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 System;
using System.IO;
using System.Text;

namespace BuildAutoIncrement {
	/// <summary>
	/// Summary description for PlainTextListExporter.
	/// </summary>
	public class ListExporterToPlainTextFile : ListExporterToTextFile {

		public ListExporterToPlainTextFile() {
		}

        override protected string ExportFileFilter {
            get {
                return "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            }
        }

        override protected string ExportFileExtension { 
            get {
                return ".vcb.txt"; 
            }
        }

        /// <summary>
        ///   Gets format string for output of project items.
        /// </summary>
        /// <param name="width">
        ///   Width required for string output.
        /// </param>
        /// <returns>
        ///   Formatting string.
        /// </returns>
        private string GetFormatString(int width) {
            return string.Format("{{0,-{0}}}", width);
        }

        /// <summary>
        ///   Gets separator line with width provided.
        /// </summary>
        /// <param name="width">
        ///   Width of the line.
        /// </param>
        /// <returns>
        ///   Entire separator line.
        /// </returns>
        private string GetSeparator(int width) {
            StringBuilder sb = new StringBuilder();
            sb.Append('-', width);
            return sb.ToString();
        }

        override protected void DoSave(string filename) {
            CollectColumnWidths();
            base.DoSave(filename);
        }

        /// <summary>
        ///   Writes header with solution name and current date and time.
        /// </summary>
        /// <param name="streamWriter">
        ///   <c>StreamWriter</c> used to write header.
        /// </param>
        override protected void WriteHeader(StreamWriter streamWriter) {
            streamWriter.WriteLine(m_solutionName);
            streamWriter.WriteLine(m_exportDateTime.ToString("g"));
            streamWriter.WriteLine();
        }

        /// <summary>
        ///   Writes heading with column captions.
        /// </summary>
        /// <param name="streamWriter">
        ///   <c>StreamWriter</c> used to write heading.
        /// </param>
        override protected void WriteHeading(StreamWriter streamWriter) {
            StringBuilder output = new StringBuilder();
            string format = GetFormatString(m_columnWidths[(int)ColumnName.ProjectName] + m_columnSpacing);
            output.AppendFormat(format, HeaderProjectName);
            foreach (AssemblyVersionTypeSelection avts in m_assemblyVersionTypes) {
                if (avts.IsSelected) {
                    format = GetFormatString(m_columnWidths[(int)ColumnName.Version] + m_columnSpacing);
                    output.AppendFormat(format, m_headings[avts.AssemblyVersionType]);
                }
            }
            streamWriter.WriteLine(output);
            streamWriter.WriteLine(GetSeparator(output.Length));
        }

        override protected void WriteItems(StreamWriter streamWriter) {
            foreach (ProjectInfo pi in m_projectInfoList) {
                if (pi.IsVersionable || !m_dontExportNonversionable) {
                    // set indentation
                    StringBuilder projectName = new StringBuilder(pi.ProjectName);
                    if (pi.Level > 0) 
                        projectName.Insert(0, " ", pi.Level);
                    StringBuilder output = new StringBuilder();
                    string format = GetFormatString(m_columnWidths[(int)ColumnName.ProjectName] + m_columnSpacing);
                    output.AppendFormat(format, projectName);
                    foreach (AssemblyVersionTypeSelection avts in m_assemblyVersionTypes) {
                        if (avts.IsSelected) {
                            format = GetFormatString(m_columnWidths[(int)ColumnName.Version] + m_columnSpacing);
                            output.AppendFormat(format, pi.CurrentAssemblyVersions[avts.AssemblyVersionType]);
                        }
                    }
                    streamWriter.WriteLine(output.ToString());
                }
            }
        }

        /// <summary>
        ///   Evaluates required widths of individual columns,
        /// </summary>
        private void CollectColumnWidths() {
            m_columnWidths = new int[Enum.GetValues(typeof(ColumnName)).Length];
            m_columnWidths[(int)ColumnName.ProjectName] = HeaderProjectName.Length;
            // find largest version column header
            foreach (AssemblyVersionTypeSelection avts in m_assemblyVersionTypes) {
                if (avts.IsSelected) {
                    int width = ((string)m_headings[avts.AssemblyVersionType]).Length;
                    if (width > m_columnWidths[(int)ColumnName.Version])
                        m_columnWidths[(int)ColumnName.Version] = width;
                }
            }
            foreach (ProjectInfo pi in m_projectInfoList) {
                int width = pi.ProjectName.Length + pi.Level;
                if (width > m_columnWidths[(int)ColumnName.ProjectName])
                    m_columnWidths[(int)ColumnName.ProjectName] = width;

                // go through all selected version types
                foreach (AssemblyVersionTypeSelection avts in m_assemblyVersionTypes) {
                    if (avts.IsSelected) {
                        width = pi.CurrentAssemblyVersions[avts.AssemblyVersionType].ToString().Length;
                        if (width > m_columnWidths[(int)ColumnName.Version])
                            m_columnWidths[(int)ColumnName.Version] = width;
                    }
                }
            }
        }

        private int[] m_columnWidths;

        private const int m_columnSpacing = 4;

    }
}

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