2010-08-10 12 views
2

Je viens de commencer à utiliser Nant pour mes builds et mes tests. Je veux le faire changer la couleur (ou l'arrière-plan) de mon texte d'invite de commande quand il échoue ainsi il est facilement remarqué.comment exécuter la commande 'color' dans le script NAnt

La commande dans l'invite de commande sous Windows est «couleur 4» pour la remplacer par rouge et la couleur 7 par retour au blanc.

Comment est-ce que je fais exécuter ceci dans un script de construction, l'écho ne fonctionne pas, l'exec ne fonctionne pas (peut-être utiliser un mauvais exec cependant). Je préférerais ne pas avoir à exécuter perl etc juste pour faire quelque chose qui est facilement fait dans une fenêtre d'invite de commande standard.

Est-ce que quelqu'un sait comment faire cela?

Répondre

4

Essayez d'utiliser une tâche personnalisée. Si la tâche est incluse dans le fichier nant, vous n'aurez aucune dépendance externe.

<project > 

    <target name="color"> 
     <consolecolor color="Red" backgroundcolor="White"/> 
     <echo message="red text"/> 
     <consolecolor color="White" backgroundcolor="Black"/> 
     <echo message="white text"/> 
    </target> 

    <script language="C#"> 
     <code> 
      [TaskName("consolecolor")] 
      public class TestTask : Task 
      { 
      private string _color; 
      private string _backgroundColor; 

      [TaskAttribute("color",Required=true)] 
      public string Color 
      { 
      get { return _color; } 
      set { _color = value; } 
      } 

      [TaskAttribute("backgroundcolor",Required=false)] 
      public string BackgroundColor 
      { 
      get { return _backgroundColor; } 
      set { _backgroundColor = value; } 
      } 

      protected override void ExecuteTask() 
      { 
      System.Console.ForegroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color); 
      System.Console.BackgroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor); 
      } 
      } 
     </code> 
    </script> 

</project> 
+0

parfait! merci Martin – RodH257

+0

Après avoir défini la couleur d'arrière-plan, vous pouvez ajouter try {System.Console.Clear();} catch() {}. 1. Cela effacera l'écran et définira l'arrière-plan entier à la même couleur. Vous pouvez voir en un coup d'œil si la construction a échoué. 2. La capture est à gérer lorsque le fichier batch est redirigé. –

2

En tant que suite à mon commentaire sur le poteau par @ Martin-Vobr:

J'ai ajouté une logique supplémentaire pour changer correctement l'arrière-plan. Cela permettra à une construction d'être lancée dans une fenêtre de commande, puis la progression peut être vérifiée en un coup d'œil. J'utilise un fond bleu pour "construire", vert pour "Succès" et rouge pour "Echec".

<!-- http://stackoverflow.com/questions/3446135/how-to-run-color-command-in-nant-script --> 
    <!-- Sample: <consolecolor color="Red" backgroundcolor="White"/> --> 
    <!-- Alternative: http://riccardotramma.com/2011/05/nantcolours-v1-0-a-task-library-for-output-colouring-in-nant/ --> 
    <script language="C#"> 
     <code> 
     <![CDATA[ 
      [TaskName("consolecolor")] 
      public class TestTask : Task 
      { 
       private string _color; 
       private string _backgroundColor; 

       [TaskAttribute("color",Required=true)] 
       public string Color 
       { 
        get { return _color; } 
        set { _color = value; } 
       } 

       [TaskAttribute("backgroundcolor",Required=false)] 
       public string BackgroundColor 
       { 
        get { return _backgroundColor; } 
        set { _backgroundColor = value; } 
       } 

       protected override void ExecuteTask() 
       { 
        System.Console.ForegroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color); 
        System.Console.BackgroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor); 
        // clearing the screen sets the entire screen to be the new color 
        ChangeColor((System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color), (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor)); 
       } 

       // added by Brad Bruce 
       // http://stackoverflow.com/questions/6460932/change-entire-console-background-color-win32-c 

       [System.Runtime.InteropServices.DllImport("kernel32.dll")] 
       static extern bool ReadConsoleOutputAttribute(IntPtr hConsoleOutput, 
        [System.Runtime.InteropServices.Out] ushort[] lpAttribute, uint nLength, COORD dwReadCoord, 
        out uint lpNumberOfAttrsRead); 

       [System.Runtime.InteropServices.DllImport("kernel32.dll")] 
       static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, 
        ushort wAttribute, uint nLength, COORD dwWriteCoord, out uint 
        lpNumberOfAttrsWritten); 

       [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
       public struct COORD { 
        public short X; 
        public short Y; 

        public COORD(short X, short Y) { 
         this.X = X; 
         this.Y = Y; 
        } 
       }; 

       [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] 
       static extern IntPtr GetStdHandle(int nStdHandle); 

       //C#: Get stdout handle 
       const int STD_OUTPUT_HANDLE = -11; 
       const int STD_INPUT_HANDLE = -10; 
       const int STD_ERROR_HANDLE = -12; 
       //INVALID_HANDLE_VALUE //(return value if invalid handle is specified) 

       // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes 

       public enum CharacterAttributes{ 
        FOREGROUND_BLUE = 0x0001, 
        FOREGROUND_GREEN = 0x0002, 
        FOREGROUND_RED = 0x0004, 
        FOREGROUND_INTENSITY = 0x0008, 
        BACKGROUND_BLUE = 0x0010, 
        BACKGROUND_GREEN = 0x0020, 
        BACKGROUND_RED = 0x0040, 
        BACKGROUND_INTENSITY = 0x0080, 
        COMMON_LVB_LEADING_BYTE = 0x0100, 
        COMMON_LVB_TRAILING_BYTE = 0x0200, 
        COMMON_LVB_GRID_HORIZONTAL = 0x0400, 
        COMMON_LVB_GRID_LVERTICAL = 0x0800, 
        COMMON_LVB_GRID_RVERTICAL = 0x1000, 
        COMMON_LVB_REVERSE_VIDEO = 0x4000, 
        COMMON_LVB_UNDERSCORE = 0x8000 
       } 

       static void ChangeColor(System.ConsoleColor color, System.ConsoleColor backgroundColor) { 
        uint written = 0; 
        COORD writeCoord = new COORD(0, 0); 
        ushort[] attribute = new ushort[400]; 

        IntPtr consoleOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE); 

        int consoleBufferWidth = Console.BufferWidth; 
        int consoleBufferLength = Console.BufferHeight; 
        //if (consoleBufferLength > Console.CursorTop) { 
        // consoleBufferLength = Console.CursorTop; 
        //} 

        for (int y = 0; y < consoleBufferLength; y++)  // rows 
        { 
         writeCoord.X = (short)0; 
         writeCoord.Y = (short)y; 
         ReadConsoleOutputAttribute(consoleOutputHandle, attribute, (uint)consoleBufferWidth, writeCoord, out written); 

         for (int x2 = 0; x2 < consoleBufferWidth; x2++){ // columns 
          attribute[x2] &= 0xFF00; // zero the background and foreground color 
          attribute[x2] |= (ushort)((((int)backgroundColor) << 4) | (int)color); 
         } 
         FillConsoleOutputAttribute(consoleOutputHandle, attribute[0], (uint)consoleBufferWidth, writeCoord, out written); 
        } 
       } 
      } 
     ]]> 
     </code> 
    </script>