你能在预构建事件中使用EnvDTE执行RunCustomTool吗?

9

我正在使用T4MVC,但无法使用预构建事件运行TextTransform.exe,因为它依赖于EnvDTE,并且必须在Visual Studio中作为主机运行。

如果我已经运行了自定义工具,它会很好地工作,因为当其执行时会标记自身为脏文件(AlwaysKeepTemplateDirty = true),但是当您打开解决方案时,它不会在构建时运行,因此我想知道是否可以通过EnvDTE在预构建事件中运行t4?

2个回答

16

我找到了一种方法来实现这个。它不是最优的,但它确实可以工作。如果你挂钩到BuildEvents.OnBuildBegin。

你按ALT+F11键进入宏IDE,点击EnvironmenEvents,并在下面的代码段中添加事件处理程序。确保它添加在自动生成的代码部分之外。

EnvironmentEvents现在看起来像这样:

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module EnvironmentEvents

    Public Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
        If Scope = vsBuildScope.vsBuildScopeSolution Or Scope = vsBuildScope.vsBuildScopeProject Then
            Dim projectItem As ProjectItem = DTE.Solution.FindProjectItem("T4MVC.tt")
            If Not projectItem Is Nothing Then
                If Not projectItem.IsOpen Then
                    projectItem.Open()
                End If
                projectItem.Save()
            End If
        End If
    End Sub

#Region "Automatically generated code, do not modify"
'Automatically generated code, do not modify
'Event Sources Begin
 <System.ContextStaticAttribute()> Public WithEvents DTEEvents As EnvDTE.DTEEvents
 <System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
 <System.ContextStaticAttribute()> Public WithEvents WindowEvents As EnvDTE.WindowEvents
 <System.ContextStaticAttribute()> Public WithEvents TaskListEvents As EnvDTE.TaskListEvents
 <System.ContextStaticAttribute()> Public WithEvents FindEvents As EnvDTE.FindEvents
 <System.ContextStaticAttribute()> Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents
 <System.ContextStaticAttribute()> Public WithEvents SelectionEvents As EnvDTE.SelectionEvents
 <System.ContextStaticAttribute()> Public WithEvents BuildEvents As EnvDTE.BuildEvents
 <System.ContextStaticAttribute()> Public WithEvents SolutionEvents As EnvDTE.SolutionEvents
 <System.ContextStaticAttribute()> Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents
 <System.ContextStaticAttribute()> Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents
 <System.ContextStaticAttribute()> Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents
 <System.ContextStaticAttribute()> Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents
 <System.ContextStaticAttribute()> Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents
 <System.ContextStaticAttribute()> Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents
 <System.ContextStaticAttribute()> Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents
 <System.ContextStaticAttribute()> Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents
'Event Sources End
'End of automatically generated code
#End Region

End Module

1
非常好!目前最佳解决方案 :) - David Ebbo
1
太棒了。我正在考虑各种使用那个IDE扩展点的方法。 - Hal
3
由于在VS 2012中缺少宏,因此这个方法无法使用。我制作了一个扩展程序来完成同样的功能:http://visualstudiogallery.msdn.microsoft.com/8d820b76-9fc4-429f-a95f-e68ed7d3111a。源代码在https://github.com/bennor/AutoT4MVC上。 - Bennor McCarthy
对于像我这样找不到EnvironmenEvents的人,打开View\ Class View菜单并在类视图中展开根命名空间。EnvironmentEvents就在其中。 - Steven

1

这绝对是我想解决的T4MVC领域之一,但是我还没有找到一个很好的解决方案。当时我确实尝试过使用预构建事件,但没有得到任何有趣的结果,这并不意味着它不能完成。

抱歉,我没有解决办法,但如果有人想出什么办法,我很乐意将其集成到T4MVC中。

大卫


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接