pw_env_setup: Implement command exit on Windows

This change adds exiting the env setup script on Windows after a command
fails.

Fixed: 147
Change-Id: I337453c75f5f8887c615a1e06ce5ef4d1d33e327
diff --git a/pw_env_setup/py/pw_env_setup/environment.py b/pw_env_setup/py/pw_env_setup/environment.py
index d363e3e..ccd543f 100644
--- a/pw_env_setup/py/pw_env_setup/environment.py
+++ b/pw_env_setup/py/pw_env_setup/environment.py
@@ -17,6 +17,9 @@
 import os
 import re
 
+# goto label written to the end of Windows batch files for exiting a script.
+_SCRIPT_END_LABEL = '_pw_end'
+
 
 class BadNameType(TypeError):
     pass
@@ -285,7 +288,7 @@
             return
 
         if windows:
-            pass  # TODO(pwbug/147) Fill in.
+            outs.write('if %ERRORLEVEL% neq 0 goto {}\n', _SCRIPT_END_LABEL)
         else:
             # Assume failing command produced relevant output.
             outs.write('if [ $? != 0 ]; then\n  return 1\nfi\n')
@@ -436,6 +439,9 @@
         for action in self._actions:
             action.write(outs, windows=self._windows)
 
+        if self._windows:
+            outs.write(':{}\n'.format(_SCRIPT_END_LABEL))
+
     @contextlib.contextmanager
     def __call__(self, export=True):
         """Set environment as if this was written to a file and sourced.