pietroppeter
pietroppeter

Reputation: 1473

How to redirect output in a task in nimble

If I have this task in a nimble file:

task readme, "generate README.md":
  exec "nim c -r readme.nim > README.md"

with this readme.nim:

echo "# Hello\nworld"

executing task with nimble (nimble readme) does not redirect the output of readme.nim to file. As expected running nim c -r readme.nim > README.md from terminal correctly creates/updates README.md.

Is this intended behaviour of nimble? is there a workaround?

note: the above was tested on windows.

Upvotes: 1

Views: 325

Answers (2)

pietroppeter
pietroppeter

Reputation: 1473

thanks to answer by @xbello and ensuing discussion, I found a good workaround for my use case:

task readme, "generate README.md":
  exec "nim c readme.nim"
  "README.md".writeFile(staticExec("readme"))

the explanation to why the simple exec has to do with the fact that nimble uses nimscript.exec which internally uses rawExec which is a builtin that (judging from different behaviours reported here for windows and linux) is not entirely cross-platform when it regards output pipeline.

Upvotes: 2

xbello
xbello

Reputation: 7443

I end up with the expected README.md:

$ cat README.md
# Hello
world

But sometimes (the readme.nim has to be compiled or recompiled) I end up with something like this:

CC: readme.nim
# Hello
world

That is, the full stdout (not the stderr) of the nim c -r readme.nim command, as expected. As a workaround you could encapsulate what you want to do in the readme.nim:

import os

let f: File = open(commandLineParams()[0], fmWrite)
f.write "# Hello\nworld"
f.close()

And in your nimble file:

task readme, "generate README.md":         
  exec "nim c -r readme.nim README.md"

Another workaround could be to suppress the output of nim c:

task readme, "generate README.md":
  exec "nim c --verbosity:0 -r readme.nim > README.md"

Upvotes: 1

Related Questions