Skip to content

Commit

Permalink
feat: added MISE_ORIGINAL_CWD to tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 26, 2024
1 parent 5e8d801 commit c4ce0d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/tasks/file-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ Completions will now be enabled for your task. In this example, `mise run build
will show `debug` and `release` as options. The `--user` flag will also show completions generated by the output of `mycli users`.

(Note that cli and markdown help for tasks is not yet implemented in mise as of this writing but that is planned.)

## CWD

mise sets the current working directory to the directory of `mise.toml` before running tasks.
This can be overridden by setting `dir="{{cwd}}"` in the task header:

```bash
#!/usr/bin/env bash
#MISE dir="{{cwd}}"
```

Also, the original working directory is available in the `MISE_ORIGINAL_CWD` environment variable:

```bash
#!/usr/bin/env bash
cd "$MISE_ORIGINAL_CWD"
```
11 changes: 11 additions & 0 deletions docs/tasks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ as `~/src/work/myproject/mise.local.toml` and they will be used inside tasks of

As of this writing vars are only supported in TOML tasks. I want to add support for file tasks, but
I don't want to turn all file tasks into tera templates just for this feature.

## Environment variables passed to tasks

The following environment variables are passed to the task:

- `MISE_ORIGINAL_CWD`: The original working directory from where the task was run.
- `MISE_CONFIG_ROOT`: The directory containing the `mise.toml` file where the task was defined.
- `MISE_PROJECT_ROOT`: The root of the project.
- `MISE_TASK_NAME`: The name of the task being run.
- `MISE_TASK_DIR`: The directory containing the task script.
- `MISE_TASK_FILE`: The full path to the task script.
17 changes: 16 additions & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ impl Run {
..Default::default()
})?;
let mut env = ts.env_with_path(&CONFIG)?;
if let Some(cwd) = &*dirs::CWD {
env.insert("MISE_ORIGINAL_CWD".into(), cwd.display().to_string());
}
if let Some(root) = &CONFIG.project_root {
env.insert("MISE_PROJECT_ROOT".into(), root.display().to_string());
env.insert("root".into(), root.display().to_string());
Expand Down Expand Up @@ -300,6 +303,16 @@ impl Run {
return Ok(());
}

let mut env = env.clone();
env.insert("MISE_TASK_NAME".into(), task.name.clone());
let task_file = task.file.as_ref().unwrap_or(&task.config_source);
env.insert("MISE_TASK_FILE".into(), task_file.display().to_string());
if let Some(dir) = task_file.parent() {
env.insert("MISE_TASK_DIR".into(), dir.display().to_string());
}
if let Some(config_root) = &task.config_root {
env.insert("MISE_CONFIG_ROOT".into(), config_root.display().to_string());
}
let string_env: Vec<(String, String)> = task
.env
.iter()
Expand Down Expand Up @@ -417,7 +430,9 @@ impl Run {
trace!("using shell: {}", shell.join(" "));
let mut full_args = shell.clone();
let mut script = script.to_string();
if shell[0] == "sh" || shell[0] == "bash" || shell[0] == "zsh" {
if script.lines().count() > 1
&& (shell[0] == "sh" || shell[0] == "bash" || shell[0] == "zsh")
{
script = format!("set -e\n{}", script);
}
if !args.is_empty() {
Expand Down
5 changes: 2 additions & 3 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct Task {
#[serde(default)]
pub env: BTreeMap<String, EitherStringOrIntOrBool>,
#[serde(default)]
pub dir: Option<PathBuf>,
pub dir: Option<String>,
#[serde(default)]
pub hide: bool,
#[serde(default)]
Expand Down Expand Up @@ -313,13 +313,12 @@ impl Task {
if let Some(dir) = &self.dir {
// TODO: memoize
// let dir = self.dir_rendered.get_or_try_init(|| -> Result<PathBuf> {
let dir = dir.to_string_lossy().to_string();
let mut tera = get_tera(self.config_root.as_deref());
let mut ctx = BASE_CONTEXT.clone();
if let Some(config_root) = &self.config_root {
ctx.insert("config_root", config_root);
}
let dir = tera.render_str(&dir, &ctx)?;
let dir = tera.render_str(dir, &ctx)?;
let dir = file::replace_path(&dir);
if dir.is_absolute() {
Ok(Some(dir.to_path_buf()))
Expand Down

0 comments on commit c4ce0d1

Please sign in to comment.