Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add more examples for toml tasks #3491

Merged
merged 10 commits into from
Dec 12, 2024
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 116 additions & 3 deletions docs/tasks/toml-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ dir = "{{cwd}}" # run in user's cwd, default is the project's base directory
[tasks.lint]
description = 'Lint with clippy'
env = { RUST_BACKTRACE = '1' } # env vars for the script
# specify a shell command to run the script with (default is 'sh -c')
shell = 'bash -c'
# you can specify a multiline script instead of individual commands
run = """
#!/usr/bin/env bash
Expand Down Expand Up @@ -134,7 +132,7 @@ run = 'cargo test {{option(name="file")}}'
Flags are like options except they don't take values. They are defined in scripts with <span v-pre>
`{{flag()}}`</span>.

Example:
Examples:

```toml
[tasks.echo]
Expand All @@ -143,10 +141,125 @@ run = 'echo {{flag(name=("myflag")}}'
# runs: echo true
```

```toml
[tasks.maybeClean]
run = """
if [ '{{flag(name='clean')}}' = 'true' ]; then
echo 'cleaning'
fi
"""
# execute: mise run maybeClean --clean
# runs: echo cleaning
```

- `name`: The name of the flag. This is used for help/error messages.

The value will be `true` if the flag is passed, and `false` otherwise.

## Shell / Shebang

You can specify a shell command to run the script with (default is [`sh -c`](/configuration/settings.html#unix_default_inline_shell_args) or [`cmd /c`](/configuration/settings.html#windows_default_inline_shell_args)) or use a shebang:

```toml
[tasks.lint]
shell = 'bash -c'
run = "cargo clippy"
```

or use a shebang:

```toml
[tasks.lint]
run = """
#!/usr/bin/env bash
cargo clippy
"""
```

By using `shell` or `shebang`, you can run scripts in different languages:

::: code-group

```toml [python]
[tools]
python = 'latest'

[tasks.python_task]
run = """
#!/usr/bin/env -S python
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should reserve env -S for shebangs that have multiple args (like deno run). We should also document on this page what the heck env -S does in a callout of some kind. I suspect most users won't have a clue what it means. I think I already do that in the tips and tricks doc but this page is going to be far more heavily trafficked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I will update 👍

for i in range(10):
print(i)
"""
```

```toml [node]
[tools]
node = 'lts'

[tasks.node_task]
shell = 'node -e'
run = [
"console.log('First line')",
"console.log('Second line')",
]
```

```toml [bun]
[tools]
bun = 'latest'

[tasks.bun_shell]
shell = "bun -e"
run = """
// https://bun.sh/docs/runtime/shell
import { $ } from "bun";
const response = await fetch("https://example.com");
await $`cat < ${response} | wc -c`; // 1256
"""
```

```toml [deno]
[tools]
deno = 'latest'

[tasks.deno_task]
description = "Shows that you can use deno in a task"
shell = 'deno eval' # or use a shebang: #!/usr/bin/env -S deno run
run = """
import ProgressBar from "jsr:@deno-library/progress";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, this is what I meant

Suggested change
shell = 'deno eval' # or use a shebang: #!/usr/bin/env -S deno run
run = """
import ProgressBar from "jsr:@deno-library/progress";
run = """
#!/usr/bin/env -S deno run
import ProgressBar from "jsr:@deno-library/progress";

import { delay } from "jsr:@std/async";

if (!confirm('Start download?')) {
Deno.exit(1);
}

const progress = new ProgressBar({ title: "downloading:", total: 100 });
let completed = 0;
async function download() {
while (completed <= 100) {
await progress.render(completed++);
await delay(10);
}
}
await download();
"""
# ❯ mise run deno_task
# [download_task] $ import ProgressBar from "jsr:@deno-library/progress";
# Start download? [y/N] y
# downloading: ...
```

```toml [ruby]
[tools]
ruby = 'latest'

[tasks.ruby_task]
shell = 'ruby -e'
run = "puts 'Hello, world!'"
```

:::

## Windows

You can specify an alternate command to run on Windows by using the `run_windows` key:
Expand Down
Loading