如何运行“颜色” NAnt 脚本中的命令

发布于 2024-09-13 10:16:33 字数 246 浏览 8 评论 0原文

我刚刚开始使用 Nant 进行构建和测试。我想让它在失败时更改命令提示符文本的颜色(或背景),以便很容易注意到。

Windows 上命令提示符中的命令是“color 4”,将其更改为红色,“color 7”将其更改回白色。

我如何使其在构建脚本中运行,echo 不起作用,exec 不起作用(尽管可能使用了 exec 错误)。我宁愿不必运行 perl 等,只是为了做一些在标准命令提示符窗口中轻松完成的事情。

有谁知道该怎么做?

I've just started using Nant for my builds and tests. I want to make it change the color (or background) of my command prompt text when it fails so its easily noticed.

The command in command prompt on Windows is 'color 4' to change it to red and color 7 for back to white.

How do I make this run in a build script, echo doesn't work, exec doesn't work (may be using exec wrong though). I'd prefer to not have to run perl etc just to do something which is easily done in a standard command prompt window.

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-09-20 10:16:33

尝试使用自定义任务。如果该任务包含在 nant 文件中,您将不会有任何外部依赖项。

<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>

Try using a custom task. If the task is included in the nant-file you'll not have any external dependency.

<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>
自由如风 2024-09-20 10:16:33

作为我对 @Martin-Vobr 帖子的评论的后续:

我添加了额外的逻辑来正确更改背景。这将允许在命令窗口中启动构建,然后可以一目了然地检查进度。我使用蓝色背景表示“构建”,绿色表示“成功”,红色表示“失败”。

 <!-- 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>

As a follow up to my comment on the post by @Martin-Vobr:

I have added additional logic to properly change the background. This will allow a build to be initiated in a command window, then progress can be checked at a glance. I use a blue background for "building", green for "Success" and red for "Failure".

 <!-- 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文