joerdav / xc

Markdown defined task runner.
https://xcfile.dev/
MIT License
1.14k stars 27 forks source link

passing args to tasks #29

Closed mxcl closed 1 year ago

mxcl commented 1 year ago

When we were doing exe/md we supported passing args.

I wouldn’t say it was a homerun, it had caveats we hadn't resolved.

Anyway, thoughts?

joerdav commented 1 year ago

We're discussing a solution to that over in #23 . The idea is to have some environment variables that are required to be set for it to work, similar to make.

mxcl commented 1 year ago

well I was talking about xc foo bar where bar is an arg that the xc task can use. Env vars are no use to me in my usecase.

joerdav commented 1 year ago

I see, I think this could be a valuable feature. In the case of a task with multiple lines, what would you expect the behaviour to be?

joerdav commented 1 year ago

Would you expect it to work as shell scripts do?

# Task
## foo

echo "The Foo Task" echo $1

mxcl commented 1 year ago

So in general I have struggled here. The markdown is meant to be human readable, and here we are making it specific for a tool like xc. But it seems the right approach. We used $@ in exe/md where this means “all args” to most shells.

# Task
## foo

Use `xc foo [arg...]` to run this task.

```sh
echo "The Foo Task"
echo  "$@"
```\

Where xc supports $@ and $1 etc.

joerdav commented 1 year ago

Okay I can see that working. But I also agree that as a form of documentation it could get confusing.

In xc there's a concept of "attributes" that can modify a task in some way like working directory: https://xcfile.dev/task-syntax/directory/

One option could be to create a new Attribute where you can define inputs to a task:

# Task
## foo

Use `xc foo [bar] [baz]` to run this task.

Directory: ./some_dir
Inputs: BAR, BAZ
```sh
echo "The Foo Task"
echo  "$BAR"
echo  "$BAZ"


Then each input will be converted to an Environment variable.
This way I think it's clearer what is required to run the task? Thoughts?
mxcl commented 1 year ago

aah this is a good idea. Nice.

joerdav commented 1 year ago

I suppose this could also work nicely with #23 aswell?

# Task
## foo

Use `xc foo [bar] [baz]` to run this task.

Inputs: BAR, BAZ
```sh
echo "The Foo Task"
echo  "$BAR"
echo  "$BAZ"

Could be run with:

xc foo dog cat

or

BAR=dog BAZ=cat xc foo

mxcl commented 1 year ago

I like it. Elegant.

mxcl commented 1 year ago

Well I need $@, eg. I’m trying to add this to @teaxyz/brewkit:

# Tasks

## docker

```sh
export TEA_PREFIX="$SRCROOT"/docker/tea

docker run \
  --name tea \
  --rm \
  --volume "$SRCROOT"/docker/home:/root \
  --volume "$SRCROOT"/docker/opt:/root/.tea \
  --volume "$SRCROOT":/src \
  --env GITHUB_TOKEN=$(gh auth token) \
  --env TEA_PANTRY_PATH=/src \
  ghcr.io/teaxyz/infuser \
  tea +tea.xyz/brewkit "$@"
``\`

The number of args is variable.

I want to put the command in the README so people can see how they could do this manually and that the methods we are using compose other commands. For me that's the power of xc that you show the user how they could do it themselves.

Anyway, if you don’t feel it fits it's fine as I can just make the command separate. LMK

joerdav commented 1 year ago

I do think this fits actually, let me implement this and play around with it a bit too.

I may combine it with the above proposition of an Inputs attribute.

You can either defined Inputs for a task, or you can use the variadic notation to capture all args.

joerdav commented 1 year ago

I'm also working on a new build which uses temporary script files rather than running the tasks line by line. This should make running commands like your docker task easier.

joerdav commented 1 year ago

Need to document how this new feature works but the long/short of it is that with this feature, xc now supports $@ and numbered args like $1 within tasks.

As well as named Inputs that can be provided via args or via environment variables.

joerdav commented 1 year ago

Specifically I tested it out with your docker example @mxcl and it works as expected.

joerdav commented 1 year ago

Released in https://github.com/joerdav/xc/tree/v0.0.154

mxcl commented 1 year ago

Terrific! Thanks,