Search code examples
haskellshake-build-system

Handling effects on the shell environment with the shake build system


I am running bash on windows (not the fancy new thing on windows 10, mingw bash) and from there shake. From what I understand it is possible for a command on windows to edit the environment of cmd. In my usecase I don't want to make assumptions on how the environment will be changed, I just want the next command to run in that changed environment. Is that possible?

The ideal scenario would be something like

(Environment env, Stdout out) <- cmd Shell (Cwd dir) "path/to/script.cmd" [arg1] [arg2]
-- Do stuff with out
cmd Shell (Cwd dir) (Env env) "env/dependent/command.cmd"

I would also be happy with running both commands in the same shell even if it means that I can't Do stuff with out but I couldn't figure out how to do that either

I guess in makefile terms what I want is similar to .ONESHELL:


Solution

  • Each cmd invocation in Shake is separate, so any environment variables don't persist. Assuming you want to treat script.cmd as a black box, there are two options:

    1. Run both commands in a single step. Something like: Stdout out <- cmd Shell (Cwd dir) "path/to/script.cmd" arg1 arg2 "&& env/dependent/command.cmd".
    2. Capture the environment from the first command and pass it into the second. You can capture the environment by running path/to/script.cmd .... && set, capturing that with Stdout, parsing it, and then supplying it with Env.

    If you want to reuse the Env more than once then the second is probably faster, but both should work.

    A worked implementation of the second approach, including set output parser, is available here (by @fakedrake).