Get a list of files in Solution / Project using the DXCore console application

I understand that the following snippets can be used to extract VS-VS information when used in the plug-in.

EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution; EnvDTE.Projects projects = solution.Projects; 

Q: I would like to create a console application and access these files. My goal is to create a console application (which can be run without VS) to create a report based on the design issues that I find in the input .sln file. What features do I use for this?

+4
source share
1 answer

Original (and updated) mail is here .

Actually, DXCore is not intended to be used outside of Visual Studio, but there are always workarounds ... In this article I will tell you how to use the DXCore Framework inside a regular C # console application to analyze the full solution and work with the abstract syntax tree. The solution should be passed as an argument to the program as the full path to the * .sln file. If the argument is not used, the hard-coded path to the test program is used, so the program will analyze itself and print information about the solution, such as a list of all types used and the number of members within each class.

Create a new C # console application, call it TestDXCoreConsoleApp and save it in the "C: \ Project" folder:

Creating a new Console App

Then we must change the Target Framework version of the new project to Framework 4.0, so this is not a β€œTarget Framework 4.0 Client Profile,” because some required assembly references do not support this version of the Target Framework:

Settings Target Framework

Now add the necessary assembly references. Here is a list of what we need:

1) DXCore builds:

  • DevExpress.CodeRush.Common
  • DevExpress.CodeRush.Core
  • DevExpress.CodeRush.StructuralParser
  • DevExpress.CodeRush.VSCore
  • DevExpress.DXCore.AssemblyResolver
  • DevExpress.DXCore.Parser

These assemblies can be found in the DevExpress IDE tools installation folder. For example, a path might look like this:

C: \ Program Files \ DevExpress 2011.1 \ IDETools \ System \ DXCore \ BIN

2) Now there are three additional assemblies to support various programming languages:

  • DX_CPPLanguage
  • DX_CSharpLanguage
  • DX_VBLanguage

Using these assemblies, we can parse CSharp, Visual Basic, and C ++ projects. They can be found here:

C: \ Program Files (x86) \ DevExpress 2011.1 \ IDETools \ System \ DXCore \ BIN \ SYSTEM

3) .NET Framework assemblies:

  • Microsoft.Build.BuildEngine.dll

4) And finally, several builds of Visual Studio:

  • Envdte
  • Vslangproj

These two can be found in the "PublicAssemblies" folder:

C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ PublicAssemblies \

Now, the DXCore support code. This code is required to download the solution, its projects and initialize the DXCore parsers. I added two folders:

1) The "Helpers" folder contains the following classes:

  • LanguageHelper.cs - defines the language of the projects (for example, CSharp, Visual Basic or C ++).
  • ParserHelper.cs - initializes the DXCore parser and several important DXCore services - the source model service and the language service, which are used to analyze the source code.
  • SolutionParser.cs is a helper class that takes the path to the solution that you are going to analyze. Calling the GetParsedSolution method will return a SolutionElement that contains the abstract source tree of the entire solution.

2) The Loaders folder contains the loaders of Visual Studio projects and applications for different versions of Visual Studio. They are used to parse * .XXproj and * .sln files. There are versions for VS2002, VS2003 and VS2005. There are no specialized loaders for VS2008 and VS2010, because these loaders for older versions of VS are great for reading and loading new project files and Visual Studio project files (for example, 2008, 2010).

Here is the final structure of TestDXCoreConsoleApp:

DXCore ConsoleApp structure

TestDXCoreConsoleApp with full source code here (267,457 bytes, C #, VS2010), so you can view the code and use it as you like. Here is the main function of the program class:

 static void Main(string[] args) { string SolutionPath; if (args != null && args.Length > 0) SolutionPath = args[0]; else SolutionPath = @"c:\Projects\TestDXCoreConsoleApp\TestDXCoreConsoleApp.sln"; try { ParserHelper.RegisterParserServices(); Console.Write("Parsing solution... "); SolutionParser solutionParser = new SolutionParser(SolutionPath); SolutionElement solution = solutionParser.GetParsedSolution(); if (solution == null) return; Console.WriteLine("Done."); foreach (ProjectElement project in solution.AllProjects) foreach (SourceFile file in project.AllFiles) foreach (TypeDeclaration type in file.AllTypes) { Console.Write(type.FullName); Console.WriteLine(", members: " + ((ITypeElement)type).Members.Count); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { ParserHelper.UnRegisterParserServices(); } Console.ReadLine(); } 

If you put the sources in the "C: \ Projects" folder and run the program without any arguments, you should see the following result:

DXCore Console App result

Press Enter to close the window. Keep in mind that the parsing process may take some time, so it may take you a few seconds until the entire solution is parsed.

+8
source

All Articles