left-icon

Visual Studio Add-Ins Succinctly®
by Joseph D. Booth

Previous
Chapter

of
A
A
A

CHAPTER 9

Projects

Projects


In this chapter, we are going to create an add-in module which will generate an HTML document providing technical details about the project. It will save the HTML file to disk and open it in a browser window to be viewed from within Visual Studio.

Project Info Wizard

Start your Project Info add-in using the wizard and the following settings:

  • Visual C# (or your preferred language).
  • Application Host: Only Visual Studio.
  • Name/Description: ProjectInfo and Generate HTML project documentation.
  • Create UI Menu and do not load at start-up.

Verify the settings at the Summary screen, and if they look okay, generate the code.

Exec() method

The Exec() method will collect the projects associated with the solution and present them to the user, but first, we need to make sure a solution is open.

// Only makes sense if a solution is open.

if (_applicationObject.Solution.IsOpen==false)

    {

       MessageBox.Show("No solution is open","ERROR");

      handled = true;

      return;

    }

Note: Be sure to add a reference to System.Windows.Forms to your project. You will also need to add VSLangProj and disable the Embed Interop types property.

Once we know we have an open solution, we can start to build HTML documentation.

// Find all project information.

   Solution theSol = _applicationObject.Solution;

   StringBuilder sb = new StringBuilder();

   string shortName = Path.GetFileNameWithoutExtension(theSol.FullName);

   sb.AppendLine("<html>");

   sb.AppendLine("<head>");

   sb.AppendLine("<title>" + shortName + "</title>");

   AddJavaScript(sb);

   sb.AppendLine("</head>");

   sb.AppendLine("<body>");

   sb.AppendLine("<h1>" +  shortName  + " solution</h1>");

Notice the AddJavaScript() function in the middle, which we will use to write some simple JavaScript functionality in our webpage. We will add this function toward the end of the chapter.

Tip: If you want to learn JavaScript quickly, be sure to download JavaScript Succinctly from the Syncfusion website. jQuery Succinctly is another excellent reference book.

Getting each project

Using the solution object as a starting point, we can write code that will loop through all projects and output some basic project information to the HTML file we are building.

VSProject theVSProj = null;

Project theProj;

for (int xx=1;xx<=theSol.Projects.Count;xx++)

