C# Code to Convert .NET solution file to NDepend Project file on The Continuous Integration Server


NDependis a .NET static code analyzer.

ndepend-dashboard C# Code to Convert .NET solution file to NDepend Project file on The Continuous Integration Server c # code programming languages windows

ndepend-dashboard

With this tool, developers can easily see the report of ‘bad’-written code and make the .NET code base beautiful and efficient. The NDepend also provides a console application so you can easily launch the analysis on the command line, which is easier to integrate into the Continuous Integration (CI) server.

However, the NDepend console application only takes a *.ndproj (NDepend project) rather than .NET *.sln (solution project). Sometimes, pre-building the *.ndproj from *.sln manually is not an option on the Build Server and even it is possible, it looks ugly.

So the following C# code will help you to convert such on the fly. It is a console application that takes two parameters. The first one is the source *.sln file and the second is the target *.ndproj file.

You would need to reference the DLL NDepend.API.dll which is located at the $(NDepend Installation Directory)\Lib. And you would need to set the copy-local attribute to false.

ndepend.api_.copy-local-false C# Code to Convert .NET solution file to NDepend Project file on The Continuous Integration Server c # code programming languages windows

ndepend.api.copy-local-false

And you would also need to put [STAThread] attribute in your main static method (entry of the application).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// https://HelloACM.com
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NDepend;
using NDepend.Path;
using NDepend.Project;
 
namespace RunNDepend
{
    class Program
    {
        [STAThread]
        static int Main(string[] args)
        {
            // check for parameters
            if (args.Length &lgt; 2)
            {
                Console.WriteLine("Two Parameters: solution ndproj");
                return 1;
            }
            if (!File.Exists(args[0]))
            {
                Console.WriteLine("File Not Found: " + args[0]);
                return 2;
            }
            if (File.Exists(args[1]))
            {
                Console.WriteLine("Deleting " + args[1]);
                File.Delete(args[1]);
            }
            // convert to IAbsoluteFilePath
            var sln = args[0].ToAbsoluteFilePath();
            var nproj = args[1].ToAbsoluteFilePath();
 
            var ndependServicesProvider = new NDependServicesProvider();
 
            var visualStudioManager = ndependServicesProvider.VisualStudioManager;
            var vsSlnOrProjFilePaths = new List<IAbsoluteFilePath> {sln};
 
            // read the assembly list
            var assembliesFilePath = (from vsSlnOrProjFilePath in vsSlnOrProjFilePaths
                                      from assembliesFilePathTmp in visualStudioManager.GetAssembliesFromVisualStudioSolutionOrProject(vsSlnOrProjFilePath)
                                      select assembliesFilePathTmp).Distinct().ToArray();
 
            var projectManager = ndependServicesProvider.ProjectManager;
 
            Console.WriteLine("Converting " + args[0] + " to " + args[1] + " ...");
            // converting
            IProject project = projectManager.CreateBlankProject(nproj, "NDepend");
            project.CodeToAnalyze.SetApplicationAssemblies(assembliesFilePath);
            projectManager.SaveProject(project);
            if (File.Exists(args[1]))
            {                 
                Console.WriteLine("Done!");
                return 0;
            }
            else
            {
                // if target *.ndproj does not exist after conversion, error occurs.      
                Console.WriteLine("Error!");
                return 3;
            }
        }
    }
}
// https://HelloACM.com
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NDepend;
using NDepend.Path;
using NDepend.Project;

namespace RunNDepend
{
    class Program
    {
        [STAThread]
        static int Main(string[] args)
        {
            // check for parameters
            if (args.Length &lgt; 2)
            {
                Console.WriteLine("Two Parameters: solution ndproj");
                return 1;
            }
            if (!File.Exists(args[0]))
            {
                Console.WriteLine("File Not Found: " + args[0]);
                return 2;
            }
            if (File.Exists(args[1]))
            {
                Console.WriteLine("Deleting " + args[1]);
                File.Delete(args[1]);
            }
            // convert to IAbsoluteFilePath
            var sln = args[0].ToAbsoluteFilePath();
            var nproj = args[1].ToAbsoluteFilePath();

            var ndependServicesProvider = new NDependServicesProvider();

            var visualStudioManager = ndependServicesProvider.VisualStudioManager;
            var vsSlnOrProjFilePaths = new List<IAbsoluteFilePath> {sln};

            // read the assembly list
            var assembliesFilePath = (from vsSlnOrProjFilePath in vsSlnOrProjFilePaths
                                      from assembliesFilePathTmp in visualStudioManager.GetAssembliesFromVisualStudioSolutionOrProject(vsSlnOrProjFilePath)
                                      select assembliesFilePathTmp).Distinct().ToArray();

            var projectManager = ndependServicesProvider.ProjectManager;

            Console.WriteLine("Converting " + args[0] + " to " + args[1] + " ...");
            // converting
            IProject project = projectManager.CreateBlankProject(nproj, "NDepend");
            project.CodeToAnalyze.SetApplicationAssemblies(assembliesFilePath);
            projectManager.SaveProject(project);
            if (File.Exists(args[1]))
            {                 
                Console.WriteLine("Done!");
                return 0;
            }
            else
            {
                // if target *.ndproj does not exist after conversion, error occurs.      
                Console.WriteLine("Error!");
                return 3;
            }
        }
    }
}

We have different return code to test for different situations on build server. You can test by using the following example,

ndepend-convert C# Code to Convert .NET solution file to NDepend Project file on The Continuous Integration Server c # code programming languages windows

ndepend-convert

Please note that the NDepend project files must end with the file extension *.ndproj or otherwise it will fail. We can then use simply the following command to run the analysis on the *.ndproj file.

NDepend.Console.exe Project.ndproj

Please note that our conversion tool must be put at the same folder with NDepend.API.dll or it will not work. After analysis, the default output directory (you can change with /OutDir command line directive) is NDependOut.

Finally the pre-compiled C# .NET code (without NDepend.API.dll) can be download RunNDepend.

Update: If you get a upgrade (I am eligible for a free v6 upgrade), this tool will not be compatible with previous versions. Here is what’ve got on the CI server.

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'NDepend.API, Version=5.4.1.8430, Culture=neutral, PublicKeyToken=02f4313b97e3f583' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at RunNDepend.Program.Main(String[] args)
Warning: C:\Jenkins\workspace\NDependOut\*.ndar not found
NDepend Report: Failed: C:\Jenkins\workspace\NDependOut\NDependReport.html (not generated)

The fix is to re-add the v6 NDepend.API.dll and rebuilt your converter solution. The error will just go away.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
795 words
Last Post: Four Useful Cell Functions/Values in Excel
Next Post: How to Run HTA as Administrator (Elevation)?

The Permanent URL is: C# Code to Convert .NET solution file to NDepend Project file on The Continuous Integration Server

Leave a Reply