你能用EnvDTE做一个RunCustomTool作为预构建事件吗?

Luh*_*ann 9 .net macros t4 envdte t4mvc

我正在使用T4MVC,我不能使用预构建事件来运行TextTransform.exe,因为它依赖于EnvDTE,并且必须以Visual Studio作为主机运行.

如果我已经运行了一次自定义工具,它运行得很好,因为它在执行(AlwaysKeepTemplateDirty = true)时会标记为脏,但是当你打开解决方案时,它不会在构建时运行,所以我想知道你是否可以通过EnvDTE运行t4作为预建活动?

Luh*_*ann 16

我想出了一种方法来做到这一点.它不是最佳的,但它确实有效.如果你连接到BuildEvents.OnBuildBegin.

你按ALT + F11进入Macro IDE,点击EnvironmenEvents并在下面的代码片段中添加eventhandler.确保将其添加到自动生成的代码部分之外.

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
Run Code Online (Sandbox Code Playgroud)

  • 由于缺少宏,这在VS 2012中不起作用,我做了一个扩展,做了同样的事情:http://visualstudiogallery.msdn.microsoft.com/8d820b76-9fc4-429f-a95f-e68ed7d3111a.来源:https://github.com/bennor/AutoT4MVC (3认同)