对TFS上的签入策略强制执行自定义stylecop规则

对TFS上的签入策略强制执行自定义stylecop规则

问题描述:

我已经安装了StyleCop签入策略(来自此处),效果很好.但是,我编写了一些自定义规则,希望将它们作为CheckIn策略的一部分加以实施(Visual Studio 2008,Team Explorer 2008,TFS 2005).

I have installed StyleCop CheckIn policy (from here) and it works great. However I have written some custom rules which I want to be enforced as part of the CheckIn policy (Visual Studio 2008, Team Explorer 2008, TFS 2005).

当我右键单击项目并单击"StyleCop设置"时,自定义规则在设置文件中会很好显示.但是,当我添加CheckIn策略并尝试导入相同的设置时,StyleCop文件的自定义规则"不会出现在编辑器窗口中.

The custom rules appear fine in the settings file when I right click on a project and click "StyleCop Settings". However when I add my CheckIn policy, and try to import the same settings.StyleCop file my Custom Rules don't appear in the editor window.

另一个可能与之相关的问题是,当我从Visual Studio打开编辑器时,我的自定义规则会出现在StyleCop设置编辑器中.但是,如果我通过双击Windows资源管理器中的设置文件来打开编辑器,那么我的规则将无处可见.

Another problem, probably related, is that my custom rules appear in the StyleCop settings editor when I open the editor from Visual Studio. However if I open the editor by double-clicking a settings file from Windows explorer my rules are nowhere to be seen.

现在任何人都可以告诉我是否可以将自定义规则作为检入策略的一部分,如果可以的话,如何在添加检入策略时将其显示在StyleCop设置编辑器窗口中.

Now can anyone tell me if it is in fact possible to have custom rules as part of a checkIn policy, and if so how to go about getting them to appear in the StyleCop settings editor window when adding a checkIn policy.

谢谢

尼尔

是的,肯定有可能!

要使它们正常工作可能会有些棘手,但是了解一些关键的东西应该会很有帮助.

It can be a little tricky to get it all working correctly, but knowing a couple key things should help a lot.

第一步是使用自定义规则使StyleCop在开发人员计算机上正常工作:
1)StyleCop必须安装在开发人员机器上
2)自定义规则必须与StyleCop安装在同一目录中(或可选地安装在子目录中).如果将它们安装在其他任何地方,则根本无法正常工作.
3)必须合并该安装目录中的Settings.StyleCop文件,包含要分析的项目的目录以及该目录之上的所有目录,才能启用自定义规则.通过双击项目目录中的Settings.StyleCop文件并确保在编辑器中启用了规则,可以最好地验证这一规则.

The first step is to have StyleCop working correctly on the developer machine with custom rules:
1) StyleCop has to be installed on the developer machine
2) The custom rules have to be installed in the same directory as StyleCop (or, optionally, into a child directory). If they're installed anywhere else then they're unlikely to work correctly, if they work at all.
3) The Settings.StyleCop files in that installation directory, the directory containing the project being analyzed, and all directories above that one, have to combine to enable your custom rules. This one is verified best by double-clicking the Settings.StyleCop file in the project directory and making sure your rules are enabled in the editor.

下一个要点更加棘手,让StyleCop在构建机器上正常工作.
这三点几乎是相同的,但是在将运行StyleCop作为签入策略的构建计算机上.因此,构建计算机必须已安装StyleCop,安装了自定义规则以及所有正确的Settings.StyleCop文件,才能在构建计算机上创建与开发人员计算机上相同的整体StyleCop规则设置.只需不意识到构建机器上必须安装了StyleCop,就可以最轻松地解决这个问题.即使安装了StyleCop,StyleCop仍可以通过VS集成(您对此毫无疑问)或

The next point is even trickier, having StyleCop working correctly on the build machine.
It's pretty much the same three points again, but on the build machine which will be running StyleCop for the check-in policy. So the build machine has to have StyleCop installed, the custom rules installed, and all the right Settings.StyleCop files to create the same overall StyleCop rule settings on the build machine as there are on the developer machine. This is easiest to break by simply not realizing that the build machine has to have StyleCop installed on it. Even if StyleCop is installed, StyleCop can run via VS integration (as you're not doubt familiar with) or StyleCop via MSBuild integration (which is less common and typically less well understood). The build machine is often set up to use MSBuild integration, which means that the custom rules need to be installed in the %Program Files%\MSBuild\Microsoft\StyleCop\v4.3 directory instead of the %Program Files%\StyleCop directory you might be expecting. Additionally, if you've modified the base Settings.StyleCop file in the %Program Files%\StyleCop directory, then you'll have to copy that file into the %Program Files%\MSBuild\Microsoft\StyleCop\v4.3 directory in order to get the same effect on the build machine. Note that doing a copy/paste with a verified working Settings.StyleCop file from a developer machine is the best way to make sure that the build machine has the correct settings. StyleCop is extremely picky about those XML files, and the slightest typo can lead to bizarre behaviour that will take days to debug. Do not hand edit. You have been warned. 8 )

因此,肯定有很多方法不能完全正确地做到这一点,您将看到奇怪的地方,就像您所描述的那样,直到您将其理解为正确为止.幸运的是,牢记我列出的三点以及构建机器上的StyleCop"和开发人员机器上的StyleCop"之间的区别,应该有可能最终使它们顺利运行.好吧,至少对我来说一直如此.

So there are definitely lots of ways to not get it exactly right, and you're going to see strangeness just like you've described until you get it there. Fortunately, by keeping in mind the three points I've listed and the difference between "StyleCop on the build machine" and "StyleCop on a developer machine" it should be possible to get it all working smoothly eventually. Well, it's consistently worked for me at least.

祝你好运!