This content has moved - please find it at https://devblog.cyotek.com.

Although these pages remain accessible, some content may not display correctly in future as the new blog evolves.

Visit https://devblog.cyotek.com.

Creating an ndrules file for use with NDepend

A long time ago, I wrote of some initial experiences using NDepend. Since then, I've done little with the product, although I did integrate it with Jenkins for both pipeline and freestyle jobs and so every time the CI builds it executes NDepend. However, the NDepend configuration used by these jobs is (probably) significantly out of date.

As I've just upgraded NDepend to the current version, I thought I'd better update the configurations. Fortunately when I created NDepend projects, I always edited them to use an external rules file and to change all the paths to relative. Absolute paths don't work well in CI scenarios and it always felt very wrong to me to have every single project duplicating all the rules - updating them would be painful experience.

The rules file I created dates from January 2017; I'm sure that something has changed in that period so I wanted to update it. The only problem is I didn't make any notes on how I created the rules file and it wasn't an obvious task to perform. This blog post is mainly notes for Forgetful Future Richard but I feel the information is useful enough to warrant it being a public post.

Reasons to use an external file

The main reason I think an external file should be used is simply to make updating rules easier, and to avoid having everything duplicated.

Although I never got to that point, NDepend allows you to combine both project and external rules, so I reasoned I would have all the stock rules in one externally referenced file and then add application specific rules at the project level.

Creating a rules file from the GUI

NDepend doesn't seem to ship with an external rule file and so I need to manually create one. There also doesn't seem to be a straight forward way of creating one either, so the approach is a little convoluted. I will update this post if I find a better way.

These instructions are using the stand-alone Visual NDepend software, I haven't tested to see if the same applies when using the Visual Studio extension.

  1. Open Visual NDepend
  2. Open the File menu and select New Project
  3. From the New NDepend Project dialog, enter a dummy name and file name and click OK
  4. When the Project Properties window appears, click Add VS Solution or Project
  5. Click Browse and then choose any existing Visual Studio solution
  6. Press F5 to run the analysis
  7. When the analysis is complete, click the Rule File drop down in the Queries and Rules Explorer window and then click Create Rule File
  8. Enter the filename of the file and then click OK to create an empty file (make sure the filename is unique or you'll get an error)
  9. Right click Project Rules from the Queries and Rules Explorer tree and click Copy
  10. Find the node for the new rules file you just created, right click it and click Paste Group
  11. Exit Visual NDepend and delete the dummy project

Creating a rules file via text editor

There's a lot of steps to jump through in order to get default rules, you can make it slightly easier by doing some of the work using a text editor.

  1. Follow steps 1 to 3 above to create a dummy project
  2. Exit Visual NDepend
  3. Open the dummy project you created in a text editor of your choice
  4. Do a search for <Queries>
  5. Select from the <Queries> element to to the closing </Queries> value - this will be almost at the end of the file so you can just select the remainder of the document and backtrack a few characters
  6. Copy this into a new text file

This approach means you don't have to run a dummy analysis, and also means you don't end up with a superfluous Project Rules container group.

Updating a NDepend project to use a rules file

The GUI doesn't allow you to delete the default Project Rules group so I do this directly by editing the .ndproj file in a text editor, rather than having to delete each child group one by one.

  • Open a NDepend project in a text editor
  • Delete the all text between <Queries> and </Queries> (I usually delete the whole thing, NDepend doesn't seem to mind)
  • Add <RuleFile> children to the <RuleFiles> element for each file you wish to reference
  • Save the file

This is an example element taken from one of my existing projects

<NDepend AppName="..." Platform="..." FileWrittenByProductVersion="...">
  <RuleFiles>
    <RuleFile Active="True" Path="..\..\..\build\ndepend\v7-stock.ndrules" />
  </RuleFiles>
</NDepend>

Closing thoughts

When I compared the "new" stock rules I created against my previous one from 2017, there were over 600 differences. Some of these are documentation changes or priority, but there were also a bunch of brand new rules. Therefore if you are using external files it behoves you to check once in a while to see if NDepend has introduced stock rule changes that could be of benefit.

Update History

  • 2019-04-16 - First published
  • 2020-11-22 - Updated formatting

About The Author

Gravatar

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported