Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 43 additions & 2 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ String hooks are shorthand for `run` hooks. Use a hook table when you need to se
postinstall = { run = "echo 'installed'", shell = "bash -c" }
```

For `preinstall` and `postinstall`, `script = ...` is a legacy alias for `run = ...`. If a `shell` is also set on a `script` hook, mise warns that the shell is ignored and still runs the script with the default inline shell. Use `run = ...` with `shell = "bash -c"` to choose the inline shell command. The `script` alias for install hooks is deprecated.
For `preinstall` and `postinstall`, `script = ...` is a legacy alias for `run = ...`. If a `shell` is also set on a `script`/`scripts` hook, mise warns that the shell is ignored and still runs the script with the default inline shell. Use `run = ...` with `shell = "bash -c"` to choose the inline shell command. The `script` alias for install hooks is deprecated.

The `postinstall` hook receives a `MISE_INSTALLED_TOOLS` environment variable containing a JSON array of the tools that were just installed:

Expand Down Expand Up @@ -149,6 +149,30 @@ Hooks are executed with the following environment variables set:
Inline `run` hooks can be written as `{ run = "..." }` for any hook type. The string shorthand
(`enter = "echo hi"`) is equivalent to `{ run = "echo hi" }`.

`run` must be a string. `run = ["echo one", "echo two"]` is not supported.

To run separate spawned inline commands, define multiple hooks. Each hook entry is a separate
execution, so mise starts one subprocess per `run` entry:

```toml
[hooks]
enter = [
{ run = "echo one" },
{ run = "echo two" },
]
```

To run multiple shell lines in one spawned command, use one multiline `run` string. This is one hook
execution and one subprocess:

```toml
[hooks.enter]
run = """
echo one
echo two
"""
```

`run` hooks execute in a subprocess using the default inline shell:
[`unix_default_inline_shell_args`](/configuration/settings.html#unix_default_inline_shell_args)
or [`windows_default_inline_shell_args`](/configuration/settings.html#windows_default_inline_shell_args).
Expand All @@ -166,13 +190,30 @@ shell = "bash"
script = "source completions.sh"
```

Current-shell hooks may use `script`/`scripts` arrays:

```toml
[hooks.enter]
shell = "bash"
script = [
"source completions.sh",
"export PROJECT_READY=1",
]

[hooks.leave]
shell = "bash"
scripts = [
"unset PROJECT_READY",
]
```

`script` with `shell` is for current-shell hooks. Here, `shell` is a shell-name selector such as
`bash`, `zsh`, or `fish`, not an inline shell command like `bash -c`. mise only prints the script
when the active `mise activate` shell matches.

Use `run` when the hook should execute as an inline command in a subprocess. `preinstall` and
`postinstall` do not have a current shell, so `script` is only kept there as a legacy alias for `run`;
if `shell` is set with `script` on those hooks, it is ignored.
if `shell` is set with `script`/`scripts` on those hooks, it is ignored.

::: warning
I feel this should be obvious but in case it's not, this isn't going to do any sort of cleanup
Expand Down
43 changes: 43 additions & 0 deletions e2e/config/test_hooks_script_arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

cat <<'EOF' >mise.toml
[tools]
dummy = 'latest'

[hooks.enter]
shell = "bash"
script = [
'export MISE_HOOK_SCRIPT_ARRAY_ONE=one',
'export MISE_HOOK_SCRIPT_ARRAY_TWO=two',
]

[hooks.leave]
shell = "bash"
scripts = [
'export MISE_HOOK_SCRIPTS_ARRAY_LEFT=yes',
]

[hooks]
preinstall = { scripts = ['echo PRE_SCRIPT_ONE', 'echo PRE_SCRIPT_TWO'] }
postinstall = { script = ['echo POST_SCRIPT_ONE', 'echo POST_SCRIPT_TWO'] }
EOF

output=$(mise install 2>&1)
assert_contains "echo '$output'" "PRE_SCRIPT_ONE"
assert_contains "echo '$output'" "PRE_SCRIPT_TWO"
assert_contains "echo '$output'" "POST_SCRIPT_ONE"
assert_contains "echo '$output'" "POST_SCRIPT_TWO"

eval "$(mise hook-env)"

cd ~ || exit 1
eval "$(mise hook-env)"
# shellcheck disable=SC2016
assert 'echo "$MISE_HOOK_SCRIPTS_ARRAY_LEFT"' "yes"

