Tuesday, May 27, 2008

Part IIa: Creating Custom Rules for Microsoft Source Analyzer

Copyright 2008-2009, Paul Jackson, all rights reserved

Before moving on to Part III and doing some real work with the custom rule, I want to describe how to set up Visual Studio so you can debug your rules.

First, you'll have to start an instance of Visual Studio to use for working on the custom rule project. This instance must be started without the DLLs for your custom rule being present in the Source Analyzer install directory. This is because you'll want Visual Studio to copy your newly built DLLs there as part of the build process -- if a copy of the DLLs is present when you start Visual Studio, then they'll be loaded by Source Analyzer and will be in use when you do a build.

Next, set your project's Debug properties to start an external program (a new instance of Visual Studio):

The path to Visual Studio depends on where you installed it, but should be something like: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe

And set the Command Line Arguments to the solution file you plan to use for testing your rules -- in my case: "C:\vs\SourceAnalyzerSample\ClassLibrary3\ClassLibrary3.sln"

Finally set the project's Post Build Event to copy the DLLs and PDBs for your custom rule to the Source Analyzer install directory:

xcopy "$(TargetDir)SourceAnalyzerSample*.*" "C:\Program Files\Microsoft Source Analysis Tool for C#" /y

You could set your project's target directory to build directly into the Source Analyzer install directory, but I prefer to have it as a separate step.

Now when you press F5 to start debugging your custom rule project, a new instance of Visual Studio should start with your testing project loaded and your newly built rules in place.


SAUMYA sINGH said...

i am getting an error (Error 1 The command "xcopy "D:\my pd\stylecop\code examples\CustomRuleDemo\CustomRuleDemo\CustomRuleDemo\bin\Debug\SourceAnalyzerSample*.*" "C:\Program Files\Microsoft Source Analysis Tool for C#" /y" exited with code 2. CustomRuleDemo

this command is showing error
could u please tell me what should i give i this as there os not such folder in my system"C:\Program Files\Microsoft Source Analysis Tool for C#"
i only have a stylecop folder in program files.
My project is saved in the following path-"D:\my pd\stylecop\code examples\CustomRuleDemo\CustomRuleDemo\CustomRuleDemo"

under external program i have given the path where devnev is stored in my system- "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"

and in command line arguments i have given the follwoing path where my project is stored- "D:\my pd\stylecop\code examples\CustomRuleDemo\CustomRuleDemo\CustomRuleDemo.sln"

please help

I also tried to leave all these settings blank and instead of start external program, i used the action start program and under debug also, i left everything blank.then i built my application.It did not give any errors.
Then I copied the dll of my application and pasted it direclty in my stylecop folder.
However the rule was not showing when i started my stylecop.

However the rule for the sourceanaylyser sample which you had explained in part I,when i did a few minor code changes, was being displayed under custom rules in stylecop settings
Please help

Dave Wasden said...

I didn't see an answer to this. I had a code 2 error while dooing an xcopy, and was missing the /I switch. This tells xcopy to assume the target path is a folder and not a file.

Dave Wasden said...

doing :)

Irene said...

Thanks a million, works beautifully :)