{

    theProj = theSol.Projects.Item(xx);

    theVSProj = (VSProject)theSol.Projects.Item(xx).Object;

    sb.AppendLine("<h2 onclick='ToggleDiv(\"proj"+xx.ToString()+"\");'>" +

                   theProj.Name+"</h2>");

    sb.AppendLine("<div id='proj" + xx.ToString() + "' style='display:none;'>");

    sb.AppendLine("<h3 onclick='ToggleDiv(\"info" + xx.ToString() + "\");'>INFO</h3>");

    sb.AppendLine("<div id='info" + xx.ToString() + "'>");

    sb.AppendLine("<p>Unique Name: " + theProj.UniqueName + "</br>");

    sb.AppendLine("Full Path: " + theProj.FullName + "</br>");

    // Report language

    if (theProj.Kind==VSLangProj.PrjKind.prjKindCSharpProject)

       { sb.AppendLine(" Language: C#</p>"); }

    if (theProj.Kind == VSLangProj.PrjKind.prjKindVBProject)

       { sb.AppendLine(" Language: Visual Basic</p>"); }

    sb.AppendLine("</div>");

We get the Item() project and create two project variables from it. The first, the Project object type, is a generic project reference, providing us with basic information such as name, path name, unique name, etc. The second variable, the VSProject object type, is a Visual Studio project, and contains additional properties and methods unique to Visual Studio.

Project type

The project type has general properties we can use to display the project info:

  • Name: Short name of project.
  • FullName: Full name and path of project.
  • Unique Name: Namespace and unique project name.

VSProject type

The VSProject type has more detailed information, specifically in Visual Studio. We can access this information to display all the references that the project uses. The following code sample places the references into a list structure in our HTML documentation.

References

This example code includes the reference name and description, version number, and whether the reference is an ActiveX control or an assembly.

sb.AppendLine("<h3 onclick='ToggleDiv(\"ref" + xx.ToString() + "\");'>REFERENCES</h3>");

sb.AppendLine("<div id='ref" + xx.ToString() + "'>");

sb.AppendLine("<ul>");

foreach (Reference theRef in theVSProj.References)

{

   string theVer = theRef.BuildNumber.ToString();

   if (theVer == "0")

   {theVer = theRef.MajorVersion.ToString() + "." + theRef.MinorVersion.ToString(); }

   sb.Append("<li>" + theRef.Name + " (" + theVer + ")");

   if (theRef.Description.Length > 0)

   { sb.Append(" -" + theRef.Description); }

   if (theRef.Type == prjReferenceType.prjReferenceTypeActiveX)

   { sb.Append(" [ActiveX] "); }

   sb.AppendLine("</li>");

}

sb.AppendLine("</ul>");

sb.AppendLine("</div>");

Project Items

Each project contains a list of all of the items that make up that project. These can be accessed via the Project Items property. The project items consist of the various source code files that make up the project. These can include source files, XML files, etc. If the file is a source code file, Visual Studio provides a code model which allows our code to access the namespace, classes, etc. within that file. We will use the code model to show the classes with a source file in this chapter, but cover the code model in more depth in Chapter 13.

sb.AppendLine("<h3 onclick='ToggleDiv(\"items" + xx.ToString() + "\");'>ITEMS</h3>");

sb.AppendLine("<div id='items" + xx.ToString() + "' style='display:none;'>");

sb.AppendLine("<ul>");

FileCodeModel theCM = null;

foreach (ProjectItem theItem in theProj.ProjectItems)

{

   sb.AppendLine("<li>" + theItem.Name);

   theCM = theItem.FileCodeModel;

   if (theCM != null)

      {

        sb.AppendLine("<ul>");

        foreach (CodeElement theElt in theCM.CodeElements)

        {   // List all the classes we find within the code file.

            if (theElt.Kind == vsCMElement.vsCMElementClass)

              { sb.AppendLine("<li>"+theElt.Name+"</li>"); }

            // If we find a namespace, there may be a class in there as well.

            if (theElt.Kind == vsCMElement.vsCMElementNamespace)

             {

                  foreach (CodeElement theInnerElt in theElt.Children)

                  {

                       string theNameSpace = theElt.Name;

                       if (theInnerElt.Kind == vsCMElement.vsCMElementClass)

                         { sb.AppendLine("<li>" + theNameSpace+"/"+

                           theInnerElt.Name + "</li>"); }

                   }

             }

        }

        sb.AppendLine("</ul>");

    }

    sb.AppendLine("</li>");

}

sb.AppendLine("</ul>");

sb.AppendLine("</div>");

For every project item, we first include the name in the list we are building. We also check to see if a code model exists for the current file (which one should for C# and VB modules). If we find a code model, we iterate through the code elements, looking for either class entities or classes within namespaces. For each class we encounter, we include the class name and optionally the namespace.

This allows our project HTML display to drill down to the class level for a given project. A sample list of items that will be generated is shown in the following list. This is a C# project that has assembly information and two source files. One file has a single class called ComputePayrollAmount and the other source file has a class called Connect within a namespace called CodeModelSample:

ITEMS

  • AssemblyInfo.cs
  • Class1.cs
  • ComputePayrollAmount
  • Connect.cs
  • CodeModelSample/Connect

If your add-in is going to do any type of source code manipulation, be sure to explore the code model object described in Chapter 13.

Adding the JavaScript

The following function adds the JavaScript to the HTML header to allow us to toggle various project elements.

private void AddJavaScript(StringBuilder sb)

   {

       sb.AppendLine("<script type='text/javascript'>");

       sb.AppendLine("function ToggleDiv(theID) ");

       sb.AppendLine("{");

       sb.AppendLine("var e = document.getElementById(theID);");

       sb.AppendLine("if(e.style.display == 'block')");

       sb.AppendLine("   e.style.display = 'none';");

       sb.AppendLine("else");

       sb.AppendLine("   e.style.display = 'block';");

       sb.AppendLine("}");

       sb.AppendLine("</script>");

   }

Showing the Results

Once the HTML file is generated, the following code saves it and then displays it.

string theFile = Path.ChangeExtension(theSol.FullName, "html");

System.IO.File.WriteAllText(theFile, sb.ToString());

System.Diagnostics.Process.Start("file://"+theFile);

Styling the HTML

The generated HTML is rather plain looking, but you can add some style sheet commands to improve the look and feel of the document. The following code is a function to add style commands to the HTML document.

private void AddStyles(StringBuilder sb)

{

  sb.AppendLine("h2 {");

  sb.AppendLine("font: bold italic 2em/1em \"Times New Roman\",

                  \"MS Serif\", \"New York\", serif;");

  sb.AppendLine("margin: 0;");

  sb.AppendLine("padding: 0;");

  sb.AppendLine("border-top: solid #e7ce00 medium;");

  sb.AppendLine("border-bottom: dotted #e7ce00 thin;");

  sb.AppendLine("width: 600px;");

  sb.AppendLine("color: #e7ce00;");

  sb.AppendLine("}");

}

In you want to use such a function, insert the function call immediately after the AddJavaScript() function call.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.