cd ~/workdir || exit 1
eval "$(mise hook-env)"
# shellcheck disable=SC2016
assert 'echo "$MISE_HOOK_SCRIPT_ARRAY_ONE"' "one"
# shellcheck disable=SC2016
assert 'echo "$MISE_HOOK_SCRIPT_ARRAY_TWO"' "two"
213 changes: 113 additions & 100 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -2040,110 +2040,123 @@
"required": ["version"],
"type": "object"
},
"hook_run_string": {
"description": "inline hook command to run",
"type": "string"
},
"hook_run_table": {
"unevaluatedProperties": false,
"properties": {
"run": {
"description": "inline hook command to run",
"type": "string"
},
"shell": {
"description": "inline shell command to use for run hooks, such as \"bash -c\"",
"type": "string"
}
},
"required": ["run"],
"type": "object"
},
"hook_script_value": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"hook_scripts_value": {
"type": "array",
"items": {
"type": "string"
}
},
"hook_script_table": {
"unevaluatedProperties": false,
"properties": {
"script": {
"description": "current-shell script/scripts for enter/leave/cd hooks",
"$ref": "#/$defs/hook_script_value"
},
"shell": {
"description": "current shell name for script hooks, such as \"bash\"",
"type": "string"
}
},
"required": ["script"],
"type": "object"
},
"hook_scripts_table": {
"unevaluatedProperties": false,
"properties": {
"scripts": {
"description": "current-shell scripts for enter/leave/cd hooks",
"$ref": "#/$defs/hook_scripts_value"
},
"shell": {
"description": "current shell name for script hooks, such as \"bash\"",
"type": "string"
}
},
"required": ["scripts"],
"type": "object"
},
"hook_task_ref": {
"unevaluatedProperties": false,
"properties": {
"task": {
"description": "mise task to run",
"type": "string"
}
},
"required": ["task"],
"type": "object"
},
"hook_definition_item": {
"oneOf": [
{
"$ref": "#/$defs/hook_run_string"
},
{
"$ref": "#/$defs/hook_run_table"
},
{
"$ref": "#/$defs/hook_script_table"
},
{
"$ref": "#/$defs/hook_scripts_table"
},
{
"$ref": "#/$defs/hook_task_ref"
}
]
},
"hook_definition": {
"oneOf": [
{
"$ref": "#/$defs/hook_definition_item"
},
{
"description": "scripts/tasks to run",
"items": {
"$ref": "#/$defs/hook_definition_item"
},
"type": "array"
}
]
},
"hooks": {
"description": "hooks to run",
"type": "object",
"additionalProperties": {
"oneOf": [
{
"description": "inline hook command to run",
"type": "string"
},
{
"unevaluatedProperties": false,
"properties": {
"run": {
"description": "inline hook command to run",
"type": "string"
},
"shell": {
"description": "inline shell command to use for run hooks, such as \"bash -c\"",
"type": "string"
}
},
"required": ["run"],
"type": "object"
},
{
"unevaluatedProperties": false,
"properties": {
"script": {
"description": "current-shell script for enter/leave/cd hooks",
"type": "string"
},
"shell": {
"description": "current shell name for script hooks, such as \"bash\"",
"type": "string"
}
},
"required": ["script"],
"type": "object"
},
{
"unevaluatedProperties": false,
"properties": {
"task": {
"description": "mise task to run",
"type": "string"
}
},
"required": ["task"],
"type": "object"
},
{
"description": "scripts/tasks to run",
"items": {
"oneOf": [
{
"description": "inline hook command to run",
"type": "string"
},
{
"unevaluatedProperties": false,
"properties": {
"run": {
"description": "inline hook command to run",
"type": "string"
},
"shell": {
"description": "inline shell command to use for run hooks, such as \"bash -c\"",
"type": "string"
}
},
"required": ["run"],
"type": "object"
},
{
"unevaluatedProperties": false,
"properties": {
"script": {
"description": "current-shell script for enter/leave/cd hooks",
"type": "string"
},
"shell": {
"description": "current shell name for script hooks, such as \"bash\"",
"type": "string"
}
},
"required": ["script"],
"type": "object"
},
{
"unevaluatedProperties": false,
"properties": {
"task": {
"description": "mise task to run",
"type": "string"
}
},
"required": ["task"],
"type": "object"
}
]
},
"type": "array"
}
]
"$ref": "#/$defs/hook_definition"
}
},
"watch_files": {
Expand Down
Loading
Loading