65.9K
CodeProject 正在变化。 阅读更多。
Home

Visual Studio 2008 的同行代码审查加载项 - ReviewMyCode

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (3投票s)

2011年10月7日

CPOL

3分钟阅读

viewsIcon

31460

downloadIcon

537

同行代码审查是提高软件质量行之有效的方法。ReviewMyCode 通过一个简单的 Visual Studio 2008 插件来帮助您克服此最佳实践中的一些障碍,该插件适合您的流程。

引言

同行代码审查是提高软件质量行之有效的方法。ReviewMyCode 通过一个简单的 Visual Studio 2008 插件来帮助您克服此最佳实践中的一些障碍,该插件适合您的流程。团队成员无需离开开发环境即可参与审查流程。

该插件通过将审查流程与 SVN 源代码管理系统集成,在团队开发环境中运行良好。

背景

Visual Studio 扩展入门 是 Visual Studio 插件开发的一个很好的起点。

Using the Code

一个插件至少必须有两个文件:插件描述符 XML 文件和实现实际插件的程序集。描述符文件描述插件程序集,位于用户的 %USERPROFILE%\Documents\Documents\Visual Studio 2008\Addins 文件夹中。

一个典型的插件描述符 XML 文件如下所示

<Extensibility xmlns="http://schemas.microsoft.com/AutomationExtensibility">
	<HostApplication>
		<Name>Microsoft Visual Studio</Name>
		<Version>9.0</Version>
	</HostApplication>
	<Addin>
		<FriendlyName>ReviewMyCode</FriendlyName>
		<Description>Review My Code</Description>
		<Assembly>ReviewMyCode.dll</Assembly>
		<FullClassName>ReviewMyCode.View.Connect</FullClassName>
		<LoadBehavior>1</LoadBehavior>
		<CommandPreload>0</CommandPreload>
		<CommandLineSafe>0</CommandLineSafe>
	</Addin>
</Extensibility>

Extensibility\Addin\Assembly 给出了插件程序集的路径。仅使用文件名,Visual Studio 将从插件描述符文件夹加载插件程序集。如果您需要调试插件,则需要将其指向插件的调试版本。您还需要设置 Visual Studio 项目的调试属性,以将 Visual Studio 作为外部程序启动。请确保指定:/resetaddin ReviewMyCode.View.Connect 作为命令行参数。

插件的入口点是Connect 类。它实现了Extensibility.IDTExtensibility2 接口。OnConnection 方法接收插件正在加载的通知,我们在这里执行初始化代码,例如创建解决方案事件(解决方案打开、解决方案关闭前)委托。为了处理解决方案级别的事件,必须在Connect 类中定义委托。

OnStartupComplete 方法接收主机应用程序已完成加载的通知,在此我们创建工具窗口(插件窗口)并显示它。插件窗口是一个UserControl

其余类只是实现逻辑和 UI。检索有关正在审查的代码的信息取决于所选行。以下代码片段显示了详细信息

private void ToolStripReviewItemAddNew_Click(object sender, EventArgs e)
{
    TextPoint textPoint = null;
    TextSelection selection = 
        Connect.ApplicationObject.ActiveDocument.Selection as TextSelection;
    if (selection != null)
    {
        textPoint = selection.ActivePoint as TextPoint;
        if (textPoint != null)
        {
            this.textBoxLineNumber.Text = 
            	textPoint.Line.ToString(CultureInfo.CurrentCulture);
        }
    }
    
    CodeElement codeElement = null;
    FileCodeModel2 fileCodeModel =
        Connect.ApplicationObject.ActiveDocument.ProjectItem.FileCodeModel 
				as VCFileCodeModel;
    if (fileCodeModel == null)
    {
        fileCodeModel = Connect.ApplicationObject.ActiveDocument.
        		ProjectItem.FileCodeModel as FileCodeModel2;
    }
    
    if (textPoint != null && fileCodeModel != null)
    {
        // Supported scopes - set up here in the right parsing order
        // from lowest level to highest level.
        // NOTE: Must be adjusted to match any CodeElements supported.
        vsCMElement[] scopes = 
            {
               vsCMElement.vsCMElementFunction,
               vsCMElement.vsCMElementProperty,
               vsCMElement.vsCMElementVariable,
               vsCMElement.vsCMElementEvent,
               vsCMElement.vsCMElementClass,
               vsCMElement.vsCMElementInterface,
               vsCMElement.vsCMElementStruct,
               vsCMElement.vsCMElementEnum,
               vsCMElement.vsCMElementDefineStmt,
               vsCMElement.vsCMElementImportStmt,
               vsCMElement.vsCMElementIncludeStmt,
               vsCMElement.vsCMElementModule,
               vsCMElement.vsCMElementOther
           };
           
        foreach (vsCMElement scope in scopes)
        {
            // Catch and ignore all exceptions from CodeModel.
            // CodeElementFromPoint will raise a COM based exception:
            // System.Runtime.InteropServices.COMException 
            // (0x80004005): Unspecified error.
            try
            {
                codeElement = fileCodeModel.CodeElementFromPoint(textPoint, scope);
                if (codeElement != null)
                {
                    break;
                }
            }
            catch
            {
            }
        }
    }
    
    if (codeElement != null)
    {
        this.textBoxMethodName.Text = codeElement.FullName;
        this.textBoxProjectName.Text =
            codeElement.ProjectItem.ContainingProject.UniqueName;
        this.textBoxFileName.Text = codeElement.ProjectItem.Name;
    }
    else
    {
        this.textBoxMethodName.Enabled = true;
        this.textBoxProjectName.Enabled = true;
        this.textBoxFileName.Enabled = true;
    }
    
    this.textBoxCreatedBy.Text = ReviewWindow.TextToTitleCase(Environment.UserName);
    this.toolStripReviewItemSave.Enabled = true;
}

关注点

使用加载项

  • 必须从 Visual Studio IDE 加载插件。为此,请从“工具”菜单中选择“加载项管理器”。
  • 要启动审查,请单击“发送到审查”按钮。这将打开一个新的邮件消息窗口。填写“收件人”字段并将邮件发送给团队成员。
  • 审阅者可以通过选择审阅项目选项卡来添加审阅项目。要修改项目,请单击审阅列表中的项目,然后选择审阅项目选项卡。
  • 审阅项目可以包含注释,例如拒绝注释或有关修复的注释。
  • 从 Visual Studio 加载解决方案会自动从 SVN 加载当前的审阅日志。关闭解决方案时,更新的审阅日志将保存到 SVN 中。

屏幕截图

审阅列表

The review list

添加审阅注释

Adding a review comment

为审阅注释添加注释

Adding a comment for a review commen

历史

  • 2011年10月5日 - 版本 1.0.0.0 - 初始版本
  • 2011年10月7日 - 添加屏幕截图
© . All rights reserved.