当我使用StyleCop时,我总是做run StyleCop
菜单或构建项目
我想在程序保存后运行StyleCop。
有可能吗?
答案 0 :(得分:1)
不幸的是,macros已被删除但写入加载项非常简单。首先创建一个新的C#Add-In项目(完成后,您需要将DLL部署到Visual Studio AddIns文件夹并重新启动VS)。
修改生成的模板以附加到DocumentSaved
事件:
private DocumentEvents _documentEvents;
public void OnConnection(object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
_documentEvents = _applicationObject.Events.DocumentEvents;
_documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
}
public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom)
{
_documentEvents.DocumentSaved -= DocumentEvents_DocumentSaved;
}
您的DocumentEvents_DocumentSaved
方法只需要调用正确的VS命令(请注意命令名称因您使用的Visual Studio版本而异)。
private void DocumentEvents_DocumentSaved(Document Document)
{
document.DTE.ExecuteCommand("Build.RunCodeAnalysisonSelection", "");
}
在这种情况下,您将仅对当前项目运行代码分析(假设它是您保存的内容,那么它也是您要测试的内容)。全部保存的此假设失败,因此您可能需要使用"Build.RunCodeAnalysisonSolution"
。当然,还有很多需要改进的空间(例如,当多个接近顺序保存时)。
如果你的目标是VS 2013,那么你不应该使用AddIns,因为它们已被弃用而不赞成使用。您有同样的事情要做,但是您通过IVsRunningDocTableEvents
收到了通知。覆盖Initialize()
中的Package
(将实现IVsRunningDocTableEvents
界面)。从AdviseRunningDocTableEvents()
致电IVsRunningDocumentTable
(使用GetService()
获取),您就完成了。
最后请注意,同样的技术也适用于任何其他事件(成功构建之后,部署之前,关闭解决方案时等)。
答案 1 :(得分:1)
我参考@Adriano Repetti的答案提出了VSAutoBuild
。
它已发布在以下网址中: https://visualstudiogallery.msdn.microsoft.com/f0930864-0637-4fb3-a34a-155375aa85b3
和github网址: https://github.com/ko2ic/VSAutoBuild