Click here to Skip to main content
Licence MIT
First Posted 1 Jan 2005
Views 36,474
Bookmarked 24 times

Remove SCC information revisited.

By | 1 Jan 2005 | Article
Removes SCC information from a solution or project file. Works with solutions, C#, VB.NET, ASP.NET, deployment projects, and project templates.

Sample Image - RemoveSCCInfo2.jpg

Introduction

Like mcarbenay, I needed and still need to remove or change source control information for a project or solution. If you have ever done it, you know it's a pain even if it really shouldn't be. The way it is hard coded into the project and solution files is pretty much ridiculous and one can spend a long time trying to do such a simple thing, especially with web applications.

So of course, I looked to see if someone already got frustrated enough to make a nice tool that does it all for me. And there was one! You can get the corresponding article here. Unfortunately, that tool doesn't handle many project types. In particular, template projects (*.etp) are not processed. Also, the way web projects are processed works about two thirds of the time for me.

Following these observations, I downloaded the source to see if I could patch it to suit my needs and then give back my contribution. Well, things being as they are, I figured it would take me less time to code it from scratch, taking into account that I would like to support even more project types in the near future and easily upgrade the parsing code for any new VS.NET versions.

So here you have the result: a tiny 200 lines program that relies heavily on regular expressions. There's not much more to say about it than that. I think the code speaks for itself, so I simply copied it in the following section.

Using the code

#region Using directives

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.DirectoryServices;
using System.IO;
using System.Text.RegularExpressions;

#endregion

namespace RemoveSCCInfo
{
    /// <SUMMARY>
    /// Utility class to remove SCC informations from solutions and project files.
    /// </SUMMARY>
    public sealed class SCCInfoRemover
    {
        #region Constructors

        /// <SUMMARY>
        /// Enforces singleton status.
        /// </SUMMARY>
        private SCCInfoRemover()
        {
        }

        #endregion

        #region Methods

        /// <SUMMARY>
        /// Removes the SCC informations from a solution or project file.
        /// If the path points to a solution file,
        /// any referenced project file will also be processed.
        /// </SUMMARY>
        /// <PARAM name="path">The full path
        ///      of the solution or project file.</PARAM>
        /// <PARAM name="backup">true if solution and project files
        /// will be copied to a backup directory, false otherwise.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///        <PARAMREF name="path" /> is a null reference or an empty string.
        /// </EXCEPTION>
        public static void RemoveSCCInfo(string path, bool backup)
        {
            if (path == null || path == "")
                throw new ArgumentNullException("path");

            if (!File.Exists(path))
                throw new FileNotFoundException("Cannot" + 
                " find the path specified.", path);

            if (Path.GetExtension(path).ToLower() == ".sln")
                CleanSolution(path, backup);
            else
                CleanProject(path, backup);
        }

