poethepoet
poethepoet copied to clipboard
Feature Request: OS Specific Tasks
This is a feature request to enable different behaviors for a task depending on the OS it is running on.
My use case involves different arguments for a build system depending on the OS, but for the sake of simplicity the example will use two commands, windows_build
and unix_build
.
My task looks like this:
[tool.poe.tasks.build]
shell = "unix_build"
interpreter = ["posix"]
However, there is no way to run windows_build
on Windows instead of unix_build
.
Potentially a syntax somewhat like this?
[tool.poe.tasks.build#linux,macos]
shell = "unix_build"
interpreter = ["posix"]
[tool.poe.tasks.build#windows]
shell = "windows_build"
interpreter = ["powershell"]
Hi @kethan1, thanks for the feedback!
You're not the first to ask about something like this, so it seems like something worth supporting, but I'll need to think through and experiment with the potential approaches a bit more to ensure the solution fits well, particularly because it might overlap with some work I'm planning on conditional tasks.
Your idea of appending the OS requirement to the task name is interesting, although maybe appending it to the content key would work better (to avoid duplication/conflict of other task options) like
[poe.tasks.build]
help = "build using the correct process for your platform"
"cmd#windows" = "..."
"cmd#macos,linux" = "..."
Although maybe using dot and underscore instead of hash and comma would be more toml friendly
In the mean time a workaround could be one of:
- define a task per platform
- Use a shell (ie bash) task to execute different commands depending on the platform
- use a script task to manage platform detection and control flow in python
See previous discussion.
Thank you for considering this feature. The syntax you proposed, or something similar to it, is excellent!
@kethan1 I have't had time to work on this yet, but I wanted to share an idea I had for how to serve this use case with a general purpose mechanism, by adding a new switch
task type that runs one task from a collection based on the captured result of another task.
So for your use case it might look like:
[tool.poe.tasks.build]
control.script = "sys:stdout.write(sys.platform)"
[[tool.poe.tasks.build.switch]]
case = "win32"
cmd = "..."
[[tool.poe.tasks.build.switch]]
# no case value defined means this is the 'default' case
cmd = "..."
The control
option contains the definition for a task, in this instance a script task that outputs the current platform. This output is captured and used to decide which of the tasked defined in the switch array to execute by comparing the captured output to the value of their case option.
What do you think?
Personally I like that this is simple, generic and familiar, though it'll take a few tweaks to how tasks work make it work nicely.
I think that's a great idea that uses the toml structure quite nicely!
@kethan1 This feature is now available as part of v0.17.0.
Just got a chance to look at the release, and the new feature solves my issue. Thank you!