Colorize Windows mach builds

It's often hard to write cross-platform code for console coloring, and for mach there have not been exceptions.  It colorizes build and tests output, so that you can easily scroll and find warnings and failures, but unfortunately only on Unix consoles.

We are not new to this kind of issues in the Windows build environment, and there's a bug filed to properly support that, though, in the meanwhile, we can apply the same trick used with pymake to get colorization on mach.  Kudos to Shawn Wilsher and Justing Dolske for first implementing this idea with pymake, now let's also colorize our mach build!

First of all, you need a version of sed that supports the unbuffered option, download it from the update sed in mozilla-build bug and copy it to the msys/bin folder in your mozilla-build install path.

Then you need a .profile file in your user home folder, for example for me it's C:\Users\Marco\.profile, then just add this code snippet into the .profile file:

  # ANSI colors.
  _black=$(echo -e "\E[30m")
  _red=$(echo -e "\E[31m")
  _green=$(echo -e "\E[32m")
  _yellow=$(echo -e "\E[33m")
  _blue=$(echo -e "\E[34m")
  _magenta=$(echo -e "\E[35m")
  _cyan=$(echo -e "\E[36m")
  _white=$(echo -e "\E[37m")
  _bright=$(echo -e "\E[1m")
  _reset=$(echo -e "\E[0m")

  # Shortcuts for quick styling.

  ./mach $@ 2>&1 | sed -r -u \
    -e "s/^(.+)(:\swarning|warning:\s)(.+)$/$_warning\1\2\3$_reset/i" \
    -e "s/(leaked\s[0-9]+)(.+)$/$_leak\1\2$_reset/i" \
    -e "s/(adding:?|creating library|generating)(.+)$/\1$_info\2$_reset/i" \
    -e "s/(changing directory to:|processing script file:)(.+)$/\1$_info\2$_reset/i" \
    -e "s/^(\s?\S+\s){1}(processing\s)(\S+)$/\1\2$_info\3$_reset/" \
    -e "s/^(.+)(interrupted|\serror|No rule to make target)(\s.+)$/$_error\1\2\3$_reset/i" \
    -e "s/^(.?)(finished)(.+)$/$_info\1\2\3$_reset/i" \
    -e "s/^(\s?\S+\s+\-*\s?){1}(\S+)$/\1$_misc\2$_reset/" \
    -e "s/(TEST-PASS|result summary:|passed: [1-9]{1}[0-9]*)/$_result\1$_reset/i" \
    -e "s/(TEST-INFO)/$_info\1$_reset/i" \
    -e "s/(TEST-UNEXPECTED-\S+|failed: [1-9]{1}[0-9]*)/$_error\1$_reset/i" \
    -e "s/^(\s?[0-9]+\:[0-9]+\.[0-9]+\s)/$_time\1$_reset/"

At this point, just close and reopen the console (fwiw, I use console2 and these steps are just tested there) and you should be able to just execute mach build browser and get this nicer output, instead of a wall of white text:

Colorized mach

You are free to use, modify and adapt the rules and coloring to your needs, if you have improvements you can drop me a mail or ping me on IRC, the latest version is on my wiki page.

MaK Martedì 05 Marzo 2013 at 5:15 pm | | Mozilla-EN