Click here to Skip to main content
15,895,192 members
Articles / Programming Languages / C#

Visual Studio .NET 2003 Add-in that adds diff tools, an explore command, Subversion support and web project reporting. Version 2.1

Rate me:
Please Sign up or sign in to vote.
4.88/5 (25 votes)
27 Jul 200528 min read 219.9K   2.8K   142  
With this add-in, you get new tools and commands that boost your productivity while developing and some helpful reports specially for web projects. Version 2.1.
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Windows.Forms;

using Extensibility;
using EnvDTE;

using my.utils; // the real diff-work is done there !

namespace WebReports {

  /// <summary>Summary description for BackupFile.</summary>
  internal class BackupFile : Report {

    /// <summary>This is used as a suggested folder, when prompting is needed.</summary>
    const string MyPreferredRoot = @"C:\temp\";

    /// <summary>Create a BackupFile object.</summary>
    public BackupFile() {
      WebReports.Options.DefineTextOption("BackupFolder", String.Empty);
    } // BackupFile


    /// <summary>The only argument that is needed is the file to be backuped</summary>
    public override int Arguments {
      get { return (1); }
    }


    /// <summary>write one line of the diff-output.</summary>
    /// <param name="output">output stream for html</param>
    /// <param name="nr1">line-number of first file</param>
    /// <param name="nr2">line-number of second file</param>
    /// <param name="typ">kind of output (i, d or null)</param>
    /// <param name="aText">text in the textline of the file</param>
    private static void WriteDiffLine(StringWriter output, int nr1, int nr2, string typ, string aText) {
      string tdText;
      if (typ == null)
        tdText = "<td>";
      else
        tdText = "<td class='" + typ + "'>";

      output.Write("<tr class='code'>");
      if (nr1 >= 0)
        output.Write(tdText + (nr1+1).ToString());
      else
        output.Write(tdText + "(" + (nr2+1).ToString() + ")");
      output.Write("</td>");
      output.Write(tdText + System.Web.HttpUtility.HtmlEncode(aText) + "</td></tr>\n");
    } // WriteDiffLine


    /// <summary>Do the analysis by reading 2 files, compare them and render html to the output.</summary>
    /// <param name="applicationObject">reference to the VS.NET</param>
    /// <param name="output">ouput stream</param>
    /// <param name="args">List of all arguments passed to this command.</param>
    public override void Render(_DTE applicationObject, StringWriter output, params string[] args) {
      if (args.Length < 1)
        throw new ArgumentException(@"Select a files to backup.");

      string aFile = args[0].ToLower();
      string aBackupDir = null;
      string targetFile = null;

      if (args.Length > 1) {
        aBackupDir = args[1];
      } else {
        aBackupDir = Options.GetTextOption("BackupFolder");
      } // if

      if (aBackupDir.Length == 0) {
        // ask for a backup folder
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.Description = "Select a folder to store the backup files..";
        fbd.SelectedPath = MyPreferredRoot;
        if (fbd.ShowDialog() == DialogResult.OK)
          aBackupDir = fbd.SelectedPath;
        else
          new ArgumentException(@"No Backup folder was selected."); // do not execute this command on cancel.
      } // if

      if (aBackupDir == null)
        throw new ArgumentException("A backup root folder is required.", "Backup Root Folder");
      if (! aBackupDir.EndsWith("\\")) aBackupDir += "\\";

      WebReports.Options.DefineTextOption("BackupFolder", aBackupDir);

      string projectRoot = Report.CurrentProjectFolder(applicationObject);
      if ((projectRoot != null) && (aFile.StartsWith(projectRoot))) {
        // strip of the root folder of the current project and append to the Backup root
        targetFile = aBackupDir + aFile.Substring(projectRoot.Length);

      } else if (aFile.Substring(1,2) == @":\") {
        // strip of the drive letter and append to the Backup root
        targetFile = aBackupDir + aFile.Substring(3);

      } else if (aFile.StartsWith(@"\\")) {
        // strip of the server & share name
        aFile = aFile.Substring(aFile.IndexOf("\\", 2)+1);
        aFile = aFile.Substring(aFile.IndexOf("\\")+1);
        targetFile = aBackupDir + aFile;
      } else {
        throw new ArgumentException("This is not a filename: " + aFile, "FileName");
      } // if

      if (! (new FileInfo(aFile).Exists))
        throw new ArgumentException("The file does not exist: " + aFile, "FileName");

      // be shure that the target folder exists
      DirectoryInfo di = new FileInfo(targetFile).Directory;
      if (! di.Exists)
        di.Create();

      // now copy
      ShellLib.ShellFileOperation.DeleteFile(targetFile);
      File.Copy(aFile, targetFile, true);

      System.Diagnostics.Debug.WriteLine("Backup file " + aFile + " to " + targetFile, "WebReports.BackupFile");

      string html = ExtractHtmlResource("WebReports.WebReports.BackupFile.htm");
      html = html.Replace("[FILENAME]", System.Web.HttpUtility.HtmlEncode(aFile));
      html = html.Replace("[TARGETFILENAME]", System.Web.HttpUtility.HtmlEncode(targetFile));
      html = html.Replace("[BACKUPROOT]", System.Web.HttpUtility.HtmlEncode(aBackupDir));

      output.Write(html);
    } // Render

  } // class

} // namespace

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Deutsche Bank AG
Germany Germany
see https://www.mathertel.de

Comments and Discussions