2010-02-26 43 views
9

我正在使用T4MVC,因爲它依賴於EnvDTE,所以無法使用預生成事件來運行TextTransform.exe,並且必須以Visual Studio作爲主機運行它。你能用EnvDTE做一個RunCustomTool作爲預生成事件嗎?

如果我運行過一次自定義工具,它會很好地工作,因爲它在執行時標記自己很髒(AlwaysKeepTemplateDirty = true),但是當您打開解決方案時,它不會在構建時運行,所以我想知道是否可以運行t4通過EnvDTE作爲預生成事件?

回答

16

我想出了一個辦法做到這一點。它不是最優的,但它實際上工作。如果你連接到BuildEvents.OnBuildBegin。

您按ALT + F11進入Macro 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

很不錯的!最好的解決方案呢:) – 2010-03-03 00:54:44

+1

輝煌。我正在考慮使用IDE擴展點的各種方法。 – Hal 2010-05-20 16:44:02

+3

由於缺乏宏,這在VS 2012中不起作用,所以我做了一個擴展,它可以做同樣的事情:http://visualstudiogallery.msdn.microsoft.com/8d820b76-9fc4-429f-a95f-e68ed7d3111a。來源https://github.com/bennor/AutoT4MVC – 2012-09-20 11:27:40

1

這絕對是我想要解決的T4MVC領域之一,但一直未能找到一個很好的解決方案。我確實做了一些嘗試使用預生成事件,但沒有得到任何有趣的地方。這並不意味着它無法完成。

對不起,我沒有解決方案,但如果有人想出了一些東西,我很樂意將它集成到T4MVC中。

大衛

相關問題