using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace TFSPMSIntegration
{
[Serializable]
public class CheckForPMSDetails : PolicyBase
{
public bool pmsDetailsConfirmed=false;
private static frmPmsDetails _frm = null;
public override string Description
{
get { return "Remind users to add PMS details to their checkins"; }
}
// This is a string that is stored with the policy definition on the source
// control server. If a user does not have our policy plugin installed, this string
// will be displayed. We can use this as an opportunity to explain to the user
// how they might go about installing our policy plugin.
public override string InstallationInstructions
{
get { return "To install this policy, follow the instructions in CheckForPMSDetails.cs."; }
}
// This string is the type of our policy. It will be displayed to the user in a list
// of all installed policy types when they are creating a new policy.
public override string Type
{
get { return "Check for PMS Details"; }
}
// This string is a description of the type of our policy. It will be displayed to the
// user when they select our policy type in the list of policies installed on the system
// as mentioned above.
public override string TypeDescription
{
get { return "This policy will prompt the user to decide whether or not they should be allowed to check in."; }
}
// This method is invoked by the policy framework when the user creates a new checkin
// policy or edits an existing checkin policy. We can use this as an opportunity to
// display UI specific to this policy type allowing the user to change the parameters
// of the policy.
public override bool Edit(IPolicyEditArgs args)
{
// no configuration to save
return true;
}
// This method performs the actual evaluation. It is called by the policy framework at various points in time
// when policy should be evaluated. In this example, we invoke this method ourselves when various asyc
// events occur that may have invalidated the current list of failures.
public override PolicyFailure[] Evaluate()
{
if (!pmsDetailsConfirmed)
{
return new PolicyFailure[] {
new PolicyFailure("Please provide PMS Details about your checkin", this),
};
}
else
{
//frmPmsDetails frm = Application.OpenForms["frmPmsDetails"] as frmPmsDetails;
if(_frm!=null)
PendingCheckin.PendingChanges.Comment = _frm.txtDescription.Text;
return new PolicyFailure[0];
}
}
// This method is called if the user double-clicks on a policy failure in the UI.
// We can handle this as we please, potentially prompting the user to perform
// some activity that would eliminate the policy failure.
public override void Activate(PolicyFailure failure)
{
//The Singleton design pattern is used for maitntain only one instance of Form class(UI).But everytime we have passing new value to the custom policy class.
//Singleton approach needed , otherwise each time we click on policy message error, it will open multiple forms(UI)
if (_frm == null)
_frm = new frmPmsDetails(this);
else
_frm.CheckForPMSDetails = this;
_frm.WindowState = FormWindowState.Minimized;
_frm.TopMost = true;
_frm.Show();
_frm.ClearAll();
_frm.WindowState = FormWindowState.Normal;
_frm.BringToFront();
}
public void fn_Evaluate()
{
pmsDetailsConfirmed = true;
base.OnPolicyStateChanged(Evaluate());
}
// This method is called if the user presses F1 when a policy failure is active in the UI.
// We can handle this as we please, displaying help in whatever format is appropriate.
// For this example, we'll just pop up a dialog.
public override void DisplayHelp(PolicyFailure failure)
{
MessageBox.Show("This policy helps you to remember to add PMS details to your checkins.", "Prompt Policy Help");
}
}
}
自定义签到策略部署
在 Visual Studio 2012 版本中使用 VSIX 包创建签入策略的步骤
1.在visual studio 2012版本中新建项目。
2.选择可扩展性项目模板并选择 VSIX 项目模板。
- 在 VSIX 项目中添加对自定义签入策略项目的引用
-
您应该在项目中添加一些项目,例如图标位图和 License.txt,但只有其中一项是强制性的:“policies.pkgdef”
- 您只需添加一个文本文件,并将其命名为policies.pkgdef。名称可以是任何名称,但扩展名应为“pkgdef”。
- 编辑 policies.pkgdef 使其如下所示:
[$RootKey$\TeamFoundation\SourceControl\Checkin 策略]
"TFSPMSIntegration"="$PackageFolder$\TFSPMSIntegration.dll"
第 1 部分:这是您为策略包选择的名称,也是注册表中键的名称
第 2 部分:这是在项目属性、应用程序/程序集名称字段中定义的程序集名称。
- 最后,您必须设置 vsixmanifest 文件,该文件名为 source.extension.vsixmanifest。
选择“资产”选项卡并使用“添加新资产”窗口添加“policies.pkgdef”文件
您必须构建解决方案,并通过双击安装 VSIX。请注意,它首先在您重新启动 Visual Studio 时生效。
*为了禁用和卸载软件包,请转到 Visual Studio 工具菜单并选择“扩展和更新”
选择扩展并执行适当的操作