Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running grunt from MSBuild

This thread (Problem capturing error output) gives you a taste of the problem I'm struggling with. I'm trying to run grunt from MSBuild and the grunt errors are not displayed in the Visual Studio output window. I have a .NET project in Visual Studio Express 2012 for Web. I have imported an external project into the project build file with the IMPORT tag and in the imported project I have an Exec task attempting to run grunt. I obviously want to see the error messages that grunt outputs in my Visual Studio output window without too much fuss. I found an extremely simple workaround that at least sends the output to a text file.

grunt.cmd > grunt-output.txt

This output file is in my .NET project folder somewhere so a quick refresh and double click allows me to open the output file and see a slightly garbled version of the grunt output in Visual Studio.

As an example I'm running a lint task on the grunt.js file, which contains things which JSHint would object to. I deliberately didn't put a semi-colon after var hello and so you get the error message Missing semicolon.

From the command line I get a nicely formatted error message.

Running "lint:files" (lint) task
Linting grunt.js...ERROR
[L2:C10] Missing semicolon.
var hello

<WARN> Task "lint:files" failed. Use --force to continue. </WARN>

Aborted due to warnings.

When I run it from Visual Studio, the output file contains this cluttered format:

    [4mRunning "lint:files" (lint) task[24m
     Linting grunt.js...[31mERROR[39m
     [31m[[39m[33mL2[39m[31m:[39m[33mC10[39m[31m][39m [33mMissing semicolon.[39m
     var hello[31m[7m [27m[39m

     [31m<[39m[33mWARN[39m[31m>[39m [33mTask "lint:files" failed. Use --force to continue.     [39m [31m</[39m[33mWARN[39m[31m>[39m

     [31mAborted due to warnings.[39m

Does anyone recognise what all those square brackets and numbers are doing, and can anyone think of a command line switch or grunt switch or node.js switch that would interpret them and turn them into formatting? The don't look like some kind of encoding, they look more like tags to suggest to the command line environment how to format the message. Please don't suggest running some kind of regular expression replace function. I want something quick and easy otherwise it would become more trouble than it's worth.

UPDATE: this link Output gets cut off if piped into another application is pointing to a problem further upstream in node dating back 10 months. While that's getting sorted out it would be nice to at least get a more readable output file.

like image 303
DavidHyogo Avatar asked Jan 27 '13 13:01

DavidHyogo


1 Answers

This thread on the grunt message board Pipe-redirecting Grunt's output is broken addresses this issue perfectly and provides a quick workaround while we wait for the overall issue to get fixed. They are escape codes to colour the output and the workaround is to use the --no-color option to remove colouring.

When I run this command from MSBuild

grunt.cmd --no-color > grunt-output.txt

I get nicely formatted output with exactly the same content as the command line:

Running "lint:files" (lint) task
Linting grunt.js...ERROR
[L2:C10] Missing semicolon.
var hello 

<WARN> Task "lint:files" failed. Use --force to continue. </WARN>

Aborted due to warnings.

I can live without the colour. It would be nice if this could be sent to the output window, though, because MSBuild throws what seems like an error in the build process when in fact it's just JSHint tactfully hurting my feelings about my JavaScript.

like image 53
DavidHyogo Avatar answered Nov 15 '22 23:11

DavidHyogo