Domanda

C'è un modo per 'Esegui strumento personalizzato' per un'intera soluzione?

Perché? Lo strumento personalizzato è in fase di sviluppo e quando vengono apportate modifiche che ho bisogno di aggiornare tutti gli elementi che lo utilizzano per assicurarsi che tutto funzioni a dovere.

È stato utile?

Soluzione

Da quando ho bisogno di una risposta per questo e ha dovuto fare io stesso, ecco la soluzione per "Esegui strumento personalizzato".

Se avete solo bisogno di eseguire tutta la tua T4 i modelli di nuovo, quindi dal momento che VS2012 non v'è trasformare tutti T4 nella Crea menu.

Per VS2017 hanno rimosso le macro, in modo da seguire https://msdn.microsoft .com / it-it / library / cc138589.aspx e fare un plugin con il vostro voce di menu, invece. Per esempio. Nome tuoi RefreshAllResxFiles di comando, e incolla questo file in (Il set di comandi di default non include le DLL per VSLangProj, quindi basta trovare il pacchetto appropriata in NuGet):

internal sealed class RefreshAllResxFiles
{
  public const int CommandId = 0x0100;
  public static readonly Guid CommandSet = new Guid(copy the guid from guidRefreshAllResxFilesPackageCmdSet from the vsct file);
  private readonly Package _package;
  private readonly DTE2 _dte;

  /// <summary>
  /// Initializes a new instance of the <see cref="RefreshAllResxFiles"/> class.
  /// Adds our command handlers for menu (commands must exist in the command table file)
  /// </summary>
  /// <param name="package">Owner package, not null.</param>
  private RefreshAllResxFiles(Package package)
  {
     _package = package ?? throw new ArgumentNullException(nameof(package));

     var commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
     if (commandService != null)
     {
        var menuCommandId = new CommandID(CommandSet, CommandId);
        var menuItem = new MenuCommand(MenuItemCallback, menuCommandId);
        commandService.AddCommand(menuItem);
     }
     _dte = ServiceProvider.GetService(typeof(DTE)) as DTE2;
  }

  public static RefreshAllResxFiles Instance { get; private set; }
  private IServiceProvider ServiceProvider => _package;

  public static void Initialize(Package package)
  {
     Instance = new RefreshAllResxFiles(package);
  }

  /// <summary>
  /// This function is the callback used to execute the command when the menu item is clicked.
  /// See the constructor to see how the menu item is associated with this function using
  /// OleMenuCommandService service and MenuCommand class.
  /// </summary>
  private void MenuItemCallback(object sender, EventArgs e)
  {
     foreach (Project project in _dte.Solution.Projects)
        IterateProjectFiles(project.ProjectItems);
  }

  private void IterateProjectFiles(ProjectItems projectProjectItems)
  {
     foreach (ProjectItem file in projectProjectItems)
     {
        var o = file.Object as VSProjectItem;
        if (o != null)
           ProcessFile(o);
        if (file.SubProject?.ProjectItems != null)
           IterateProjectFiles(file.SubProject.ProjectItems);
        if (file.ProjectItems != null)
           IterateProjectFiles(file.ProjectItems);
     }

  }

  private void ProcessFile(VSProjectItem file)
  {
     if (file.ProjectItem.Name.EndsWith(".resx"))
     {
        file.RunCustomTool();
        Log(file.ProjectItem.Name);
     }
  }
  public const string VsWindowKindOutput = "{34E76E81-EE4A-11D0-AE2E-00A0C90FFFC3}";

  private void Log(string fileName)
  {
     var output = _dte.Windows.Item(VsWindowKindOutput);
     var pane = ((OutputWindow)output.Object).OutputWindowPanes.Item("Debug");
     pane.Activate();
     pane.OutputString(fileName);
     pane.OutputString(Environment.NewLine);
  }
}

E la vecchia soluzione per la macro:

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

Public Module RecordingModule
    Sub IterateFiles()
        Dim solution As Solution = DTE.Solution
        For Each prj As Project In solution.Projects
            IterateProjectFiles(prj.ProjectItems)
        Next
    End Sub

    Private Sub IterateProjectFiles(ByVal prjItms As ProjectItems)
        For Each file As ProjectItem In prjItms
            If file.Object IsNot Nothing AndAlso TypeOf file.Object Is VSProjectItem Then
                AddHeaderToItem(file.Object)
            End If
            If file.SubProject IsNot Nothing AndAlso file.SubProject.ProjectItems IsNot Nothing AndAlso file.SubProject.ProjectItems.Count > 0 Then
                IterateProjectFiles(file.SubProject.ProjectItems)
            End If
            If file.ProjectItems IsNot Nothing AndAlso file.ProjectItems.Count > 0 Then
                IterateProjectFiles(file.ProjectItems)
            End If
        Next
    End Sub

    Private Sub AddHeaderToItem(ByVal file As VSProjectItem)
        If file.ProjectItem.Name.EndsWith(".resx") Then
            file.RunCustomTool()
            Log(file.ProjectItem.Name)
        End If
    End Sub
    Private Sub Write(ByVal name As String, ByVal message As String)
        Dim output As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        Dim window As OutputWindow = output.Object
        Dim pane As OutputWindowPane = window.OutputWindowPanes.Item(name)
        pane.Activate()
        pane.OutputString(message)
        pane.OutputString(Environment.NewLine)
    End Sub
    Private Sub Log(ByVal message As String, ByVal ParamArray args() As Object)
        Write("Debug", String.Format(message, args))
    End Sub

    Private Sub Log(ByVal message As String)
        Write("Debug", message)
    End Sub

End Module

Altri suggerimenti

In Visual Studio 2010 c'è un pulsante nella barra delle icone del navigatore soluzione che verrà eseguito tutti i modelli T4 in una soluzione.

In Visual Studio 2012 mostrano la barra degli strumenti "Build". C'è un pulsante in quella barra che eseguirà tutti i modelli T4 in una soluzione.

È possibile eseguire tutti i modelli T4 in una soluzione in Visual Studio 2010. destro del mouse sullo spazio barra degli strumenti superiore e attivare la barra degli strumenti "Build". Verrà aggiunta una barra degli strumenti con il seguente:

  • Crea Selezione
  • Genera soluzione
  • trasformare tutti T4 template
  • Annulla

"trasformare tutti modelli T4" dovrebbe darvi quello che volete.

Per chi ha iniziato utilizzando la soluzione fornita nelle altre risposte, ma sta trovando che in esecuzione tutti i modelli nella soluzione sta impiegando troppo tempo - e se un sottoinsieme dei modelli sarebbe sufficiente - allora è possibile eseguire più modelli utilizzando la procedura seguente.

  1. Selezionare i modelli che si desidera eseguire in Esplora soluzioni in Visual Studio. Si noti che è i file effettivi che si dovrebbe scegliere - non la cartella che li contiene
  2. .
  3. Fare clic destro su uno dei file di modello selezionati e selezionare Run Custom Tool dal menu contestuale.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top