Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run 'color' command in NAnt script

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?

like image 910
RodH257 Avatar asked Feb 27 '23 12:02

RodH257


1 Answers

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>
like image 173
Martin Vobr Avatar answered Apr 05 '23 17:04

Martin Vobr