        /// <SUMMARY>
        /// Removes SCC informations from a solution file.
        /// Any referenced project file will also be processed.
        /// </SUMMARY>
        /// <PARAM name="path">The full path of the solution file.</PARAM>
        /// <PARAM name="backup">true if solution and project
        /// files will be copied to a backup directory, false otherwise.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///  <PARAMREF name="path" /> is a null reference or an empty string.
        /// </EXCEPTION>
        private static void CleanSolution(string path, bool backup)
        {
            if (path == null || path == "")
                throw new ArgumentNullException("path");

            if (!File.Exists(path))
                throw new FileNotFoundException("Cannot" + 
                " find the path specified.", path);

            // Load solution file

            string solution;

            using (StreamReader sr = new StreamReader(path))
            {
                solution = sr.ReadToEnd();
            }

            // Parse SCC informations section, if any

            Regex regex = new Regex(@"
                \tGlobalSection\(SourceCodeControl\).*\r\n #begin of SCC section
                (
                (.+\r\n)*?             #any number of lines
                \t\tSccProjectUniqueName\d+\s*=\s*(?<name>.+)\r\n  #unique name
                (.+\r\n)*?             #any number of lines
                \t\tSccLocalPath\d+\s*=\s*(?<local>.+)\r\n         #local name
                )*                #rinse and repeat for all projects
                (.+\r\n)*?        #any number of lines
                \tEndGlobalSection\r\n  #end of SCC section
                ", RegexOptions.IgnoreCase | RegexOptions.Multiline | 
                   RegexOptions.ExplicitCapture | 
                   RegexOptions.IgnorePatternWhitespace);
            Match m = regex.Match(solution);

            if (m.Success)
            {
                // Wil contains referenced projects in SCC section
                // key : SccProjectUniqueName
                // value : SccLocalPath
                HybridDictionary projects = new HybridDictionary();

                Group nameGroup = m.Groups["name"];
                Group localGroup = m.Groups["local"];

                for (int i = 0; i < nameGroup.Captures.Count; i++)
                    projects.Add(nameGroup.Captures[i].Value.Replace(@"\\", 
                                 @"\"), localGroup.Captures[i].Value);

                string solutionDir = Path.GetDirectoryName(path);

                // Process referenced projects

                foreach (DictionaryEntry project in projects)
                {
                    string name = (string) project.Key;
                    string local = (string) project.Value;

                    // Check if web project
                    if (name.StartsWith("http://"))
                    {
                        // Try to use local name
                        if (!local.StartsWith("http://"))
                        {
                            int pos = name.LastIndexOf('/') + 1;
                            string fileName = 
                              name.Substring(pos, name.Length - pos);

                            CleanProject(Path.Combine(solutionDir, local + 
                               Path.DirectorySeparatorChar + fileName), backup);
                        }
                        else
                        {
                            // Local name is also web location,
                            // so use get local name by other means

                            local = GetLocalPath(name);

                            if (local != null)
                                CleanProject(local, backup);
                        }
                    }
                    else
                    {
                        // Local project
                        CleanProject(Path.Combine(solutionDir, name), backup);
                    }
                }

                if (backup)
                    BackupFiles(path);

                RemoveReadOnlyAttribute(path);

                using (StreamWriter sw = new StreamWriter(path, false))
                {
                    sw.Write(solution.Replace(m.Value, ""));
                }

                CleanSCCFiles(path, backup);
            }
        }

        /// <SUMMARY>
        /// Removes SCC informations from a solution file.
        /// </SUMMARY>
        /// <PARAM name="path">The full path of the project file.</PARAM>
        /// <PARAM name="backup">true if project files will be copied
        /// to a backup directory, false otherwise.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///        <PARAMREF name="path" /> is a null reference or an empty string.
        /// </EXCEPTION>
        private static void CleanProject(string path, bool backup)
        {
            if (path == null || path == "")
                throw new ArgumentNullException("path");

            if (!File.Exists(path))
                throw new FileNotFoundException("Cannot" + 
                  " find the path specified.", path);

            // Load project file

            string project;

            using (StreamReader sr = new StreamReader(path))
            {
                project = sr.ReadToEnd();
            }

            // Parse SCC informations section, if any

            Regex regex = new Regex(@"^.*Scc\w+\s*=\s*.+\r\n", 
              RegexOptions.IgnoreCase | RegexOptions.Multiline | 
              RegexOptions.ExplicitCapture | 
              RegexOptions.IgnorePatternWhitespace);
            project = regex.Replace(project, "");

            regex = new Regex(@"
                .*<SOURCECONTROLSETTINGS>\r\n    #begin SCC settings section
                (.+\r\n)*?                          #any number of lines
                .*</SOURCECONTROLSETTINGS>\r\n    #end SCC settings section
                ", RegexOptions.IgnoreCase | RegexOptions.Multiline | 
                   RegexOptions.ExplicitCapture | 
                   RegexOptions.IgnorePatternWhitespace);

            project = regex.Replace(project, "");

            if (backup)
                BackupFiles(path);

            RemoveReadOnlyAttribute(path);

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                sw.Write(project);
            }

            CleanSCCFiles(path, backup);
        }

        /// <SUMMARY>
        /// Removes SCC related files, such as '*.scc', '*.vssscc' and '*.vspscc'
        /// </SUMMARY>
        /// <PARAM name="path">The full path
        /// of the solution or project directory.</PARAM>
        /// <PARAM name="backup">true if SCC related files
        /// will be copied to a backup directory, false otherwise.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///   <PARAMREF name="path" /> is a null reference or an empty string.
        /// </EXCEPTION>
        private static void CleanSCCFiles(string path, bool backup)
        {
            if (path == null || path == "")
                throw new ArgumentNullException("path");

            if (!File.Exists(path))
                throw new FileNotFoundException("Cannot" + 
                " find the path specified.", path);

            path = Path.GetDirectoryName(path);

            DeleteFiles(Directory.GetFiles(path, "*.scc"), backup);
            DeleteFiles(Directory.GetFiles(path, "*.vssscc"), backup);
            DeleteFiles(Directory.GetFiles(path, "*.vspscc"), backup);
        }

        /// <SUMMARY>
        /// Deletes a collection of files.
        /// </SUMMARY>
        /// <PARAM name="files">The files to be deleted.</PARAM>
        /// <PARAM name="backup">true if the files will be copied
        /// to a backup directory, false otherwise.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///        <PARAMREF name="files" /> is a null reference.
        /// </EXCEPTION>
        private static void DeleteFiles(string[] files, bool backup)
        {
            if (files == null)
                throw new ArgumentNullException("files");

            if (files.Length > 0)
            {
                if (backup)
                    BackupFiles(files);

                foreach (string file in files)
                {
                    RemoveReadOnlyAttribute(file);
                    File.Delete(file);
                }
            }
        }

        /// <SUMMARY>
        /// Backups one or more files.
        /// Files will be stored in a directory named 'Backup_YYYY-MM-DD'
        /// where YYYY-MM-DD is the current date in ISO format.
        /// </SUMMARY>
        /// <PARAM name="files">The files to backup.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///        <PARAMREF name="files" /> is a null reference.
        /// </EXCEPTION>
        private static void BackupFiles(params string[] files)
        {
            if (files == null)
                throw new ArgumentNullException("files");

            string backupDirName = Path.DirectorySeparatorChar + 
                   "Backup_" + DateTime.Now.ToString("yyyy-MM-dd") + 
                   Path.DirectorySeparatorChar;

            foreach (string file in files)
            {
                string dir = Path.GetDirectoryName(file) + backupDirName;

                Directory.CreateDirectory(dir);
                File.Copy(file, dir + Path.GetFileName(file), true);
            }
        }

        /// <SUMMARY>
        /// Removes the read-only attribute on a file.
        /// </SUMMARY>
        /// <PARAM name="path">The full path to the file.</PARAM>
        /// <EXCEPTION cref="ArgumentNullException">
        ///   <PARAMREF name="path" /> is a null reference or an empty string.
        /// </EXCEPTION>
        private static void RemoveReadOnlyAttribute(string path)
        {
            if (path == null || path == "")
                throw new ArgumentNullException("path");

            FileAttributes attributes = File.GetAttributes(path);

            if ((attributes & FileAttributes.ReadOnly) != 0)
                File.SetAttributes(path, attributes & (~FileAttributes.ReadOnly));
        }

        /// <SUMMARY>
        /// Gets the local path of a locally hosted web application.
        /// </SUMMARY>
        /// <PARAM name="url">The url of the application.</PARAM>
        /// <RETURNS>The local path of the locally hosted web application.
        /// A null value is returned if the local path
        /// cannot be obtained.</RETURNS>
        /// <EXCEPTION cref="ArgumentNullException">
        ///        <PARAMREF name="url" /> is a null reference or an empty string.
        /// </EXCEPTION>
        private static string GetLocalPath(string url)
        {
            if (url == null || url == "")
                throw new ArgumentNullException("url");

            string local = null;

            try
            {
                Uri uri = new Uri(url);

                string adsiPath = String.Format("IIS://{0}/W3SVC/1/Root{1}", 
                         uri.Host, Path.GetDirectoryName(uri.AbsolutePath));
                DirectoryEntry dirEntry = new DirectoryEntry(adsiPath);

                string wwwPath = dirEntry.Properties["Path"].Value.ToString();

                local = Path.Combine(wwwPath, Path.GetFileName(uri.AbsolutePath));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Source);
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
            }

            return local;
        }

