Friday, June 23, 2006 2:41 PM
Mickey Williams
Custom Checkin Policies for Team Foundation Server
One of my talks tomorrow is on TFS source code control, and as part of the discussion, I'll be demonstrating custom checkin policies. A custom checkin policy runs when you attempt to modify the source archive, and is given veto power over your checkin. Well, it's really not a veto, but that's a long story... Anyhow, the Evaluate method is the core of a checkiin policy, and this one ensures that the source file has only one top-level type:
public override PolicyFailure[] Evaluate()
{
List<PolicyFailure> changes = new List<PolicyFailure>();
PendingChange[] pendingChanges = PendingCheckin.PendingChanges.CheckedPendingChanges;
foreach (PendingChange change in pendingChanges)
{
if (Path.GetExtension(change.FileName) == ".cs")
{
if ((change.ChangeType == ChangeType.Edit) ||
(change.ChangeType == ChangeType.Add))
{
if (!FileHasOneType(change.LocalItem))
{
string msg = string.Format(Constants.FailureMessage, change.LocalItem);
PolicyFailure failure = new PolicyFailure(msg, this);
changes.Add(failure);
}
}
}
}
return changes.ToArray();
}
The basic idea of Evaluate is that it returns an array of PolicyFailure instances. If the array is empty, the policy has succeeded. If the array has elements, they are reported back to the user.