2010-02-26 10 views
9

Ich benutze T4MVC, und ich kann kein Pre-Build-Ereignis verwenden, um TextTransform.exe auszuführen, da es auf EnvDTE angewiesen ist und mit Visual Studio als Host ausgeführt werden muss.Können Sie ein RunCustomTool mit EnvDTE als Pre-Build-Ereignis erstellen?

Wenn ich Custom Tool einmal ausgeführt habe, funktioniert es gut, weil es sich selbst als schmutzig markiert, wenn es ausgeführt wird(), aber wenn Sie die Lösung öffnen, wird nicht auf Build ausgeführt, also habe ich mich gefragt, ob Sie ausführen könnten t4 über EnvDTE als Pre-Build-Event?

Antwort

16

dachte ich, einen Ausweg, dies zu tun. Es ist nicht optimal, aber es funktioniert tatsächlich. Wenn Sie eine Verbindung zu BuildEvents.OnBuildBegin herstellen.

Drücken Sie ALT + F11, um zur Macro IDE zu gelangen, klicken Sie auf EnvironmenEvents und fügen Sie den Eventhandler in das folgende Code-Snippet ein. Stellen Sie sicher, dass es außerhalb des automatisch generierten Codeabschnitts hinzugefügt wird.

Die EnvironmentEvents sieht nun wie folgt aus:

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

Very nice! Beste Lösung noch :) –

+1

Brilliant. Ich denke über alle möglichen Wege nach, diesen IDE-Erweiterungspunkt zu verwenden. – Hal

+3

Da dies in VS 2012 wegen fehlender Makros nicht funktioniert, habe ich eine Erweiterung gemacht, die das gleiche tut: http://visualstudiogallery.msdn.microsoft.com/8d820b76-9fc4-429f-a95f-e68ed7d3111a. Quelle unter https://github.com/bennor/AutoT4MVC –

1

Dies ist definitiv einer der Bereiche von T4MVC, die ich gerne lösen würde, aber ich konnte keine großartige Lösung finden. Ich habe einige Versuch zu der Zeit gemacht, ein Pre-Build-Ereignis zu verwenden, aber kam nicht überall interessant. Was nicht bedeutet, dass es nicht getan werden kann.

Entschuldigung, ich habe keine Lösung für Sie, aber wenn jemand etwas einleitet, würde ich mich freuen, es in T4MVC zu integrieren.

David