An ADR Visual Studio Extension – Creating a Visual Studio Extension to Scrape the Solution and List all Items
Tags: csharp, extensions, architecture
Published: 2019-09-19 17:31:06

---
title: An ADR Visual Studio Extension – Creating a Visual Studio Extension to Scrape the Solution and List all Items
published: true
description: An ADR Visual Studio Tool - Part 1 - Creating a Visual Studio Extension to Scrape the Solution and List all Items, Including Solution Items and Solution Folders
tags: csharp, extensions, architecture
canonical_url: https://www.pmichaels.net/2019/09/07/an-adr-visual-studio-tool-part-1-creating-a-visual-studio-extension-to-scrape-the-solution-and-list-all-items-including-solution-items-and-solution-folders/
series: Creating A Visual Studio Extension
---

A while back, a colleague of mine brought the concept of <a href="https://adr.github.io/">ADRs</a> to my attention.  The idea being that, when you make a decision on a project, you write it down, but you do so inside the code base, and check it into the source control system.  

Even in the days when people believed writing long functional specifications was a good idea, having documentation that married up to the code it documented was a distant dream.  Typically, you'd spend about a week writing a spec, and the minute you wrote the first line of code, the document was, essentially, considered dead (and only ever referred back to where the customer disputed what had been delivered).

Since I've never written a Visual Studio Extension, but always thought it would be a cool idea, I had an idea to start with this.  My thought was that I could build something that would extract the ADRs from the main codebase.  This isn't one of those posts where I have a completed solution, and I'm just documenting it… it's more of an ongoing journey… which may result in the conclusion that this either doesn't make sense, isn't feasible, or has already been done.

I'm going to upload the progress so far to <a href="https://github.com/pcmichaels/ADRManager/tree/master/ADRManager">this GitHub repo</a>.

In this first post, we'll create an extension capable of viewing the project it's in.

<h1>Step 1 - Install the SDK</h1>

To do any extension development, you need to install the SDK - you can do this through the Visual Studio Installer:

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-1.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-1.png" alt="" width="860" height="235" class="alignnone size-full wp-image-3904" /></a>

<h1>Step 2 - Create a new (VSIX) project</h1>

VS Extensions are referred to as VSIX, because that's the extension of the deployable product.

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-2.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-2.png" alt="" width="1538" height="1022" class="alignnone size-full wp-image-3903" /></a>


<h1>Step 3 - Add a new Tool Window and Test</h1>

Add a new Item (right click project -> add new item), and select the Tool Window:

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-3.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-3.png" alt="" width="939" height="652" class="alignnone size-full wp-image-3902" /></a>

There is no need to do any plumbing here - any eligible extension types in the solution will be compiled and used - try pressing F5 now.  You should get a version of Visual Studio to debug:

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-4.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-4.png" alt="" width="1028" height="713" class="alignnone size-full wp-image-3901" /></a>

As you can see, I've been here before.  For the purposes of testing, I've set-up a convoluted project:

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-5.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-5.png" alt="" width="261" height="467" class="alignnone size-full wp-image-3900" /></a>

The reason for this will become clear shortly, for now, just launch the tool window that you created (View -> Other Windows -> Tool Window 1 (or whatever you called it):

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-6.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-6.png" alt="" width="458" height="457" class="alignnone size-full wp-image-3899" /></a>

<h1>Step 4 - Add some code to the Tool Window</h1>

For the purpose of this first stage, we'll just analyse the project structure.  When it's finished, I'd like it to be able to identify the ADR docs based on a configurable location but, for now, let's just show how many projects and files we have.  For now, we won't change anything, let's just hook into the button click of the subtle button in the screenshot above:

        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
            var dte = (DTE)Package.GetGlobalService(typeof(DTE));            

            var sln = Microsoft.Build.Construction.SolutionFile.Parse(dte.Solution.FullName);
            projectsText.Text = $"{sln.ProjectsInOrder.Count.ToString()} projects";

            foreach (Project p in dte.Solution.Projects)
            {
                projectsText.Text += $"{Environment.NewLine} {p.Name} {p.ProjectItems.Count}";
            }
        }


<em>SwitchToMainThreadAsync</em> is because any interaction with the solution needs to be on the main thread.  After that, we parse the solution file and output the name and items in each "project":

<a href="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-7.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/08/extension-7.png" alt="" width="453" height="456" class="alignnone size-full wp-image-3898" /></a>

As you can see, it classes each top level folder as a solution project, which will be ideal for us.

<h1>Summary</h1>

In this post, we've seen how to create a Visual Studio Extension, and how to trawl the current solution and projects.  In the next post, we'll try to extract some ADR specific stuff.

<h1>References</h1>

<a href="https://docs.microsoft.com/en-us/visualstudio/extensibility/installing-the-visual-studio-sdk?view=vs-2017">https://docs.microsoft.com/en-us/visualstudio/extensibility/installing-the-visual-studio-sdk?view=vs-2017</a>

<a href="https://msdn.microsoft.com/en-us/library/ms973240.aspx?f=255&MSPPError=-2147217396">https://msdn.microsoft.com/en-us/library/ms973240.aspx?f=255&MSPPError=-2147217396</a>

<a href="https://docs.microsoft.com/en-us/visualstudio/extensibility/starting-to-develop-visual-studio-extensions?view=vs-2017">https://docs.microsoft.com/en-us/visualstudio/extensibility/starting-to-develop-visual-studio-extensions?view=vs-2017</a>

<a href="http://www.visualstudioextensibility.com/articles/packages/">http://www.visualstudioextensibility.com/articles/packages/</a>

This article was originally posted <a href="https://www.pmichaels.net/2019/09/07/an-adr-visual-studio-tool-part-1-creating-a-visual-studio-extension-to-scrape-the-solution-and-list-all-items-including-solution-items-and-solution-folders/">here</a>.