WEB开发网
开发学院软件开发VC VSTS 2008 - 自定义签入代码审查策略 阅读

VSTS 2008 - 自定义签入代码审查策略

 2008-06-03 21:38:49 来源:WEB开发网   
核心提示: 再右击项目, 选择"添加引用"选择"浏览"选项卡, 找到下面路径的文件:C:Program FilesMicrosoft Visual Studio 9.0Common7IDE PrivateAssembliesMicrosoft.TeamFoun

VSTS 2008 - 自定义签入代码审查策略

再右击项目, 选择"添加引用"选择"浏览"选项卡, 找到下面路径的文件:

C:Program FilesMicrosoft Visual Studio 9.0Common7IDE PrivateAssembliesMicrosoft.TeamFoundation.VersionControl.Client.dll

并添加:

VSTS 2008 - 自定义签入代码审查策略

在类中输入下面代码:

using System.Windows.Forms;
  
using Microsoft.TeamFoundation.VersionControl.Client;
  
using System.Collections;
  
using System.IO;
  
using System;
  
using System.Collections.Generic;
  
using System.Text;
  
namespace CopyrightCheckinPolicy
  
{
  
[Serializable]
  
public class CopyrightPolicy : PolicyBase
  
{
  
public override string Description
  
{
  
get { return Strings.policyDescription; }
  
}
  
public override bool Edit(IPolicyEditArgs policyEditArgs)
  
{
  
return false;
  
}
  
public override PolicyFailure[] Evaluate()
  
{
  
if (this.Disposed)
  
{
  
throw new ObjectDisposedException(Strings.policyType, Strings.policyDisposedMessage);
  
}
  
ArrayList changes = new ArrayList();
  
PendingChange[] checkedFiles = this.PendingCheckin.PendingChanges.CheckedPendingChanges;
  
foreach (PendingChange change in checkedFiles)
  
{
  
if (Path.GetExtension(change.LocalItem) == ".cs")
  
{
  
if (change.ChangeType.ToString().Contains(ChangeType.Edit.ToString()) || change.ChangeType.ToString().Contains(ChangeType.Add.ToString()))
  
{
  
if (IsFileMissingCopyrightNotice(change.LocalItem))
  
{
  
PolicyFailure failure = new PolicyFailure(Strings.activateMessage + " : " + change.LocalItem, this);
  
changes.Add(failure);
  
}
  
}
  
}
  
}
  
return (PolicyFailure[])changes.ToArray(typeof(PolicyFailure));
  
}
  
public override string Type
  
{
  
get { return Strings.policyType; }
  
}
  
public override string TypeDescription
  
{
  
get { return Strings.policyTypeDescription; }
  
}
  
public override bool CanEdit
  
{
  
get
  
{
  
return false;
  
}
  
}
  
public override void DisplayHelp(PolicyFailure failure)
  
{
  
MessageBox.Show(Strings.policyHelp);
  
}
  
public override string InstallationInstructions
  
{
  
get
  
{
  
return Strings.policyInstallationInstructions;
  
}
  
set
  
{
  
Strings.policyInstallationInstructions = value;
  
}
  
}
  
public override void Activate(PolicyFailure failure)
  
{
  
MessageBox.Show(Strings.activateMessage);
  
}
  
protected override void OnPolicyStateChanged(PolicyFailure[] failures)
  
{
  
base.OnPolicyStateChanged(failures);
  
}
  
public override void Initialize(IPendingCheckin pendingCheckin)
  
{
  
base.Initialize(pendingCheckin);
  
pendingCheckin.PendingChanges.CheckedPendingChangesChanged += new EventHandler(pendingCheckin_CheckedPendingChangesChanged);
  
}
  
public override void Dispose()
  
{
  
PendingCheckin.PendingChanges.CheckedPendingChangesChanged -= new EventHandler(pendingCheckin_CheckedPendingChangesChanged);
  
base.Dispose();
  
}
  
private void pendingCheckin_CheckedPendingChangesChanged(Object sender, EventArgs e)
  
{
  
OnPolicyStateChanged(Evaluate());
  
}
  
private bool IsFileMissingCopyrightNotice(string fullFileName)
  
{
  
try
  
{
  
StreamReader sr = new StreamReader(fullFileName, true);
  
string sourceCode = sr.ReadToEnd();
  
bool result = !sourceCode.Contains("Copyright");
  
sr.Close();
  
return result;
  
}
  
catch (Exception)
  
{
  
return false;
  
}
  
}
  
}
  
internal class Strings
  
{
  
public static string policyType = "Copyright Notice Policy";
  
public static string policyDescription = "This policy ensures that all C# Source Files include a Copyright Notice.";
  
public static string policyInstallationInstructions = "Sorry, No instructions are available at this time";
  
public static string policyTypeDescription = policyType + " Description";
  
public static string policyDisposedMessage = policyType + " Object has been disposed.";
  
public static string policyHelp = "Sorry, help for " + policyType + " is not available at this time.";
  
public static string activateMessage = "Copyright Notice not found in this source file.";
  
}

上一页  1 2 3 4  下一页

Tags:VSTS 定义 代码

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接