        #endregion
    }
}

History

  • 1.0: supports C#, VB.NET, ASP.NET, deployment projects, and project templates.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Sebastien Lorion

Architect

Canada Canada

Member

Sébastien Lorion is software architect as day job.
 
He is also a musician, actually singing outside the shower Smile | :)
 
He needs constant mental and emotional stimulation, so all of this might change someday ...
 
You can visit his blog at http://sebastienlorion.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan't get it to work for VS 2002 PinmemberMabaitRoy20:23 12 Aug '08  
AnswerRe: Can't get it to work for VS 2002 PinmemberSebastien Lorion21:04 12 Aug '08  
QuestionRe: Can't get it to work for VS 2002 PinmemberMabaitRoy22:26 12 Aug '08  
GeneralRe: Can't get it to work for VS 2002 PinmemberMabaitRoy22:55 12 Aug '08  
AnswerRe: Can't get it to work for VS 2002 PinmemberSebastien Lorion9:24 13 Aug '08  
QuestionDoes this work on VS2005? PinmemberKeith Mallen9:33 21 Apr '08  
AnswerRe: Does this work on VS2005? PinmemberSebastien Lorion15:40 22 Apr '08  
Generalgreat tool, but... PinmemberPerica Zivkovic21:43 24 Oct '05  
GeneralRe: great tool, but... PinmemberSébastien Lorion6:45 6 Nov '05  
GeneralMan without fear PinsussAnonymous1:36 9 Aug '05  
GeneralThanks and some observations Pinmemberandrew mark stone18:33 11 Apr '05  
GeneralRe: Thanks and some observations PinmemberSébastien Lorion18:47 11 Apr '05  
Questions does it shows that much ? Pinmembermcarbenay0:10 4 Jan '05  
AnswerRe: s does it shows that much ? PinmemberSébastien Lorion0:21 4 Jan '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Sebastien Lorion
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid